ITEEDU

swing的版面结构

版面管理器

事件处理

鼠标事件

键盘事件处理

窗口与面板的使用

JInternalFrame的使用(窗口与面板)

JTabbedPane,JScroolPane,JScrolBa的使用

标签与按钮的使用与介绍

[复选框、选项按钮、列表方框、下拉式列表的使用与介绍]

表格(Table)的使用与介绍

JTable的事件处理

1文字输入组件的使用与介绍

树(Tree)的使用与介绍

对话框(Option Pane与Dialog)的使用与介绍

菜单与工具栏的使用与介绍

文件选择对话框,颜色选择对话框,分隔线的使用与介绍.

Slider,Timer,Progress的使用

对话框(Option Pane与Dialog)的使用与介绍

11-1:使用JDialog组件: JDialog的类层次结构图:

  java.lang.Object
    --java.awt.Component
     --java.awt.Container
       --java.awt.Window
        --java.awt.JDialog
         --javax.swing.JDialog

如果你为公司设计一套“物品工具借用系统”,在这个系统中,借用物品的员工必须详细填员工编号、借用物品、借用器具、借 用日期、预计归还日期时间、借用原因等等,若没有详细填这此数据,就无法取得准许借用物品的证明文件。因此在设计这套系统 的过程中,你必须查看用户是否已经填妥相关数据,若员工忘记了填写某些重要字段,系统应该给予警示,提醒用户哪些字段必须 填写。这个情况也常发生在网络问卷或网络会员注册系统,用户必须填写相关数据,例如用户若没有填写E-Mail邮件地址,则系统 会提示你应当填写邮件地址,否则系统将不处理用户填写的信息。

为应付这种情况,java提供了JDialog与JOptionPane供我们使用,事实上,JOptionPane是阳春版的JDialog,当你在使用 JOptionPane时,系统会自动产生JDialog组件,并将JOptionPane的内容放入JDialog的ContentPane中,而这些均由系统在背后自动 运行,并不需要由我们介入。使用JOptionPane的好处是此组件已经默认了许多交互方式,你只用设置想要的显示模式,JOptionPane 就能轻易的显示出来,可说相当方便,若这些模式还是无法满足你的需求,你就可以使用JDialog来自行设计你的对话框。 我们先来看如何构造JDialog,JOptionPane将在后半段介绍:

JDialog构造函数:
  • JDialog():建立一个non-modal的对话框,没有title也不属于任何事件窗口组件。
  • JDialog(Dialog owner):建立一个属于Dialog组件的对话框,为non-modal形式,也没有title.
  • JDialog(Dialog owner,Boolean modal):建立一个属于Dialog组件的对话框,可决定modal形式,但没有title.
  • JDialog(Dialog owner,String title):建立一个属于Dialog组件的对话框,为non-modal形式,对话框上有title.
  • JDialog(Dialog owner,String title,Boolean modal):建立一个属于Dialog组件的对话框,可决定modal形式,且对话框上有 title.
  • JDialog(Frame owner):建立一个属于Frame组件的对话框,为non-modal形式,也没有title.
  • JDialog(Frame owner,Boolean modal):建立一个属于Frame组件的对话框,可决定modal形式,但没有title.
  • JDialog(Frame owner,String title):建立一个属于Frame组件的对话框,为non-modal形式,对话框上有title.
  • JDialog(Frame owner,String title,Boolean modal):建立一个属于Frame组件的对话框,可决定modal形式,且对话框上有title.

上面所说的modal是一种对话框操作模式,当modal为true时,代表用户必须结束对话框才能回到原来所属的窗口。当modal为 false时,代表对话框与所属窗口可以互相切换,彼此之间在操作上没有顺序性。

一般而言对话框都会依附在某个窗口上,例如JFrame或JDialog,原因在于对话框通常是一个程序运行的过程中与用户互动的中 间过程,在使用JDialog上跟JFrame非常相似,由上面的JDialog层次结构图中你可以发现,JDialog是继承AWT的Dialog类而来,因 此JDialog为一个Heavyweight组件。要加入组件到JDialog上与JFrame是一样的,你必须先取得JDialog的ContentPane,然后再把组 件加到此ContentPane中,JDialog默认的版面管理器是BorderLayout.

11-1-1:在JFrame上建立JDialog:

我们来看一个JDialog的例子,在这个例子中,用户在主窗口按下"借用物品"的按钮时,会跳出一个让用户填写窗体的JDialog 窗口,用户必须结束此JDialog窗口后才能返回主窗口.

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;

public class DialogDemo implements ActionListener {
	JFrame f = null;

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals("借用物品")) {
			new LendingSystem(f);
		} else if (cmd.equals("离开系统")) {
			System.exit(0);
		}
	}

	public DialogDemo() {
		f = new JFrame("JDialog Example");
		Container contentPane = f.getContentPane();
		JPanel buttonPanel = new JPanel();
		JButton b = new JButton("借用物品");
		b.addActionListener(this);
		buttonPanel.add(b);
		b = new JButton("离开系统");
		b.addActionListener(this);
		buttonPanel.add(b);

		buttonPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(Color.blue, 2), "借用物品系统",
				TitledBorder.CENTER, TitledBorder.TOP));
		contentPane.add(buttonPanel, BorderLayout.CENTER);
		f.pack();
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		SwingUtil.setLookAndFeel();
		new DialogDemo();
	}
}

class LendingSystem implements ActionListener {
	JTextField staffField, objectField, borrowDateField, returnDateField,
			reasonField;
	JDialog dialog;

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals("确定")) {

		} else if (cmd.equals("取消")) {
			dialog.dispose();
		}
	}

	LendingSystem(JFrame f) {
		dialog = new JDialog(f, "借用物品", true);
		GridBagConstraints c;
		int gridx, gridy, gridwidth, gridheight, anchor, fill, ipadx, ipady;
		double weightx, weighty;
		Insets inset;

		GridBagLayout gridbag = new GridBagLayout();
		Container dialogPane = dialog.getContentPane();
		dialogPane.setLayout(gridbag);

		JLabel label = new JLabel("员工编号 : ");
		gridx = 0; // 第0列
		gridy = 0; // 第0行
		gridwidth = 1; // 占一单位宽度
		gridheight = 1; // 占一单位高度
		weightx = 0; // 窗口增大时组件宽度增大比率0
		weighty = 0; // 窗口增大时组件高度增大比率0
		anchor = GridBagConstraints.CENTER; // 容器大于组件size时将组件置于容器中央
		fill = GridBagConstraints.BOTH; // 窗口拉大时会填满水平与垂直空间
		inset = new Insets(0, 0, 0, 0); // 组件间间距
		ipadx = 0; // 组件内水平宽度
		ipady = 0; // 组件内垂直高度
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(label, c);
		dialogPane.add(label);

		label = new JLabel("借用器具 : ");
		gridx = 3;
		gridy = 0;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(label, c);
		dialogPane.add(label);

		label = new JLabel("借用日期: ");
		gridx = 0;
		gridy = 1;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(label, c);
		dialogPane.add(label);

		label = new JLabel("归还日期: ");
		gridx = 3;
		gridy = 1;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(label, c);
		dialogPane.add(label);

		label = new JLabel("借用原因 : ");
		gridx = 0;
		gridy = 2;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(label, c);
		dialogPane.add(label);

		staffField = new JTextField();
		gridx = 1;
		gridy = 0;
		gridwidth = 2;
		gridheight = 1;
		weightx = 1;
		weighty = 0;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(staffField, c);
		dialogPane.add(staffField);

		objectField = new JTextField();
		gridx = 4;
		gridy = 0;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(objectField, c);
		dialogPane.add(objectField);

		borrowDateField = new JTextField();
		gridx = 1;
		gridy = 1;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(borrowDateField, c);
		dialogPane.add(borrowDateField);

		returnDateField = new JTextField();
		gridx = 4;
		gridy = 1;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(returnDateField, c);
		dialogPane.add(returnDateField);

		reasonField = new JTextField();
		gridx = 1;
		gridy = 2;
		gridwidth = 5;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(reasonField, c);
		dialogPane.add(reasonField);

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1, 2));
		JButton b = new JButton("确定");
		panel.add(b);
		b = new JButton("取消");
		b.addActionListener(this);
		panel.add(b);

		gridx = 0;
		gridy = 3;
		gridwidth = 6;
		weightx = 1;
		weighty = 1;
		c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, inset, ipadx, ipady);
		gridbag.setConstraints(panel, c);
		dialogPane.add(panel);

		dialog.setBounds(200, 150, 400, 130);
		dialog.show();
	}
}

class SwingUtil {
	public static final void setLookAndFeel() {
		try {
			Font font = new Font("JFrame", Font.PLAIN, 12);
			Enumeration keys = UIManager.getLookAndFeelDefaults().keys();

			while (keys.hasMoreElements()) {
				Object key = keys.nextElement();
				if (UIManager.get(key) instanceof Font) {
					UIManager.put(key, font);
				}
			}
			AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration",
					"true");
			AlloyTheme theme = new GlassTheme();
			LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
			UIManager.setLookAndFeel(alloyLnF);
		} catch (UnsupportedLookAndFeelException ex) {
			ex.printStackTrace();
		}
		/*
		 * try{ Font font = new Font("JFrame", Font.PLAIN, 12); Enumeration keys
		 * = UIManager.getLookAndFeelDefaults().keys();
		 * 
		 * while (keys.hasMoreElements()) { Object key = keys.nextElement(); if
		 * (UIManager.get(key) instanceof Font) { UIManager.put(key, font); } }
		 * UIManager
		 * .setLookAndFeel("com.stefankrause.xplookandfeel.XPLookAndFeel");
		 * }catch(Exception e){ e.printStackTrace(); }
		 */
	}
}