这个TextField的显示为什么总是不完全?
程序:
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总是向左偏移了一些,显示不完全。不明白为什么?
请各位大侠赐教!
问题点数:20、回复次数:6Top
1 楼f_acme(沧海一声笑)回复于 2006-03-01 23:26:17 得分 3
会不会是那个Input继承了Container的缘故?看不出来。Top
2 楼f_acme(沧海一声笑)回复于 2006-03-02 09:15:41 得分 10
把继承container改为继承JPanle就可以了。Top
3 楼f_acme(沧海一声笑)回复于 2006-03-02 09:16:46 得分 7
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();
}
}
Top
4 楼mixianger(米香儿)回复于 2006-03-02 10:28:14 得分 0
谢谢楼上的!Top
5 楼mixianger(米香儿)回复于 2006-03-02 11:07:37 得分 0
虽然现在这样是好用的,但是我把那个TestInput加入到Frame中去的时候是先加到一个Container中(这个Container没有用布局管理器,所以我才返回TestInput的Width和Height,用来计算坐标的。),再加入到Frame中,就算改为继承JPanel结果还是一样Top
6 楼mixianger(米香儿)回复于 2006-03-02 16:53:46 得分 0
还有别的更好的想法或解释吗?
期盼大家的帮助!
谢谢!Top




