package com.swing.test1; import javax.swing.JFrame; public class SwingTest1 {//Swing 练习1 static final int WIDTH=300; static final int HEIGHT=200; public static void main(String[] args) { JFrame f=new JFrame("Hello Swing"); f.setSize(WIDTH, HEIGHT); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
3.Swing包包
描 述
com.sum.swing.plaf.motif
用户界面代表类,实现Motif界面样式
com.sum.java.swing.plaf.windows
用户界面代表类,实现Windows界面样式
javax.swing
Swing组件和使用工具
javax.swing.border
Swing轻量级组件的边框
javax.swing.colorchooser
JColorChooser的支持类/接口
javax.swing.event
事件和侦听器类
javax.swing.filechooser
JFileChooser的支持类/接口
javax.swing.pending
未完全实现的Swing组件
javax.swing.plaf
抽象类,定义UI代表的行为
javax.swing.plaf.basic
实现所有标准界面样式公共功能的基类
javax.swing.plaf.metal
用户界面代表类,实现Metal界面样式
javax.swing.table
JTable组件
javax.swing.text
支持文档的显示和编辑
javax.swing.text.html
支持显示和编辑HTML文档
javax.swing.text.html.parser
HTML文档的分析器
javax.swing.text.rtf
支持显示和编辑RTF文件
javax.swing.tree
JTree组件的支持类
javax.swing.undo
支持取消操作
其中,swing包是Swing提供的最大包,它包含将近100个类和25个接口,几乎所有的Swing组件都在swing包中,只有JTableHeader和JTextComponent是例外,它们分别在swing.table和swing.text中。
— swing.border包中定义了事件和事件监听器类,与AWT的event包类似,它们都包括事件类和监听器接口。
— swing.pending包包含了没有完全实现的Swing组件。 — swing.table包中主要包括表格组建(JTable)的支持类。— swing.tree同样是JTree的支持类。
— swing.text、swing.text.html、swing.text.html.parser和swing.text.rtf都是用于显示和编辑文档的包。
Chap3 Java Swing组建基础 1.Swing组件类的层次 Swing组件分从显示效果上分为两种类型:JComponent类和Window类;
JComponent组件主要包括一些不能独立显示的组件(必须依附与其他组件才能显示)。 JPanelJTableJTreeJTextAreaJTextFieldJButton Window组件类主要包括可以独立显示的组件。 JFrameJDialog Swing组件从 功能 上分为三种类型: 顶级组件(顶级容器,可以独立显示) JFrame、JApplet、JDialog、JWindow 中间组件 中间容器类(可以充当容器,但不能独立显示) JPanel、JScrollPane、JSplitPane、JToolBar 特殊中间组件类(在GUI上起特殊作用的中间层,属于中间容器类,但是能起到美化和专业化的作用) JInternalFrame、JLayeredPane、JRootPane等 基本组件(实现人机交互的组件,只能依附于中间组件才能显示) JButton、JComboBox、JList、JMenu、JSlider、JTextField等 2.Window类 一切图形化的东西必须包括在顶级容器内。Swing中主要有三种可以使用的顶级容器: JFrame(用于设计类似于Windows系统中的窗口程序) 内容面板(ContentPane) 基本组件 菜单条 JDialog(用于设计对话框)JApplet(用于设计可以嵌入到网页中的Java程序) 3.添加内容面板到顶级容器 JFrame f=new JFrame("添加内容到顶级容器"); f.setSize(WIDTH,HEIGHT); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); //顶级容器添加内容面板 JPanel contentPane=new JPanel(); f.setContentPane(contentPane); //添加基本组件到内容面板 JButton b=new JButton("确定"); contentPane.add(b); 4.添加菜单到顶级容器 JFrame f=new JFrame("添加内容到顶级容器"); f.setSize(WIDTH,HEIGHT); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); //顶级容器添加内容面板 JPanel contentPane=new JPanel(); f.setContentPane(contentPane); //添加基本组件到内容面板 JButton b=new JButton("确定"); contentPane.add(b); //添加菜单到顶级面板 JMenuBar menubar=new JMenuBar(); f.setJMenuBar(menubar); JMenu menu1=new JMenu("文件"); JMenu menu2=new JMenu("编辑"); JMenu menu3=new JMenu("视图"); menubar.add(menu1); menubar.add(menu2); menubar.add(menu3); JMenuItem item1=new JMenuItem("打开"); JMenuItem item2=new JMenuItem("保存"); JMenuItem item3=new JMenuItem("退出"); menu1.add(item1); menu1.add(item2); menu1.addSeparator(); menu1.add(item3); f.setVisible(true);//得刷新一次 5.JComponent类(所有轻量级组件的父类) JComponent的九大特性: Tooltips工具提示功能(鼠标停在组件上,显示提示),通过setToolTipText实现绘画和边框(setBorder,BorderFactory)可插入的观感器(定制自己的桌面,更换新的颜色方案,包括默认、Motif和Windows的L&F)自定义属性layout支持无障碍拖拽支持双缓冲键绑定Chap4 Label&Button
一、标签的使用
常用构造:JLabel label=new JLabel("label");常用方法:label.setText();label.getText();
JLabel label1=new JLabel("label1"); JLabel label2=new JLabel(); String name=label1.getText(); label2.setText(name);
二、按钮的使用
1.普通按钮(JButton):
JButton btn=new JButton("普通按钮"); btn.setEnabled(true); btn.setVisible(true); pane.add(btn);
2.单选按钮(JRadioButton):
//添加单选按钮 JRadioButton rbtn1=new JRadioButton("Ars",true); JRadioButton rbtn2=new JRadioButton("Mu"); JRadioButton rbtn3=new JRadioButton("Liv"); ButtonGroup bg=new ButtonGroup(); bg.add(rbtn1); bg.add(rbtn2); bg.add(rbtn3); pane.add(rbtn1); pane.add(rbtn2); pane.add(rbtn3);
3.复选框(JCheckbox):
//添加复选框 JCheckBox cb1=new JCheckBox("tennis");
chap 05 布局管理器
一、布局管理器概述:
布局管理器是相对于contentPanel而言的。
BorderLayout:东、南、西、北、中;FlowLayout:按照加入的先后顺序排列,行满换行;从左到右,居中排列;GridLayout:将空间划分为网状区域;GriBagLayout:网状划分,功能较GridLayout复杂;CardLayout:将组件当成卡片,每一只能显示一个。BoxLayout:通过允许在容器中水平或垂直的方式安排多个组件;SpringLayout:通过定义组件边沿的关系来实现布局;GroupLayout:指定一个窗体上组件彼此之间的关系。二、布局管理器的种类:
1.BorderLayout:
JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); JPanel contentPane=new JPanel(); jf.setContentPane(contentPane); JButton b1=new JButton("生活"); JButton b2=new JButton("工作"); JButton b3=new JButton("睡觉"); JButton b4=new JButton("购物"); JButton b5=new JButton("饮食"); BorderLayout lay=new BorderLayout();//创建一个布局管理器对象,将中间容器设置为此布局管理 jf.setLayout(lay); contentPane.add(b1,"North");//将五个普通按钮组件分别按照东、南、西、北、中五个方位添加到中间容器中 contentPane.add(b2,"South"); contentPane.add(b3,"East"); contentPane.add(b4,"West"); contentPane.add(b5,"Center");
2.FlowLayout:
JButton b1=new JButton("港币"); JButton b2=new JButton("人民币"); JButton b3=new JButton("美元"); JButton b4=new JButton("欧元"); JButton b5=new JButton("英镑"); contentPane.setLayout(new FlowLayout());//将中间容器的布局管理器设置为FlowLayout contentPane.add(b1); //将五个按钮分别按照FlowLayout布局管理器方式添加到中间容器中 contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); contentPane.add(b5); jf.pack(); //自动调整Frame的大小,使得所有的控件都能显示出来
3.GridLayou:
JButton b1=new JButton("港币"); JButton b2=new JButton("人民币"); JButton b3=new JButton("美元"); JButton b4=new JButton("欧元"); JButton b5=new JButton("英镑"); JButton b6=new JButton("主板"); JButton b7=new JButton("内存"); JButton b8=new JButton("硬盘"); JButton b9=new JButton("显示器");
GridLayout gird=new GridLayout(3,3); //创建一个 GridLayout布局管理器对象,将之行数设为3,列数设为3,并且将之作为中间容器的布局管理器 contentPane.setLayout(gird);
contentPane.add(b1); //将九个普通按钮组件一一添加到中间容器中 contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); contentPane.add(b5); contentPane.add(b6); contentPane.add(b7); contentPane.add(b8); contentPane.add(b9); jf.pack();
4.GridBagLayut:
GridBagLayout按照开发人员自己的思路来排列控件位置,二GridLayout布局管理器是根据系统的安排来布局。
使用步骤:
创建GridBagLayout对象;将容器设置成为此对象的布局管理器;创建约束GridBagConstraints对象; fill参数用于档组件不能填充单元格时,在各个方向上是否填充;anchor参数用于当一个组件大于分配给它的单元格时约束缩小该组件;weightx是设置放大时的增量数字,为0是放大不变;100时放大完全填充;gridx定义组件左上角的行与列的位置;gridwidth定义组件所占用的列数 创建各个相应的组件;添加各个组件与约束到网格组布局管理器中。public class MyPanel extends JPanel { static final int WIDTH=300; static final int HEIGHT=150; JFrame loginFrame; MyPanel() { loginFrame=new JFrame("Info Goverment"); loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridBagLayout lay=new GridBagLayout();//创建网格组布局方式对象 setLayout(lay); loginFrame.add(this, BorderLayout.WEST); loginFrame.setSize(WIDTH,HEIGHT); Toolkit kit=Toolkit.getDefaultToolkit();//设置顶层容器框架为居中 Dimension screenSize=kit.getScreenSize(); int width=screenSize.width; int height=screenSize.height; int x=(width-WIDTH)/2; int y=(height-HEIGHT)/2; loginFrame.setLocation(x,y); JButton ok=new JButton("确认"); JButton cancel=new JButton("放弃"); JLabel title=new JLabel("布局管理器测试窗口"); JLabel name=new JLabel("用户名"); JLabel password=new JLabel("密 码"); final JTextField nameinput=new JTextField(15); final JTextField passwordinput=new JTextField(15); GridBagConstraints constraints=new GridBagConstraints(); constraints.fill=GridBagConstraints.NONE; constraints.anchor=GridBagConstraints.EAST; constraints.weightx=3; constraints.weighty=4; add(title,constraints,0,0,4,1); //使用网格组布局添加控件 add(name,constraints,0,1,1,1); add(password,constraints,0,2,1,1); add(nameinput,constraints,2,1,1,1); add(passwordinput,constraints,2,2,1,1); add(ok,constraints,0,3,1,1); add(cancel,constraints,2,3,1,1); loginFrame.setResizable(false); loginFrame.setVisible(true); } public void add(Component c, GridBagConstraints constraints, int x, int y, int w, int h) { constraints.gridx=x; constraints.gridy=y; constraints.gridwidth=w; constraints.gridheight=h; add(c,constraints); } }
5.CardLayout:
public class MyFrame extends JFrame { private JPanel pane = null; // 主要的JPanel,该JPanel的布局管理将被设置成CardLayout private JPanel p = null; // 放按钮的JPanel private CardLayout card = null; // CardLayout布局管理器 private JButton button_1 = null; // 上一步 private JButton button_2 = null; // 下一步 private JButton b_1 = null, b_2 = null, b_3 = null; // 三个可直接翻转到JPanel组件的按钮 private JPanel p_1 = null, p_2 = null, p_3 = null; // 要切换的三个JPanel MyFrame() { super("CardLayout Test"); try { // 将LookAndFeel设置成Windows样式 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { ex.printStackTrace(); } card = new CardLayout(5, 5); //创建一个具有指定的水平和垂直间隙的新卡片布局 pane = new JPanel(card); // JPanel的布局管理将被设置成CardLayout p = new JPanel(); // 构造放按钮的JPanel button_1 = new JButton("< 上一步"); button_2 = new JButton("下一步 >"); b_1 = new JButton("1"); b_2 = new JButton("2"); b_3 = new JButton("3"); b_1.setMargin(new Insets(2,2,2,2)); b_2.setMargin(new Insets(2,2,2,2)); b_3.setMargin(new Insets(2,2,2,2)); p.add(button_1); p.add(b_1); p.add(b_2); p.add(b_3); p.add(button_2); p_1 = new JPanel(); p_2 = new JPanel(); p_3 = new JPanel(); p_1.setBackground(Color.RED); p_2.setBackground(Color.BLUE); p_3.setBackground(Color.GREEN); p_1.add(new JLabel("JPanel_1")); p_2.add(new JLabel("JPanel_2")); p_3.add(new JLabel("JPanel_3")); pane.add(p_1, "p1"); pane.add(p_2, "p2"); pane.add(p_3, "p3"); //下面是翻转到卡片布局的某个组件的动作事件处理,当单击某个普通按钮组件,就会触发出现下一个组件 button_1.addActionListener(new ActionListener() { /// 上一步的按钮动作 public void actionPerformed(ActionEvent e) { card.previous(pane); } }); button_2.addActionListener(new ActionListener() { // 下一步的按钮动作 public void actionPerformed(ActionEvent e) { card.next(pane); } }); b_1.addActionListener(new ActionListener() { // 直接翻转到p_1 public void actionPerformed(ActionEvent e) { card.show(pane, "p1"); } }); b_2.addActionListener(new ActionListener() { // 直接翻转到p_2 public void actionPerformed(ActionEvent e) { card.show(pane, "p2"); } }); b_3.addActionListener(new ActionListener() { // 直接翻转到p_3 public void actionPerformed(ActionEvent e) { card.show(pane, "p3"); } }); this.getContentPane().add(pane); this.getContentPane().add(p, BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 200); this.setVisible(true); } } 6.BoxLayout:
箱式布局提供三种填充物:支柱、固定去、弹簧;
import java.awt.BorderLayout; import java.awt.Container;
import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField;
public class BoxLayout { public static void main(String[] args) { BoxLayoutFrame frame1=new BoxLayoutFrame(); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.show(); } } class BoxLayoutFrame extends JFrame { private static final int WIDTH=300; private static final int HEIGHT=200; public BoxLayoutFrame() {
setTitle("测试箱式布局管理器");//设置顶层容器名称、大小 setSize(WIDTH,HEIGHT); Container con=getContentPane();//创建一个中间容器 JLabel label1=new JLabel(" 姓名:");//创建标签组件、文本框组件 JTextField textField1=new JTextField(10); textField1.setMaximumSize(textField1.getPreferredSize()); Box hbox1=Box.createHorizontalBox();//创建一个水平箱子,箱式布局提供三种填充物:支柱、固定去、弹簧 hbox1.add(label1); //在水平箱子上添加一个标签组件,并且创建一个不可见的、20个单位的组件。在这之后再添加一个文本框组件 hbox1.add(Box.createHorizontalStrut(20)); hbox1.add(textField1); JLabel label2=new JLabel(" 密码:");//创建标签组件、文本框组件 JTextField textField2=new JTextField(10); textField2.setMaximumSize(textField2.getPreferredSize()); Box hbox2=Box.createHorizontalBox();//创建一个水平箱子 hbox2.add(label2); //在水平箱子上添加一个标签组件,并且创建一个不可见的、20个单位的组件。在这之后再添加一个文本框组件 hbox2.add(Box.createHorizontalStrut(20)); hbox2.add(textField2); JButton button1=new JButton("确定");//创建两个普通按钮组件,并且创建一个水平箱子,将两个按钮添加到箱子中 JButton button2=new JButton("取消"); Box hbox3=Box.createHorizontalBox(); hbox3.add(button1); hbox3.add(button2); Box vbox=Box.createVerticalBox();//创建一个垂直箱子,这个箱子将两个水平箱子添加到其中,创建一个横向 glue 组件。 vbox.add(hbox1); vbox.add(hbox2); vbox.add(Box.createVerticalGlue()); vbox.add(hbox3); con.add(vbox,BorderLayout.CENTER); // 将垂直箱子添加到BorderLayout布局管理器中的中间位置 } }
7.SpringLayout
//这段代码主要是为读者展示如何使用SpringLayout布局管理器为组件进行布局 import javax.swing.*; import java.awt.*; //产生不同的箱子布局管理器对象,每个对象放置不同的控件 public class test10 { static final int WIDTH=300; static final int HEIGHT=200; public static void main(String[] args) { JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); JPanel contentPane=new JPanel(); jf.setContentPane(contentPane); JButton b1=new JButton("测试程序模块1");//创建了两个普通按钮组件、一个标签组件,将它们添加到中间容器中 JButton b2=new JButton("测试程序模块2"); JLabel l=new JLabel("测试程序"); contentPane.add(l); contentPane.add(b2); contentPane.add(b1); // 创建一个 SpringLayout布局管理器,并且将之作为中间容器的布局方式 SpringLayout lay=new SpringLayout(); contentPane.setLayout(lay); //针对每个组件设置其与边界的距离 lay.putConstraint(SpringLayout.NORTH,l, 5,SpringLayout.NORTH,contentPane); lay.putConstraint(SpringLayout.WEST,l, 85,SpringLayout.WEST,contentPane); lay.putConstraint(SpringLayout.EAST,l, 85,SpringLayout.EAST,contentPane); lay.putConstraint(SpringLayout.NORTH,b1, 55,SpringLayout.NORTH,contentPane); lay.putConstraint(SpringLayout.WEST,b1, 5,SpringLayout.WEST,contentPane); lay.putConstraint(SpringLayout.EAST,b1, 25,SpringLayout.EAST,contentPane); lay.putConstraint(SpringLayout.NORTH,b2, 105,SpringLayout.NORTH,contentPane); lay.putConstraint(SpringLayout.WEST,b2, 5,SpringLayout.WEST,contentPane); lay.putConstraint(SpringLayout.EAST,b2, 25,SpringLayout.EAST,contentPane); } }
8.GroupLayout:
//这段代码主要是为读者展示如何使用GroupLayout布局管理器进行布局 import java.awt.Container; import java.awt.HeadlessException; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class test11 extends JFrame { private static final long serialVersionUID = 1L; public test11() throws HeadlessException { Container c = getContentPane();//创建一个中间容器,并且创建一个GroupLayout布局管理器对象 GroupLayout layout = new GroupLayout(c); JButton b1 = new JButton("按钮 1");//创建两个普通按钮组件、文本框组件 JButton b2 = new JButton("按钮 2"); JTextField text = new JTextField("文本"); GroupLayout.SequentialGroup hsg = layout.createSequentialGroup();//创建一个hsg组,将两个按钮一个一个的添加到组里面 hsg.addComponent(b1); hsg.addComponent(b2); GroupLayout.ParallelGroup hpg = layout.createParallelGroup(GroupLayout.Alignment.CENTER); //创建一个hpg组,将文本框组件和上面的那个组添加到其中,并且居中排列 hpg.addComponent(text).addGroup(hsg); layout.setHorizontalGroup(hpg); //沿水平线来确定hpg组中两个按钮组件的位置 GroupLayout.ParallelGroup vpg = layout.createParallelGroup();//创建一个vpg组,按照水平线来排列两个按钮组件的位置 vpg.addComponent(b1); vpg.addComponent(b2); GroupLayout.SequentialGroup vsg = layout.createSequentialGroup();// 将文本框组件和前面的容纳两个按钮组件的组同时添加到vsg组中 vsg.addComponent(text).addGroup(vpg); layout.setVerticalGroup(vsg); //沿垂直线来确定vsg中vpg和文本框组件的位置 this.setLayout(layout); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } public static void main(String[] args) { test11 demo = new test11(); demo.setVisible(true); } }
heckBox("badminton",true); pane.add(cb1); pane.add(cb2); pane.add(cb3);
chap 05 布局管理器
一、布局管理器概述:
布局管理器是相对于contentPanel而言的。
BorderLayout:东、南、西、北、中;FlowLayout:按照加入的先后顺序排列,行满换行;从左到右,居中排列;GridLayout:将空间划分为网状区域;GriBagLayout:网状划分,功能较GridLayout复杂;CardLayout:将组件当成卡片,每一只能显示一个。BoxLayout:通过允许在容器中水平或垂直的方式安排多个组件;SpringLayout:通过定义组件边沿的关系来实现布局;GroupLayout:指定一个窗体上组件彼此之间的关系。二、布局管理器的种类:
1.BorderLayout:
JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); JPanel contentPane=new JPanel(); jf.setContentPane(contentPane); JButton b1=new JButton("生活"); JButton b2=new JButton("工作"); JButton b3=new JButton("睡觉"); JButton b4=new JButton("购物"); JButton b5=new JButton("饮食"); BorderLayout lay=new BorderLayout();//创建一个布局管理器对象,将中间容器设置为此布局管理 jf.setLayout(lay); contentPane.add(b1,"North");//将五个普通按钮组件分别按照东、南、西、北、中五个方位添加到中间容器中 contentPane.add(b2,"South"); contentPane.add(b3,"East"); contentPane.add(b4,"West"); contentPane.add(b5,"Center");
2.FlowLayout:
JButton b1=new JButton("港币"); JButton b2=new JButton("人民币"); JButton b3=new JButton("美元"); JButton b4=new JButton("欧元"); JButton b5=new JButton("英镑"); contentPane.setLayout(new FlowLayout());//将中间容器的布局管理器设置为FlowLayout contentPane.add(b1); //将五个按钮分别按照FlowLayout布局管理器方式添加到中间容器中 contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); contentPane.add(b5); jf.pack(); //自动调整Frame的大小,使得所有的控件都能显示出来
3.GridLayou:
JButton b1=new JButton("港币"); JButton b2=new JButton("人民币"); JButton b3=new JButton("美元"); JButton b4=new JButton("欧元"); JButton b5=new JButton("英镑"); JButton b6=new JButton("主板"); JButton b7=new JButton("内存"); JButton b8=new JButton("硬盘"); JButton b9=new JButton("显示器");
GridLayout gird=new GridLayout(3,3); //创建一个 GridLayout布局管理器对象,将之行数设为3,列数设为3,并且将之作为中间容器的布局管理器 contentPane.setLayout(gird);
contentPane.add(b1); //将九个普通按钮组件一一添加到中间容器中 contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); contentPane.add(b5); contentPane.add(b6); contentPane.add(b7); contentPane.add(b8); contentPane.add(b9); jf.pack();
4.GridBagLayut:
GridBagLayout按照开发人员自己的思路来排列控件位置,二GridLayout布局管理器是根据系统的安排来布局。
使用步骤:
创建GridBagLayout对象;将容器设置成为此对象的布局管理器;创建约束GridBagConstraints对象; fill参数用于档组件不能填充单元格时,在各个方向上是否填充;anchor参数用于当一个组件大于分配给它的单元格时约束缩小该组件;weightx是设置放大时的增量数字,为0是放大不变;100时放大完全填充;gridx定义组件左上角的行与列的位置;gridwidth定义组件所占用的列数 创建各个相应的组件;添加各个组件与约束到网格组布局管理器中。public class MyPanel extends JPanel { static final int WIDTH=300; static final int HEIGHT=150; JFrame loginFrame; MyPanel() { loginFrame=new JFrame("Info Goverment"); loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridBagLayout lay=new GridBagLayout();//创建网格组布局方式对象 setLayout(lay); loginFrame.add(this, BorderLayout.WEST); loginFrame.setSize(WIDTH,HEIGHT); Toolkit kit=Toolkit.getDefaultToolkit();//设置顶层容器框架为居中 Dimension screenSize=kit.getScreenSize(); int width=screenSize.width; int height=screenSize.height; int x=(width-WIDTH)/2; int y=(height-HEIGHT)/2; loginFrame.setLocation(x,y); JButton ok=new JButton("确认"); JButton cancel=new JButton("放弃"); JLabel title=new JLabel("布局管理器测试窗口"); JLabel name=new JLabel("用户名"); JLabel password=new JLabel("密 码"); final JTextField nameinput=new JTextField(15); final JTextField passwordinput=new JTextField(15); GridBagConstraints constraints=new GridBagConstraints(); constraints.fill=GridBagConstraints.NONE; constraints.anchor=GridBagConstraints.EAST; constraints.weightx=3; constraints.weighty=4; add(title,constraints,0,0,4,1); //使用网格组布局添加控件 add(name,constraints,0,1,1,1); add(password,constraints,0,2,1,1); add(nameinput,constraints,2,1,1,1); add(passwordinput,constraints,2,2,1,1); add(ok,constraints,0,3,1,1); add(cancel,constraints,2,3,1,1); loginFrame.setResizable(false); loginFrame.setVisible(true); } public void add(Component c, GridBagConstraints constraints, int x, int y, int w, int h) { constraints.gridx=x; constraints.gridy=y; constraints.gridwidth=w; constraints.gridheight=h; add(c,constraints); } }
5.CardLayout:
public class MyFrame extends JFrame { private JPanel pane = null; // 主要的JPanel,该JPanel的布局管理将被设置成CardLayout private JPanel p = null; // 放按钮的JPanel private CardLayout card = null; // CardLayout布局管理器 private JButton button_1 = null; // 上一步 private JButton button_2 = null; // 下一步 private JButton b_1 = null, b_2 = null, b_3 = null; // 三个可直接翻转到JPanel组件的按钮 private JPanel p_1 = null, p_2 = null, p_3 = null; // 要切换的三个JPanel MyFrame() { super("CardLayout Test"); try { // 将LookAndFeel设置成Windows样式 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { ex.printStackTrace(); } card = new CardLayout(5, 5); //创建一个具有指定的水平和垂直间隙的新卡片布局 pane = new JPanel(card); // JPanel的布局管理将被设置成CardLayout p = new JPanel(); // 构造放按钮的JPanel button_1 = new JButton("< 上一步"); button_2 = new JButton("下一步 >"); b_1 = new JButton("1"); b_2 = new JButton("2"); b_3 = new JButton("3"); b_1.setMargin(new Insets(2,2,2,2)); b_2.setMargin(new Insets(2,2,2,2)); b_3.setMargin(new Insets(2,2,2,2)); p.add(button_1); p.add(b_1); p.add(b_2); p.add(b_3); p.add(button_2); p_1 = new JPanel(); p_2 = new JPanel(); p_3 = new JPanel(); p_1.setBackground(Color.RED); p_2.setBackground(Color.BLUE); p_3.setBackground(Color.GREEN); p_1.add(new JLabel("JPanel_1")); p_2.add(new JLabel("JPanel_2")); p_3.add(new JLabel("JPanel_3")); pane.add(p_1, "p1"); pane.add(p_2, "p2"); pane.add(p_3, "p3"); //下面是翻转到卡片布局的某个组件的动作事件处理,当单击某个普通按钮组件,就会触发出现下一个组件 button_1.addActionListener(new ActionListener() { /// 上一步的按钮动作 public void actionPerformed(ActionEvent e) { card.previous(pane); } }); button_2.addActionListener(new ActionListener() { // 下一步的按钮动作 public void actionPerformed(ActionEvent e) { card.next(pane); } }); b_1.addActionListener(new ActionListener() { // 直接翻转到p_1 public void actionPerformed(ActionEvent e) { card.show(pane, "p1"); } }); b_2.addActionListener(new ActionListener() { // 直接翻转到p_2 public void actionPerformed(ActionEvent e) { card.show(pane, "p2"); } }); b_3.addActionListener(new ActionListener() { // 直接翻转到p_3 public void actionPerformed(ActionEvent e) { card.show(pane, "p3"); } }); this.getContentPane().add(pane); this.getContentPane().add(p, BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 200); this.setVisible(true); } } 6.BoxLayout:
箱式布局提供三种填充物:支柱、固定去、弹簧;
import java.awt.BorderLayout; import java.awt.Container;
import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField;
public class BoxLayout { public static void main(String[] args) { BoxLayoutFrame frame1=new BoxLayoutFrame(); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.show(); } } class BoxLayoutFrame extends JFrame { private static final int WIDTH=300; private static final int HEIGHT=200; public BoxLayoutFrame() {
setTitle("测试箱式布局管理器");//设置顶层容器名称、大小 setSize(WIDTH,HEIGHT); Container con=getContentPane();//创建一个中间容器 JLabel label1=new JLabel(" 姓名:");//创建标签组件、文本框组件 JTextField textField1=new JTextField(10); textField1.setMaximumSize(textField1.getPreferredSize()); Box hbox1=Box.createHorizontalBox();//创建一个水平箱子,箱式布局提供三种填充物:支柱、固定去、弹簧 hbox1.add(label1); //在水平箱子上添加一个标签组件,并且创建一个不可见的、20个单位的组件。在这之后再添加一个文本框组件 hbox1.add(Box.createHorizontalStrut(20)); hbox1.add(textField1); JLabel label2=new JLabel(" 密码:");//创建标签组件、文本框组件 JTextField textField2=new JTextField(10); textField2.setMaximumSize(textField2.getPreferredSize()); Box hbox2=Box.createHorizontalBox();//创建一个水平箱子 hbox2.add(label2); //在水平箱子上添加一个标签组件,并且创建一个不可见的、20个单位的组件。在这之后再添加一个文本框组件 hbox2.add(Box.createHorizontalStrut(20)); hbox2.add(textField2); JButton button1=new JButton("确定");//创建两个普通按钮组件,并且创建一个水平箱子,将两个按钮添加到箱子中 JButton button2=new JButton("取消"); Box hbox3=Box.createHorizontalBox(); hbox3.add(button1); hbox3.add(button2); Box vbox=Box.createVerticalBox();//创建一个垂直箱子,这个箱子将两个水平箱子添加到其中,创建一个横向 glue 组件。 vbox.add(hbox1); vbox.add(hbox2); vbox.add(Box.createVerticalGlue()); vbox.add(hbox3); con.add(vbox,BorderLayout.CENTER); // 将垂直箱子添加到BorderLayout布局管理器中的中间位置 } }
7.SpringLayout
//这段代码主要是为读者展示如何使用SpringLayout布局管理器为组件进行布局 import javax.swing.*; import java.awt.*; //产生不同的箱子布局管理器对象,每个对象放置不同的控件 public class test10 { static final int WIDTH=300; static final int HEIGHT=200; public static void main(String[] args) { JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); JPanel contentPane=new JPanel(); jf.setContentPane(contentPane); JButton b1=new JButton("测试程序模块1");//创建了两个普通按钮组件、一个标签组件,将它们添加到中间容器中 JButton b2=new JButton("测试程序模块2"); JLabel l=new JLabel("测试程序"); contentPane.add(l); contentPane.add(b2); contentPane.add(b1); // 创建一个 SpringLayout布局管理器,并且将之作为中间容器的布局方式 SpringLayout lay=new SpringLayout(); contentPane.setLayout(lay); //针对每个组件设置其与边界的距离 lay.putConstraint(SpringLayout.NORTH,l, 5,SpringLayout.NORTH,contentPane); lay.putConstraint(SpringLayout.WEST,l, 85,SpringLayout.WEST,contentPane); lay.putConstraint(SpringLayout.EAST,l, 85,SpringLayout.EAST,contentPane); lay.putConstraint(SpringLayout.NORTH,b1, 55,SpringLayout.NORTH,contentPane); lay.putConstraint(SpringLayout.WEST,b1, 5,SpringLayout.WEST,contentPane); lay.putConstraint(SpringLayout.EAST,b1, 25,SpringLayout.EAST,contentPane); lay.putConstraint(SpringLayout.NORTH,b2, 105,SpringLayout.NORTH,contentPane); lay.putConstraint(SpringLayout.WEST,b2, 5,SpringLayout.WEST,contentPane); lay.putConstraint(SpringLayout.EAST,b2, 25,SpringLayout.EAST,contentPane); } }
8.GroupLayout:
//这段代码主要是为读者展示如何使用GroupLayout布局管理器进行布局 import java.awt.Container; import java.awt.HeadlessException; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class test11 extends JFrame { private static final long serialVersionUID = 1L; public test11() throws HeadlessException { Container c = getContentPane();//创建一个中间容器,并且创建一个GroupLayout布局管理器对象 GroupLayout layout = new GroupLayout(c); JButton b1 = new JButton("按钮 1");//创建两个普通按钮组件、文本框组件 JButton b2 = new JButton("按钮 2"); JTextField text = new JTextField("文本"); GroupLayout.SequentialGroup hsg = layout.createSequentialGroup();//创建一个hsg组,将两个按钮一个一个的添加到组里面 hsg.addComponent(b1); hsg.addComponent(b2); GroupLayout.ParallelGroup hpg = layout.createParallelGroup(GroupLayout.Alignment.CENTER); //创建一个hpg组,将文本框组件和上面的那个组添加到其中,并且居中排列 hpg.addComponent(text).addGroup(hsg); layout.setHorizontalGroup(hpg); //沿水平线来确定hpg组中两个按钮组件的位置 GroupLayout.ParallelGroup vpg = layout.createParallelGroup();//创建一个vpg组,按照水平线来排列两个按钮组件的位置 vpg.addComponent(b1); vpg.addComponent(b2); GroupLayout.SequentialGroup vsg = layout.createSequentialGroup();// 将文本框组件和前面的容纳两个按钮组件的组同时添加到vsg组中 vsg.addComponent(text).addGroup(vpg); layout.setVerticalGroup(vsg); //沿垂直线来确定vsg中vpg和文本框组件的位置 this.setLayout(layout); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } public static void main(String[] args) { test11 demo = new test11(); demo.setVisible(true); } }
chap 06 如何使用面板组件
1.JPanel:
JPanel可以与顶层窗口想联系,也可以放置在与顶层窗口联系的面板中。使用最为频繁。
2.JScrollPane:
JScrollPanel类是一个带滚动的容器类。可以用于显示文本、表格等内容。
static final int WIDTH=300; static final int HEIGHT=150; public static void main(String[] args) { JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea ta=new JTextArea("我们是某某软件公司的骨干开发人员,我们会竭诚为您服务!!!");//创建一个文本域组件和一个滚动条面板,并且将滚动条面板添加到顶层容器内 JScrollPane sp=new JScrollPane(ta); jf.setContentPane(sp); jf.setVisible(true); }
3.JSplitPane:
主要用于将不同的组件分隔开来。
public static void main (String[] args) { JButton b1= new JButton ("确定");//创建两个普通按钮组件 JButton b2 = new JButton ("取消"); JSplitPane splitPane = new JSplitPane ();//创建一个分隔容器类 splitPane.setOneTouchExpandable (true); //让分割线显示出箭头 splitPane.setContinuousLayout (true); //当用户操作分割线箭头时,系统会重绘图形 splitPane.setPreferredSize (new Dimension (100,200)); splitPane.setOrientation (JSplitPane.HORIZONTAL_SPLIT); //设置分割线为水平分割线 splitPane.setLeftComponent (b1); //将b1放到分割线左边,将b2放到分割线右边 splitPane.setRightComponent (b2); splitPane.setDividerSize (3); //设置分割线大小为3个单位 splitPane.setDividerLocation(50); //设置分割线的位置位于中间 JFrame frame = new JFrame ("测试窗口"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setVisible (true); frame.setContentPane (splitPane); frame.pack (); }
4.JTabbedPane:
JTabbedPane主要用于创建选项卡容器。
public static void main(String[] args) { try {//显示外观风格 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e){} JFrame frame = new JFrame ("资金状况"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setVisible (true);//默认为false JTabbedPane tp=new JTabbedPane();//创建一个选项卡容器,将之添加到顶层容器内 frame.setContentPane(tp); JPanel panel1 = new JPanel (); JPanel panel2 = new JPanel (); JPanel panel3 = new JPanel (); JPanel panel4 = new JPanel (); JPanel panel5 = new JPanel (); tp.addTab("panel1", panel1); //添加选项卡容器,并且设置其中每个选项卡的标签以及其是否可启用 tp.setEnabledAt(0,true); tp.setTitleAt(0,"个人收入状况"); tp.addTab ("panel2", panel2); tp.setEnabledAt (1, true); tp.setTitleAt (1,"工资"); tp.addTab ("panel3", panel3); tp.setEnabledAt (2, true); tp.setTitleAt (2,"奖金"); tp.addTab ("panel4", panel4); tp.setEnabledAt(0,true); tp.setTitleAt(3,"津贴"); tp.addTab ("panel5", panel5); tp.setEnabledAt(4,true); tp.setTitleAt(4,"社保"); ///设置其大小以及其选项卡的位置方向 tp.setPreferredSize (new Dimension (500,200)); tp.setTabPlacement (JTabbedPane.TOP); ///设置选项卡在容器内的显示形式 tp.setTabLayoutPolicy (JTabbedPane.SCROLL_TAB_LAYOUT); frame.pack();
//创建八个标签组件,将五个中间容器设置为流布局,并且将标签组件分别放入到其中 JLabel l1=new JLabel("工资状况"); JLabel l2=new JLabel("3000元/月"); JLabel l3=new JLabel("奖金状况"); JLabel l4=new JLabel("1500元/月"); JLabel l5=new JLabel("津贴状况"); JLabel l6=new JLabel("500元/月"); JLabel l7=new JLabel("社保状况"); JLabel l8=new JLabel("200元/月"); panel2.setLayout(new FlowLayout()); panel3.setLayout(new FlowLayout()); panel4.setLayout(new FlowLayout()); panel5.setLayout(new FlowLayout()); panel2.add(l1); panel2.add(l2); panel3.add(l3); panel3.add(l4); panel4.add(l5); panel4.add(l6); panel5.add(l7); panel5.add(l8); frame.pack(); }
5.JInternalFrame:
JInternalFrame的使用与JFrame基本相同,唯一的不同就是JInternalFrame是中间容器类,不能单独出现,必须依靠最上层组件。
static final int WIDTH=300; static final int HEIGHT=150; public static void main(String[] args) { JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); JPanel contentPane=new JPanel();//创建一个中间容器,并且将之添加到顶层容器内,将之设置为流布局。 jf.setContentPane(contentPane); contentPane.setLayout(new FlowLayout()); JDesktopPane dp=new JDesktopPane();//创建一个虚拟桌面容器,将dp添加到以上创建的中间容器中 dp.setLayout(new FlowLayout()); contentPane.add(dp); JInternalFrame jif=new JInternalFrame("第一个窗口",true,true,true); //创建两个JIntenaFrame容器,并且创建六个标签组件,分别将它们添加到两个JInternaFrame容器内 JInternalFrame jif1=new JInternalFrame("第二个窗口",true,true,true); JLabel l1=new JLabel("这是我第一个窗口"); JLabel l2=new JLabel("这也是你第一个窗口"); JLabel l3=new JLabel("这同时是他第一个窗口"); JLabel l4=new JLabel("这是我第二个窗口"); JLabel l5=new JLabel("这也是你第二个窗口"); JLabel l6=new JLabel("这同时是他第二个窗口"); jif.setLayout(new FlowLayout()); jif1.setLayout(new FlowLayout()); jif.add(l1); jif.add(l2); jif.add(l3); jif1.add(l4); jif1.add(l5); jif1.add(l6); dp.add(jif); dp.add(jif1); jif.setVisible(true); jif1.setVisible(true); } 6.JLayeredPane:
主要用于为JFC、Swing容器添加深度,允许组件在必要的时候相互重叠。
import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLayeredPane;
public class JLayeredPanel extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; static final int WIDTH=300; static final int HEIGHT=150; JLayeredPane lp=new JLayeredPane(); static JButton b1=new JButton("确定"); static JButton b2=new JButton("取消"); public JLayeredPanel() { ///设置顶层容器的标题 super("测试窗口"); ///将新建的JLayeredPane放到顶层容器内 super.setContentPane(lp); b1.addActionListener(this); // 按钮事件 b2.addActionListener(this); lp.add(b1, new Integer(200)); // 将组件添加到JLayeredPane中,指定所在的层 lp.add(b2, new Integer(300)); b1.setBounds(new Rectangle(100, 100, 100, 100)); // Button出现位置 b1.setVisible(true); // 显示 b2.setBounds(new Rectangle(50, 50, 100, 100)); b2.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(360, 260); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("确定")) { // 判断是哪个按钮的动作 lp.setLayer(b1, 300); // 重新设置组件层数 lp.setLayer(b2, 200); } else if (e.getActionCommand().equals("取消")) { lp.setLayer(b1, 200); lp.setLayer(b2, 300); } } public static void main(String args[]) { new JLayeredPanel(); } }
7.JRootPane
JRootPane的层次结构图如下所示:
chap 07 Swing事件处理机制
一、Swing中的监听器
Swing所支持的监听器分别有:
焦点监听器键盘监听器鼠标监听器鼠标移动监听器鼠标滑轮监听器属性变化监听器二、事件处理的过程与步骤
定义实现事件监听接口类创建事件监听器想事件源注册监听器对象//这段程序代码主要是创建一个文本框和一个普通按钮组件,当单击这个按钮组件时,会触发动作事件,清空文本框中的数据 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class EventHandle { static final int WIDTH=300; static final int HEIGHT=200; static JTextField l=new JTextField(20); public static void main(String[] args) { JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); JPanel contentPane=new JPanel(); contentPane.setLayout(new BorderLayout()); jf.setContentPane(contentPane); JButton b=new JButton("清空文本框中的信息"); contentPane.add(l,"North"); contentPane.add(b,"South"); ActionListener ac=new ActionHandler();// 创建一个事件监听器 b.addActionListener(ac); //向事件源注册 } } //定义实现事件监听类 class ActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { new EventHandle().l.setText(""); } }
//匿名类方式处理
b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { l.setText(""); } });
三、适配器类
适配器类不需要实现一个接口中的所有方法,而借口要求如此。适配器只需事先自己所需要的方法即可,因为适配器会将其他的不需要事先的方法自动以空事先的方式实现。
四、窗口事件的处理
监听借口:WindowsListener;
接口实现:
import java.awt.GridLayout; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;
import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;
public class WindowHandle extends JFrame { public WindowHandle() { super.setTitle("测试窗口"); ///向事件源注册监听器类 WindowListener wh=new windowhandler(); addWindowListener(wh); } public static void main(String[] args) { WindowHandle me=new WindowHandle(); me.setSize(400,300); me.setVisible(true); } ///创建一个窗口事件处理类 class windowhandler implements WindowListener { public void windowActivated(WindowEvent e)//此方法不需要,但必须要空实现 {} public void windowClosed(WindowEvent e) //此方法不需要,但必须要空实现 {} public void windowClosing(WindowEvent e) { JButton b1=new JButton("确定"); JButton b2=new JButton("取消"); JLabel l=new JLabel("你能确定关闭系统了吗?"); JDialog d=new JDialog((JFrame)e.getSource(),"系统出错了!",true);//创建一个对话框 d.setSize(200,100); d.setLocation(0,0); JPanel p=new JPanel(); p.setLayout(new GridLayout(1,2)); d.add(p,"South"); d.add(l,"Center"); p.add(b1); p.add(b2); d.setVisible(true); b1.setVisible(true); b2.setVisible(true); l.setVisible(true); } public void windowDeactivated(WindowEvent e){} //此方法不需要,但必须要空实现 public void windowIconified(WindowEvent e){} //此方法不需要,但必须要空实现 public void windowDeiconified(WindowEvent e){} //此方法不需要,但必须要空实现 public void windowOpened(WindowEvent e){} //此方法不需要,但必须要空实现 } }
适配器实现:
import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;
import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;
public class WindowHandle1 extends JFrame { public WindowHandle1() { super.setTitle("测试窗口"); WindowListener wh=new windowhandler(); addWindowListener(wh); } public static void main(String[] args) { WindowHandle1 me=new WindowHandle1(); me.setSize(400,300); me.setVisible(true); } class windowhandler extends WindowAdapter { public void windowClosing(WindowEvent e) { JButton b1=new JButton("确定"); JButton b2=new JButton("取消"); JLabel l=new JLabel("你能确定关闭系统了吗?"); JDialog d=new JDialog((JFrame)e.getSource(),"系统出错了!",true);//创建一个对话框 d.setSize(200,100); d.setLocation(0,0); JPanel p=new JPanel(); p.setLayout(new GridLayout(1,2)); d.add(p,"South"); d.add(l,"Center"); p.add(b1); p.add(b2); d.setVisible(true); b1.setVisible(true); b2.setVisible(true); l.setVisible(true);
} } }
五、动作事件的处理
监听借口:ActionListener;
//这段程序代码主要是为读者展示如何处理动作事件,当单击按钮组件时,其上的文本会发生变化 import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JButton; ///创建一个动作事件类,在这个类中,如果单击按钮的话,那么按钮上的文本会发生变化 public class ActionHandle extends Frame { JButton b; public ActionHandle(String str) { super(str); b=new JButton("确认"); add(b); ActionListener ac=new actionhandler(); b.addActionListener(ac); } public static void main(String[] args) { ActionHandle me=new ActionHandle("动作事件测试窗口"); me.pack(); me.setVisible(true); } class actionhandler implements ActionListener { public void actionPerformed(ActionEvent e) { ((JButton)e.getSource()).setLabel("取消"); } } }
5.焦点事件的处理
接口为:FocusListener
//这段程序代码主要是为读者展示如何使用焦点事件,当如何获得焦点以及如何失去焦点 import javax.swing.*; import java.awt.event.*; import java.awt.*; //此类继承了焦点监听器接口 public class test6 extends JFrame implements FocusListener { List info=new List(10); JTextField tf=new JTextField(""); JButton button=new JButton("确认"); public test6(String title) { super(title); add(info,"North"); add(tf,"Center"); add(button,"South"); tf.addFocusListener(this); } public void focusGained(FocusEvent e) { if(e.isTemporary())//将焦点更改事件的标识为暂时性或者永久性 info.add("暂时性获得"); else info.add("长久性获得"); } public void focusLost(FocusEvent e) { if(e.isTemporary())//将焦点更改事件的标识为暂时性或者永久性 info.add("暂时性失去"); else info.add("长久性失去"); } public static void main(String[] args) { test6 t=new test6("测试窗口"); t.pack(); t.setVisible(true); } }