学以致用——Java源码——使用Swing创建用户界面原型之三(Create GUI based on UI design)

程序功能:

使用Swing控件创建以下用户界面。

 

设计思路:

1. 观察界面原型的结构

先从大处着眼,如,上图整体上可看成由左右两个panel构成(可用FlowLaout布局)。左边的panel又可看成由上、中、下三个子panel构成(可用BorderLayout布局。注意:如果使用GridLayout布局,将会发现上面的子panel(Printer: MyPrinter)明显远离中间的子panel)。依次类推,逐步细化。

2. 声明所有的界面元素(别名:GUI component, widget, control,控件)

3. 在frame的构造函数中初始化所有的界面元素

4. 按照第一步的观察结果,组装控件

向容器中添加控件前,先确定布局管理器(本例中综合使用了FlowLayout,BorderLayout和GridLayout三种布局管理器 )

运行结果:

 

代码:

1. 测试类

import javax.swing.JFrame;


public class BlankFrame12_11Test {
	
	   public static void main(String[] args)
	   { 
	      BlankFrame12_11 testFrame = new BlankFrame12_11(); 
	      testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      testFrame.setSize(500, 220); 
	      testFrame.setVisible(true); 
	   } 

}

2. 实体类

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

//Java How to Program, Exercise 12.8
//by pandenghuang@163.com
/**
 * 使用Swing控件创建用户界面
 * 12.11 Create the following GUI. 
 * @author Pandenghuang@163.com
 * @Date Jan 11, 2019, 11:29:10 AM
 *
 */
public class BlankFrame12_11 extends JFrame{
	
	//声明界面中的基本控件
	private final JButton okJButton; 	
	private final JButton cancelJButton; 
	private final JButton setupJButton; 
	private final JButton helpJButton; 
	private final JLabel printerJLabel; 	
	private final JLabel printerNameJLabel; 	
	private final JCheckBox imageJCheckBox; 	
	private final JCheckBox textJCheckBox; 
	private final JCheckBox codeJCheckBox; 	
	private final JLabel imageJLabel; 	
	private final JLabel textJLabel; 
	private final JLabel codeJLabel; 	
	private final JRadioButton selectionJRadioButton; 
	private final JRadioButton allJRadioButton; 
	private final JRadioButton appletJRadioButton; 
	private final JLabel selectionJLabel; 
	private final JLabel allJLabel; 
	private final JLabel appletJLabel;
	private final JLabel printQualityJLabel;
	private final JComboBox<String> printQualityJComboBox; 
	private final JCheckBox printToFileJCheckBox; 	
	private final JLabel printToFileJLabel; 	
	private static final String[] printQualities = 
	      {"High", "Medium"};
	private final JTextArea leftJTextArea; 
	private final JTextArea middleJTextArea; 
	private final JTextArea rightJTextArea; 

	
	//声明容器控件 
	private final JPanel leftJPanel;
	private final JPanel rightJPanel;
	private final JPanel leftTopJPanel;
	private final JPanel leftMiddleJPanel;
	private final JPanel leftMiddleSubJPanel1;
	private final JPanel leftMiddleSubJPanel1_1;
	private final JPanel leftMiddleSubJPanel1_2;
	private final JPanel leftMiddleSubJPanel1_3;
	private final JPanel leftMiddleSubJPanel2;
	private final JPanel leftMiddleSubJPanel2_1;
	private final JPanel leftMiddleSubJPanel2_2;
	private final JPanel leftMiddleSubJPanel2_3;
	private final JPanel leftBottomJPanel;
	

	public BlankFrame12_11 () {
		super("Blank GUI for Java How to Program, exercise 12.11");
		
		setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
		
		//初始化基本控件
		okJButton = new JButton("OK");
		cancelJButton = new JButton("Cancel") ; 
		setupJButton = new JButton("Setup");
		helpJButton = new JButton("Help") ; 
		printerJLabel = new JLabel("Printer:");
		printerNameJLabel = new JLabel("MyPrinter");
		imageJCheckBox = new JCheckBox(); 
		textJCheckBox = new JCheckBox(); 
		codeJCheckBox = new JCheckBox(); 
		imageJLabel = new JLabel("Image");
		textJLabel = new JLabel("Text");
		codeJLabel = new JLabel("Code");
		selectionJRadioButton = new JRadioButton();
		allJRadioButton = new JRadioButton();
		appletJRadioButton = new JRadioButton();
		selectionJLabel = new JLabel("Selection");
		allJLabel = new JLabel("All");
		appletJLabel = new JLabel("Applet");
		printQualityJLabel = new JLabel();
		printQualityJComboBox = new JComboBox<String>(printQualities);
		printQualityJComboBox.setBackground(Color.WHITE);  
		printToFileJCheckBox = new JCheckBox(); 
		printToFileJLabel = new JLabel("Print to File");
		leftJTextArea = new JTextArea(6,6);
		middleJTextArea = new JTextArea(6,4);
		rightJTextArea = new JTextArea(6,6);
		               
		//初始化容器控件
		leftTopJPanel = new JPanel();
		leftJPanel = new JPanel();
		leftMiddleJPanel = new JPanel();
		leftBottomJPanel = new JPanel();
		leftMiddleSubJPanel1 = new JPanel();
		leftMiddleSubJPanel2 = new JPanel();
		leftMiddleSubJPanel1_1 = new JPanel();
		leftMiddleSubJPanel1_2 = new JPanel();
		leftMiddleSubJPanel1_3 = new JPanel();
		leftMiddleSubJPanel2_1 = new JPanel();
		leftMiddleSubJPanel2_2 = new JPanel();
		leftMiddleSubJPanel2_3 = new JPanel();
		rightJPanel = new JPanel();
		
		//向容器中填充基本控件
		leftTopJPanel.setLayout(new FlowLayout(FlowLayout.LEFT,2,1));
		leftTopJPanel.add(printerJLabel);
		leftTopJPanel.add(printerNameJLabel);
		leftMiddleSubJPanel1_1.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
		leftMiddleSubJPanel1_1.add(imageJCheckBox);
		leftMiddleSubJPanel1_1.add(imageJLabel);
		leftMiddleSubJPanel1_2.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
		leftMiddleSubJPanel1_2.add(textJCheckBox);
		leftMiddleSubJPanel1_2.add(textJLabel);
		leftMiddleSubJPanel1_3.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
		leftMiddleSubJPanel1_3.add(codeJCheckBox);
		leftMiddleSubJPanel1_3.add(codeJLabel);
		leftMiddleSubJPanel1.setLayout(new GridLayout(3,1,1,5));
		leftMiddleSubJPanel1.add(leftMiddleSubJPanel1_1);
		leftMiddleSubJPanel1.add(leftMiddleSubJPanel1_2);
		leftMiddleSubJPanel1.add(leftMiddleSubJPanel1_3);
		
		leftMiddleSubJPanel2_1.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
		leftMiddleSubJPanel2_1.add(selectionJRadioButton);
		leftMiddleSubJPanel2_1.add(selectionJLabel);
		leftMiddleSubJPanel2_2.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
		leftMiddleSubJPanel2_2.add(allJRadioButton);
		leftMiddleSubJPanel2_2.add(allJLabel);
		leftMiddleSubJPanel2_3.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
		leftMiddleSubJPanel2_3.add(appletJRadioButton);
		leftMiddleSubJPanel2_3.add(appletJLabel);
		leftMiddleSubJPanel2.setLayout(new GridLayout(3,1,1,5));
		leftMiddleSubJPanel2.add(leftMiddleSubJPanel2_1);
		leftMiddleSubJPanel2.add(leftMiddleSubJPanel2_2);
		leftMiddleSubJPanel2.add(leftMiddleSubJPanel2_3);
		
		leftMiddleJPanel.setLayout(new FlowLayout(FlowLayout.CENTER,2,5));
		leftMiddleJPanel.add(leftJTextArea);
		leftMiddleJPanel.add(leftMiddleSubJPanel1);
		leftMiddleJPanel.add(middleJTextArea);
		leftMiddleJPanel.add(leftMiddleSubJPanel2);
		leftMiddleJPanel.add(rightJTextArea);
		
		leftBottomJPanel.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
		leftBottomJPanel.add(printQualityJLabel);
		leftBottomJPanel.add(printQualityJComboBox);
		leftBottomJPanel.add(printToFileJCheckBox);
		leftBottomJPanel.add(printToFileJLabel);		
		
		leftJPanel.setLayout(new BorderLayout());
		leftJPanel.add(leftTopJPanel,BorderLayout.NORTH);
		leftJPanel.add(leftMiddleJPanel,BorderLayout.CENTER);
		leftJPanel.add(leftBottomJPanel,BorderLayout.SOUTH);
		
		rightJPanel.setLayout(new GridLayout(4,1,1,10));
		rightJPanel.add(okJButton);
		rightJPanel.add(cancelJButton);
		rightJPanel.add(setupJButton);
		rightJPanel.add(helpJButton);

		add(leftJPanel);
		add(rightJPanel);
		setVisible(true);
		
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值