程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestInput extends Container
{
        int x=0;
        int Height=0,Width=0;
        
        TextField textfield;
        JPanel panel;
        String text="";
        Dimension dt;
        Dimension dc;
        
        public TestInput()
        {
                super();
                panel=new JPanel();
                textfield=new TextField(15);                textfield.setLocation(200,0);
                panel.add(textfield);
                this.add(panel);
        }
        
        public int getWidth()
        {
                return Width=301;
        }
        
        public int getHeight()
        {
                return Height=25;
        }
        
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestFrame extends JFrame
{
        public TestFrame()
        {
                JFrame f=new JFrame("Test");
                
                TestInput input=new TestInput();
            input.setLocation(0,20);
                System.out.println("Input's width: "+input.getWidth()+","+"Input's heitht: "+input.getHeight());
                
                this.setLayout(new BorderLayout());
                getContentPane().add("North",new Label("This is a test label!This is a test label!These are books for the students!"));
                getContentPane().add("Center",input);
                getContentPane().add("South",new Label("This is a test label!"));
        }
        public static void main(String args[])
        {
                TestFrame app=new TestFrame();
                app.setSize(500,400);
                app.setVisible(true);
                
                app.addWindowListener(new WindowAdapter()
                {
                        public void windowClosing(WindowEvent e)
                        {
                                System.exit(0);
                        }
                });
        }
}
显示出来以后,那个TextField总是向左偏移了一些,显示不完全。不明白为什么?
请各位大侠赐教!

解决方案 »

  1.   

    会不会是那个Input继承了Container的缘故?看不出来。
      

  2.   

    把继承container改为继承JPanle就可以了。
      

  3.   

    import java.awt.*;
    import javax.swing.*;class TestInput extends JPanel {
    int x = 0; int Height = 0, Width = 0; TextField textfield; JPanel panel; String text = ""; Dimension dt; Dimension dc; public TestInput() {
    super();
    panel = new JPanel();
    panel.setLayout(new FlowLayout());
    textfield = new TextField(15);
    panel.add(textfield);
    this.add(panel);
    } public int getWidth() {
    return Width = 301;
    } public int getHeight() {
    return Height = 25;
    }
    }public class JustTest extends JFrame { public JustTest() {
    super("Test"); TestInput input = new TestInput();
    System.out.println("Input's width: " + input.getWidth() + ","
    + "Input's heitht: " + input.getHeight()); this.setLayout(new BorderLayout());
    add("North",new Label("This is a test label!"));
    add("Center", input);
    add("South", new Label("This is a test label too!")); setSize(500, 400);
    setVisible(true);
    } public static void main(String args[]) {
    new JustTest();
    }
    }
      

  4.   

    虽然现在这样是好用的,但是我把那个TestInput加入到Frame中去的时候是先加到一个Container中(这个Container没有用布局管理器,所以我才返回TestInput的Width和Height,用来计算坐标的。),再加入到Frame中,就算改为继承JPanel结果还是一样