仿照教程上编写火车票订票系统的界面图,采用GridLayout来进行页面布局。
具体的GUI图如下所示:
案例中采用GridLayout(3,2)布局,但是它给出的布局文件如下:
从图中我觉得应该是采用GridLayout(3,1).也就是三行一列,我不清楚是我理解错误还是教程有问题
我仿照教程写的代码如下:
package com.charmingD.GridLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class GridLayoutTest {
private JFrame mJFrame;
private JPanel mJPanel1;
private JLabel deparstureJLabel;
private JLabel destinationJLabel;
private JLabel trainNumJLabel;
private JList deparstureJList;
private JList destinationJList;
private JList trainNumJList;
String[] deparsture = {"北京","上海","杭州"};
String[] destination = {"北京","上海","杭州"};
String[] trainNum = {"K1314","G520","T250"};
private JPanel mJPanel2;
private JLabel mNameJLabel;
private JLabel mPhoneNum;
private JTextField mNameTextField;
private JTextField mPhoneTextField;
private JPanel mJPanel3;
private JLabel buttonJLabel;
private JRadioButton mButton1;
private JRadioButton mButton2;
private ButtonGroup group;
public GridLayoutTest() {
// TODO Auto-generated constructor stub
mJFrame = new JFrame("Test GridLayout!!!");
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mJFrame.setVisible(true);
mJFrame.getContentPane().setLayout(new GridLayout(3,2));
mJPanel1 = new JPanel();
mJPanel1.setLayout(new GridLayout(3,1));
deparstureJLabel = new JLabel("出发地:");
mJPanel1.add(deparstureJLabel);
deparstureJList = new JList(deparsture);
deparstureJList.setVisibleRowCount(1);
JScrollPane scolldepar = new JScrollPane(deparstureJList);
mJPanel1.add(scolldepar);
destinationJLabel = new JLabel("目的地:");
mJPanel1.add(destinationJLabel);
destinationJList = new JList(destination);
destinationJList.setVisibleRowCount(1);
JScrollPane scolldesti = new JScrollPane(destinationJList);
mJPanel1.add(scolldesti);
trainNumJLabel = new JLabel("车次:");
mJPanel1.add(trainNumJLabel);
trainNumJList = new JList(trainNum);
trainNumJList.setVisibleRowCount(1);
JScrollPane scolltrainNo = new JScrollPane(trainNumJList);
mJPanel1.add(scolltrainNo);
mJFrame.getContentPane().add(mJPanel1);
mJPanel2 = new JPanel();
mJPanel2.setLayout(new GridLayout(2,1));
mNameJLabel = new JLabel("客户姓名:");
mJPanel2.add(mNameJLabel);
mNameTextField = new JTextField(20);
mJPanel2.add(mNameJLabel);
mPhoneNum = new JLabel("客户联系方式:");
mJPanel2.add(mPhoneNum);
mPhoneTextField = new JTextField(12);
mJPanel2.add(mPhoneTextField);
mJFrame.getContentPane().add(mJPanel2);
mJPanel3 = new JPanel();
mJPanel3.setLayout(new FlowLayout());
buttonJLabel = new JLabel("是否需要送票:");
mJPanel3.add(buttonJLabel);
mButton1 = new JRadioButton("需要送票");
mButton2 = new JRadioButton("不需要送票");
group = new ButtonGroup();
group.add(mButton1);
group.add(mButton2);
mJPanel3.add(mButton1);
mJPanel3.add(mButton2);
mJFrame.getContentPane().add(mJPanel3);
mJFrame.pack();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new GridLayoutTest();
}
}
得到的结果图如下:
首先结果运行的和理想结果是有差异的,其中不清楚为什么mPhoneTextField组件没有显示出来?
另外不管结果正不正确,我将其中的布局文件参数从新更改后,如下:
mJFrame.getContentPane().setLayout(new GridLayout(3,1));
mJPanel1.setLayout(new GridLayout(3,2));
mJPanel2.setLayout(new GridLayout(2,2));
结果图还是跟上面的一样,我不清楚是不是Java的GUI设计机制不成熟啊?
求大神给与解答啊?
------解决思路----------------------
mJPanel2.add(mNameJLabel);
这句写了两遍,反倒是mNameTextField没有加进去
------解决思路----------------------
那个什么没显示的,你注意看下是否add进去了,名字对不对。
还有你说的显示效果跟你想象的不一样? 我看你这个效果,应该是在添加“客户联系方式”前,把那个没显示出来的textfield添加进去就跟原图一样了。
------解决思路----------------------
一样啊,你是说哪里不一样?
------解决思路----------------------
恩,改的不够狠,多试几组你就知道问题在哪里了。比如1,3你试过了么。
你先要看清每一个布局是控制那些组件的,观察那些组件的变化。
你看的书没有问题,Java的机制更没有问题