这个TextField的显示为什么总是不完全?

mixianger 2006-03-01 01:44:48
程序:
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总是向左偏移了一些,显示不完全。不明白为什么?
请各位大侠赐教!
...全文
191 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mixianger 2006-03-02
  • 打赏
  • 举报
回复
虽然现在这样是好用的,但是我把那个TestInput加入到Frame中去的时候是先加到一个Container中(这个Container没有用布局管理器,所以我才返回TestInput的Width和Height,用来计算坐标的。),再加入到Frame中,就算改为继承JPanel结果还是一样
mixianger 2006-03-02
  • 打赏
  • 举报
回复
谢谢楼上的!
f_acme 2006-03-02
  • 打赏
  • 举报
回复
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();
}
}
f_acme 2006-03-02
  • 打赏
  • 举报
回复
把继承container改为继承JPanle就可以了。
mixianger 2006-03-02
  • 打赏
  • 举报
回复
还有别的更好的想法或解释吗?
期盼大家的帮助!
谢谢!
f_acme 2006-03-01
  • 打赏
  • 举报
回复
会不会是那个Input继承了Container的缘故?看不出来。

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧