刚刚学习了GUI,碰到问题,急需要同志们解决import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Order extends JFrame {
public static void main(String [] args){ JFrame frame = new JFrame("国内机票订票系统");
frame.setLocation(200, 200);//订票系统主界面位置
frame.setSize(600, 600);//订票系统主界面大小
frame.setBackground(Color.GRAY);//订票系统主界面背景颜色 JButton button1 = new JButton("提交订票信息");
JButton button2 = new JButton("返回主界面");
FlowLayout layout = new FlowLayout(FlowLayout.CENTER ,150,450);
frame.setLayout(layout);//设置布局管理器
frame.add(button1);//添加按钮1
frame.add(button2);//添加按钮2
frame.setVisible(true);//主界面可见
frame.setResizable(false);//主界面大小不可变 }
}运行结果
下面添加事件监听器
package javaapplication2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class Order extends JFrame { public static void main(String [] args){ JFrame frame = new JFrame("国内机票订票系统");
frame.setLocation(200, 200);//订票系统主界面位置
frame.setSize(600, 600);//订票系统主界面大小
frame.setBackground(Color.GRAY);//订票系统主界面背景颜色
JButton button1 = new JButton("提交订票信息");
JButton button2 = new JButton("返回主界面");
FlowLayout layout = new FlowLayout(FlowLayout.CENTER ,150,450);
frame.setLayout(layout);//设置布局管理器
frame.add(button1);//添加按钮1
frame.add(button2);//添加按钮2
Monitor bh = new Monitor();
button1.addActionListener(bh);
frame.add(button1,FlowLayout.CENTER);
frame.pack();
frame.setVisible(true);//主界面可见
frame.setResizable(false);//主界面大小不可变
}
}
class Monitor extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.print("已提交订票信息!请稍后!");
}
}结果,运行的界面图,两个按钮倒置了,下面尺寸还扩大了一倍
import javax.swing.*;
import java.awt.event.*;
public class Order extends JFrame {
public static void main(String [] args){ JFrame frame = new JFrame("国内机票订票系统");
frame.setLocation(200, 200);//订票系统主界面位置
frame.setSize(600, 600);//订票系统主界面大小
frame.setBackground(Color.GRAY);//订票系统主界面背景颜色 JButton button1 = new JButton("提交订票信息");
JButton button2 = new JButton("返回主界面");
FlowLayout layout = new FlowLayout(FlowLayout.CENTER ,150,450);
frame.setLayout(layout);//设置布局管理器
frame.add(button1);//添加按钮1
frame.add(button2);//添加按钮2
frame.setVisible(true);//主界面可见
frame.setResizable(false);//主界面大小不可变 }
}运行结果
下面添加事件监听器
package javaapplication2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class Order extends JFrame { public static void main(String [] args){ JFrame frame = new JFrame("国内机票订票系统");
frame.setLocation(200, 200);//订票系统主界面位置
frame.setSize(600, 600);//订票系统主界面大小
frame.setBackground(Color.GRAY);//订票系统主界面背景颜色
JButton button1 = new JButton("提交订票信息");
JButton button2 = new JButton("返回主界面");
FlowLayout layout = new FlowLayout(FlowLayout.CENTER ,150,450);
frame.setLayout(layout);//设置布局管理器
frame.add(button1);//添加按钮1
frame.add(button2);//添加按钮2
Monitor bh = new Monitor();
button1.addActionListener(bh);
frame.add(button1,FlowLayout.CENTER);
frame.pack();
frame.setVisible(true);//主界面可见
frame.setResizable(false);//主界面大小不可变
}
}
class Monitor extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.print("已提交订票信息!请稍后!");
}
}结果,运行的界面图,两个按钮倒置了,下面尺寸还扩大了一倍
frame.pack();
这两行去掉就可以了.不知道你加上去是干什么用的.
让frame自动根据组件的大小调整自己的大小,去掉看看!
1、为什么要把一个对象向frame增加两次呢?你把同一个组件向frame增加两次,只有后一个才会起作用,所以,“提交订票信息”到后面啦
。
2、你指定了frame的尺寸,又指定了Layout的组件水平间隔和垂直间隔,为什么还要使用pack呢?这样一来frame的尺寸设置就失去作用了,然后系统会根据Layout对组件水平间隔和垂直间隔的设置,自动计算frame的尺寸,于是窗口比你设想的要大。
3、另外,你使用的是FlowLayout,那么你知道这个Layout的基本约定吗?通过API可以知道,这是一个从左到右的布局模式,默认是居中的,一行放不下,自动转下行,那么,为什么在增加组件时要指定FlowLayout.CENTER?你这样指定实际是不会有用的。(当然,这条和你提出的问题没有关系)