程序功能:
使用Swing控件创建以下用户界面。
三年前没解决的问题,今天终于通过参考SwingSet3中的JButtonDemo界面的源码而解出。用到的核心知识是组合使用FlowLayout和GridLayout实现控件的对齐(alignment)。
参考文章:
吐槽-使用Swing实现的第一个页面,https://blog.csdn.net/hpdlzu80100/article/details/52041032
运行结果:
代码:
1. 测试类
import javax.swing.JFrame;
public class BlankFrame12_8Test {
public static void main(String[] args)
{
BlankFrame12_8_v2 testFrame = new BlankFrame12_8_v2();
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setSize(568, 200);
testFrame.setVisible(true);
}
}
2. 实体类
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
//Java How to Program, Exercise 12.8
//by pandenghuang@163.com
/**
* 使用Swing控件创建用户界面
* 12.8 Create the following GUI.
* You do not have to provide any functionality.
* @author Pandenghuang@163.com
* @Date Jan 10, 2019, 10:30:58 PM
*
*/
public class BlankFrame12_8_v2 extends JFrame{
//声明界面中的基本控件
private final JButton okJButton; //OK
private final JButton cancelJButton; //Cancel
private final JButton helpJButton; //Help
private final JLabel xJLabel; //X
private final JLabel yJLabel; //Y
private final JTextField xJTextField; //X field
private final JTextField yJTextField; //Y field
private final JCheckBox snapToGridJCheckBox; //Snap to Grid checkbox
private final JCheckBox showGridJCheckBox; //Show Grid checkbox
private final JLabel snapToGridJLabel; //Snap to Grid
private final JLabel showGridJLabel; //Snap to Grid
//声明容器控件
private final JPanel leftJPanel;
private final JPanel middleJPanel;
private final JPanel rightJPanel;
private final JPanel leftSubJPanel1;
private final JPanel leftSubJPanel2;
private final JPanel middleSubJPanel1;
private final JPanel middleSubJPanel2;
public BlankFrame12_8_v2 () {
super("Blank GUI for Java How to Program, exercise 12.8");
setLayout(new FlowLayout(FlowLayout.CENTER,30,20));
//初始化基本控件
okJButton = new JButton("OK");
cancelJButton = new JButton("Cancel") ; //Cancel
helpJButton = new JButton("Help"); //Help
xJLabel = new JLabel("X"); //X
yJLabel = new JLabel("Y"); //Y
xJTextField = new JTextField(10); //X field
yJTextField = new JTextField(10); //X field
snapToGridJCheckBox = new JCheckBox(); //Snap to Grid checkbox
showGridJCheckBox = new JCheckBox(); //Show Grid checkbox
snapToGridJLabel = new JLabel("Snap to Grid"); //Snap to Grid
showGridJLabel = new JLabel("Show Grid"); //Snap to Grid
//初始化容器控件
leftJPanel = new JPanel();
middleJPanel = new JPanel();
rightJPanel = new JPanel();
leftSubJPanel1 = new JPanel();
leftSubJPanel2 = new JPanel();
middleSubJPanel1 = new JPanel();
middleSubJPanel2 = new JPanel();
//设置容器控件的布局管理器
leftJPanel.setLayout(new FlowLayout(FlowLayout.CENTER,2,6));
GridLayout gridMiddleJPanel = new GridLayout(2,1);
gridMiddleJPanel.setHgap(1);
gridMiddleJPanel.setVgap(10);
middleJPanel.setLayout(gridMiddleJPanel);
GridLayout gridRightJPanel = new GridLayout(3,1);
gridRightJPanel.setHgap(1);
gridRightJPanel.setVgap(20);
rightJPanel.setLayout(gridRightJPanel);
//向容器中填充基本控件
leftSubJPanel1.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
leftSubJPanel1.add(snapToGridJCheckBox);
leftSubJPanel1.add(snapToGridJLabel);
leftSubJPanel2.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
leftSubJPanel2.add(showGridJCheckBox);
leftSubJPanel2.add(showGridJLabel);
leftJPanel.setLayout(new GridLayout(2,1));
leftJPanel.add(leftSubJPanel1);
leftJPanel.add(leftSubJPanel2);
middleSubJPanel1.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
middleSubJPanel1.add(xJLabel);
middleSubJPanel1.add(xJTextField);
middleSubJPanel2.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
middleSubJPanel2.add(yJLabel);
middleSubJPanel2.add(yJTextField);
GridLayout grid = new GridLayout(2,1);
grid.setVgap(16);
middleJPanel.setLayout(grid);
middleJPanel.add(middleSubJPanel1);
middleJPanel.add(middleSubJPanel2);
rightJPanel.setLayout(gridRightJPanel);
rightJPanel.add(okJButton);
rightJPanel.add(cancelJButton);
rightJPanel.add(helpJButton);
add(leftJPanel);
add(middleJPanel);
add(rightJPanel);
setVisible(true);
}
}