1、10.6 用户界面对象 10.6.1按钮,命令按钮组件JButton: 用于接收用户的命令 构造函数 JButton(String text): 创建以指定文本作为按钮标签的按钮 JButton(Icon icon): 以指定的图标创建按钮Icon sample=new ImageIcon(“sample.gif”)JButton btn=new Jbutton(sample) JButton(String text,Icon icon):以指定的文本和图标创建按钮,常用的JButton方法,JButton使用示例,import javax.swing.*; import java.awt.*
2、; import java.awt.event.*;public class ButtonDemo extends JFrame implements ActionListener private JButton btnLeft; private JButton btnRight; private JButton btnUp; private JButton btnDown; private JTextField tfMove; int x,y; /用于控制文本框的显示位置 private JPanel movePanel; private Container container=getCon
3、tentPane(); public ButtonDemo() super(“位图按钮“); setSize(260,220); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,JButton使用示例(续),/创建向左移动的图标按钮 Icon icLeft=new ImageIcon(“left.gif“); btnLeft=new JButton(icLeft); /注册监听器 btnLeft.addActionListener(this); /设置提示字符串 btnLeft.setToolTipText(“控制文本向左移动“);/设
4、置向右移动的图标按钮 Icon icRight=new ImageIcon(“right.gif“); btnRight=new JButton(icRight); /注册监听器 btnRight.addActionListener(this); /设置提示字符串 btnRight.setToolTipText(“控制文本向右移动“);,JButton使用示例(续),/设置向上移动的图标按钮 Icon icUp=new ImageIcon(“Up.gif“); btnUp=new JButton(icUp); /注册监听器 btnUp.addActionListener(this); /设置提
5、示字符串 btnUp.setToolTipText(“控制文本向上移动“);/设置向下移动的图标按钮 Icon icDown=new ImageIcon(“Down.gif“); btnDown=new JButton(icDown); /注册监听器 btnDown.addActionListener(this); /设置提示字符串 btnDown.setToolTipText(“控制文本向下移动“); tfMove=new JTextField(“位图按钮示例“); movePanel=new JPanel(); ,JButton使用示例(续),public void setLayout()
6、 container.setLayout(new BorderLayout(); container.add(btnLeft,BorderLayout.WEST); container.add(btnRight,BorderLayout.EAST); container.add(btnUp,BorderLayout.NORTH); container.add(btnDown,BorderLayout.SOUTH); /设置移动面板的布局为null movePanel.setLayout(null); movePanel.add(tfMove); /设置文本框的显示位置 x=0; y=0; tf
7、Move.setBounds(x,y,80,20); container.add(movePanel,BorderLayout.CENTER); ,JButton使用示例(续),public static void main(String args) ButtonDemo frame=new ButtonDemo(); frame.setLayout(); frame.setVisible(true); ,JButton使用示例(续),public void actionPerformed(ActionEvent e) if (e.getSource()=btnRight) x+=2; /控制
8、文本框在水平方向上向右移动 if (e.getSource()=btnLeft) x-=2; /控制文本框在水平方向上向左移动 if (e.getSource()=btnUp) y-=2; /控制文本框在水平方向上向上移动 if (e.getSource()=btnDown) y+=2; /控制文本框在水平方向上向下移动 /重新调整文本框的位置 tfMove.setBounds(x,y,80,20); ,10.6.2 标签,标签:文本或图象的显示区域。,10.6.3 单行文本框和多行文本域,JTextField:可以输入一行信息,多行文本域,JTextArea:可以输入多行信息,文本框和文本域
9、举例,import java.awt.*; import javax.swing.*; import java.awt.event.*; public class TextDemo extends JFrame implements ActionListener private JLabel lbName,lbAddr,lbTel; private JTextField tfName,tfAddr,tfTel; private JTextArea taCollect; private JPanel InputPanel,CollectPanel; private GridBagLayout I
10、nputLayout; private GridBagConstraints constraints; private Container container; public TextDemo() super(“个人信息“); setSize(300,260); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container=this.getContentPane(); container.setLayout(new GridLayout(2,1); ,文本框和文本域举例(续),public void setLayout()InputPane
11、l=new JPanel();InputLayout=new GridBagLayout();constraints=new GridBagConstraints();InputPanel.setLayout(InputLayout);/创建姓名标签并添加到输入面板constraints.anchor=GridBagConstraints.CENTER;lbName=new JLabel(“姓名“);constraints.fill=GridBagConstraints.NONE;AddComponent(InputPanel,lbName,InputLayout,constraints,0,
12、0,1,1,20,0);/创建姓名文本框并添加到输入面板tfName=new JTextField();constraints.fill=GridBagConstraints.HORIZONTAL;AddComponent(InputPanel,tfName,InputLayout,constraints,0,1,1,1,80,100);,文本框和文本域举例(续),/ 创建住址标签 lbAddr=new JLabel(“住址“); constraints.fill=GridBagConstraints.NONE; AddComponent(InputPanel,lbAddr,InputLayo
13、ut,constraints,1,0,1,1,20,0); / 创建住址文本框并添加到输入面板 tfAddr=new JTextField(); constraints.fill=GridBagConstraints.HORIZONTAL; AddComponent(InputPanel,tfAddr,InputLayout,constraints,1,1,1,1,80,100);/创建电话标签 lbTel=new JLabel(“电话“); constraints.fill=GridBagConstraints.NONE; AddComponent(InputPanel,lbTel,Inpu
14、tLayout,constraints,2,0,1,1,20,0);,文本框和文本域举例(续),/ 创建电话文本框并添加到输入面板 tfTel=new JTextField(); tfTel.addActionListener(this); constraints.fill=GridBagConstraints.HORIZONTAL; AddComponent(InputPanel,tfTel,InputLayout,constraints,2,1,1,1,80,100); /添加汇总信息面板 CollectPanel=new JPanel(); CollectPanel.setLayout(
15、new BorderLayout(); taCollect=new JTextArea(“汇总信息“); taCollect.setEditable(false);/不允许直接对汇总信息编辑 CollectPanel.add(taCollect); /将输入面板和汇总面板信息添加到框架中 container.add(InputPanel); container.add(CollectPanel); ,文本框和文本域举例(续),public static void main(String args) TextDemo frame=new TextDemo();frame.setLayout();
16、frame.setVisible(true); public void actionPerformed(ActionEvent e)if(e.getSource() instanceof JTextField)/向文本域添加信息taCollect.append(“n“);taCollect.append(tfName.getText()+“n“);taCollect.append(tfAddr.getText()+“n“);taCollect.append(tfTel.getText()+“n“);,10.6.10 复选框和单选按钮,复选框(JCheckBox): 可用于接收用户的多选输入,J
17、RadioButton和ButtonGroup,JRadioButton用于接收用户的单选输入。 按钮组组件ButtonGroup与JRadioButton配合使用,建立多个单选按钮之间的这种单选关系 。,常用的JRadioButton方法,常用的ButtonGroup方法,ButtonGroup使用举例,ButtonGroup btg=new JButttonGroup(); JRadioButtion jrb1=new JRadioButton(“男“,new ImageIcon(”Man.gif“); JRadioButtion jrb2=new JRadioButton(“女“,new
18、 ImageIcon(”Woman.gif“); btg.add(jrb1); btg.add(jrb2);,JRadioButton和JCheckBox的事件处理,产生ItemEvent和ActionEvent事件 public void itemStateChanged(ItemEvent e) /确定事件源为单选按钮if (e.getSource() instanceof JRadioButton) if(jrb1.isSelected()/当选中jrb1单选按钮时的处理代码else if (jrb2.isSelected()/当选中jrb2单选按钮时的处理代码 ,JRadioButto
19、nDemo.java,JCheckBoxDemo.java,复选框应用示例,import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JCheckBoxDemo extends JFrame implements ItemListener private JCheckBox right1;private JCheckBox right2;private JCheckBox right10;private JCheckBox right8;private char choices=0,0,0,0;
20、private Container container;JLabel displayLabel;,复选框应用示例(续),public void setLayout() right1 = new JCheckBox(“1“);right1.setSelected(false);right2 = new JCheckBox(“2“);right2.setSelected(false);right10 = new JCheckBox(“10“);right10.setSelected(false); right8 = new JCheckBox(“8“);right8.setSelected(fal
21、se);right1.addItemListener(this);right2.addItemListener(this);right10.addItemListener(this);right8.addItemListener(this);/创建标签对象displayLabel = new JLabel(“0000“);,复选框应用示例(续),/创建面板对象checkPanelJPanel checkPanel = new JPanel( );/将复选框按钮添加到checkPanel的同一列checkPanel.setLayout(new GridLayout(0, 1); checkPan
22、el.add(right1);checkPanel.add(right2);checkPanel.add(right10);checkPanel.add(right8);container.setLayout(new BorderLayout( );container.add(checkPanel, BorderLayout.WEST);container.add(displayLabel, BorderLayout.CENTER);,复选框应用示例(续),public void itemStateChanged(ItemEvent e) int index = 0;Object source
23、 = e.getItemSelectable( );if (source = right8) index=0;choicesindex=8;else if (source = right10) index=1;choicesindex=10;,复选框应用示例(续),else if (source = right2) index=2;choicesindex=2;else if (source = right1)index=3;choicesindex=1;if (e.getStateChange( ) = ItemEvent.DESELECTED)choicesindex= -;display
24、Label.setText(new String(choices);,单选框应用示例,import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JRadioButtonDemo extends JFrame implements ActionListenerstatic String birdString = “鸟“;static String catString = “猫“;static String dogString = “狗“;static String rabbitString = “
25、兔“;private Container container;private JLabel display;private JRadioButton birdButton,catButton,dogButton,rabbitButton;private ButtonGroup group;,单选框应用示例(续),public void setLayout() / 创建单选按钮对象birdButton = new JRadioButton(birdString); birdButton.setActionCommand(birdString);catButton = new JRadioButt
26、on(catString); catButton.setActionCommand(catString);dogButton = new JRadioButton(dogString); dogButton.setActionCommand(dogString);rabbitButton = new JRadioButton(rabbitString); rabbitButton.setActionCommand(rabbitString);,单选框应用示例(续),/ 将单选按钮组成一组,建立它们之间的单选关系 group = new ButtonGroup(); group.add(bird
27、Button); group.add(catButton); group.add(dogButton); group.add(rabbitButton);/ 为单选按钮添加事件监听器birdButton.addActionListener(this); catButton.addActionListener(this); dogButton.addActionListener(this); rabbitButton.addActionListener(this);,单选框应用示例(续),public void actionPerformed(ActionEvent e) display.set
28、Text(“ 你选中了“ + e.getActionCommand() + “。“); ,10.6.5 组合框,复选框(JComboBox): 可供用户从项目列表中选择一个或多个项目,用于限制用户选择范围以及避免复杂数据的输入。,组合框的事件处理,产生两个ItemEvent事件: 取消以前选择的项目 选中当前选择的项目 public void itemStateChanged(ItemEvent e) /确定事件源为组合框if (e.getSource() instanceof JComboBox) String str=(String)e. getSelectedItem(); ,10.6.
29、6 滚动条,滚动条(JScrollBar):可供用户在一定的数值范围内进行选择。,滚动条的事件处理,产生AdjustmentEvent事件: public void adjustmentValueChanged(AdjustmentEvent e) /确定事件源为滚动条if (e.getSource() instanceof JScrollBar) / 处理代码 ,ScrollBarDemo.java,滚动条应用示例,public class ScrollBarDemo extends JFrame implements AdjustmentListener/创建水平和垂直滚动条JScroll
30、Bar vScb=new JScrollBar(JScrollBar.VERTICAL,0,0,0,100);JScrollBar hScb=new JScrollBar(JScrollBar.HORIZONTAL,0,0,0,100);JPanel panel= new JPanel();GridBagLayout Layout=new GridBagLayout();GridBagConstraints constraints=new GridBagConstraints();JLabel lbVScrollBar=new JLabel(“垂直滚动条的值“,JLabel.CENTER);J
31、Label lbHScrollBar=new JLabel(“水平滚动条的值“,JLabel.CENTER);JTextField tfVScrollBar=new JTextField();JTextField tfHScrollBar=new JTextField();,滚动条应用示例(续),public void setLayout() panel.setLayout(Layout); constraints.anchor=GridBagConstraints.CENTER; constraints.fill=GridBagConstraints.HORIZONTAL; /以网格包布局向
32、面板添加文本框和标签组件 addComponent(panel,lbHScrollBar,Layout,constraints,0,0,1,1,20,0); addComponent(panel,tfHScrollBar,Layout,constraints,0,1,1,1,80,100); addComponent(panel,lbVScrollBar,Layout,constraints,1,0,1,1,20,0); addComponent(panel,tfVScrollBar,Layout,constraints,1,1,1,1,80,100); /将面板panel、水平和垂直滚动条添
33、加到框架中 this.getContentPane().add(panel,BorderLayout.CENTER); this.getContentPane().add(vScb,BorderLayout.EAST); this.getContentPane().add(hScb,BorderLayout.SOUTH); /注册事件监听器 vScb.addAdjustmentListener(this); hScb.addAdjustmentListener(this); ,滚动条应用示例(续),public void adjustmentValueChanged(AdjustmentEve
34、nt e) if(e.getSource()=hScb) /获取水平滚动条的值,在相应的文本框中显示 tfHScrollBar.setText(String.valueOf(hScb.getValue(); if(e.getSource()=vScb) /显示垂直滚动条的当前值 tfVScrollBar.setText(String.valueOf(vScb.getValue(); ,10.6.7 滚动面板,滚动面板(JScrollPane):一种中间层次的容器组件,当窗格含有的内容显示不下时,允许用户使用水平方向和垂直方向的滚动条上下左右移动窗格的内容,从而可以看见窗格含有的全部内容 。,滚
35、动面板示例,import javax.swing.*; public class ScrollPaneDemo extends JFrame JScrollPane scrollPane=new JScrollPane();Icon icon =new ImageIcon(“img10.jpg“);JLabel lbIcon=new JLabel(icon);public ScrollPaneDemo()super(“滚动面板示例“);setSize(350,300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void
36、 setLayout()/向滚动面板的浏览窗口添加组件scrollPane.getViewport().add(lbIcon);this.getContentPane().add(scrollPane);public static void main(String args)ScrollPaneDemo frame = new ScrollPaneDemo();frame.setLayout();frame.setVisible(true); ,10.6.8 菜单,菜单:涉及到五个类,JMenuBar、JMenu、JMenuItem、JCheckBoxMenuItem和JRadioButton
37、MenuItem。 实现菜单的步骤: 首先创建一个MenuBar对象,并将其置于一个可容纳菜单的容器(如Frame对象)中。 创建一个或多个Menu对象,并将它们添加到先前创建的MenuBar对象中。 创建一个或多个MenuItem对象,再将其加入到各Menu对象中,创建菜单示例,import javax.swing.*; public class TestMenuBar public static void main(String args) /设置菜单栏 JFrame frame1 = new JFrame(“MenuBar“); JMenuBar jmb = new JMenuBar(); frame1.setJMenuBar(jmb); /设置菜单 JMenu jm1 = new JMenu(“File“); JMenu jm2 = new JMenu(“Format“); JMenu jm3 = new JMenu(“Help“); jmb.add(jm1); jmb.add(jm2); jmb.add(jm3);,