日期:2014-05-20  浏览次数:20712 次

JAVA图形化编程,菜鸟问题
我把Frame修改成全屏大小后,相应的TextField,Label位置也变了,我想把 起点 到 查询按钮中间的显示内容,放在左上角顶部,TextArea放在它们下面。
代码如下:
import java.awt.*;
import java.awt.event.*;

public class Interface {
private Frame f;
private TextField tf;
private TextField tf2;
private Label lab1;
private Label lab2;
private Label lab3;
private Button b;
private TextArea ta;

private Dialog d;
private Label lab;
private Button okBut;

private HignWay hignWay; //类对象

public Interface() {
init();
}

public void init() {
f = new Frame("江苏省高速公路路网行驶路径计算程序");
f.setBounds(0, 0, 1280, 1000);
f.setBackground(Color.lightGray);
f.setLayout(new FlowLayout(FlowLayout.LEFT));

lab1 = new Label("起点");
lab1.setLocation(0,0);
lab2 = new Label("到");
lab3 = new Label("终点");
tf = new TextField(20);
tf2 = new TextField(20);
b = new Button("查询");
ta = new TextArea();
ta.setBounds(0,50,200,300 );
d = new Dialog(f,"警告提示",true);
d.setBounds(400,200,300,150); //x,y,width,height
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");

d.add(lab);
d.add(okBut);
f.add(lab1);
f.add(tf);
f.add(lab2);
f.add(tf2);
f.add(lab3);
f.add(b);
f.add(ta);

myEvent();
f.setVisible(true);

hignWay = new HignWay(); //实例化
hignWay.init(); //实例化后调用init方法
}

private void myEvent() {
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false); //Visible可见的,false则不可见
}
});

d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
}
);

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) 
{
System.exit(0);
}
});

tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
showDir();
}
});

tf2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
showDir();
}
});

b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showDir();
}
});
}

修改前的效果图如下:


修改后的效果图如下:


------解决方案--------------------
你用的是FloatLayout肯定会这样的,要想窗体放大放下,其中的组建位置和大小不变的话,建议不要使用布局管理器,f.setLayout(null); 然后用setBounds(x,y,width,height)或者setPosition(x,y)和setSize(width,height)。