博文正文开头格式:(2分)
项目 |
内容 |
这个作业属于哪个课程 |
|
这个作业的要求在哪里 |
https://www.cnblogs.com/nwnu-daizh/p/11995615.html |
作业学习目标 |
(1) 掌握菜单组件用途及常用API; (2) 掌握对话框组件用途及常用API; (3) 学习设计简单应用程序的GUI。 |
第一部分:总结菜单、对话框两类组件用途及常用API
菜单
菜单创建
菜单项中的图标
复选框和单选按钮菜单项
弹出菜单
快捷键和加速器
启用和禁用菜单项
工具栏
工具提示
Swing菜单简介
菜单是GUI编程中经常用到的一种组件。 位于窗口顶部的菜单栏(menu bar)中包括下拉菜单的
名字。点击一个名字就可以打开包含菜单项(menu items)和子菜单(submenus)的菜单。
菜单的创建
●创建一个菜单栏
菜单栏是一个可以添加到容器组件任何位置的组件。通常放置在框架的顶部。
JMenuBar menuBar=new JMenuBar();
●调用框架的setJMenuBar方法可将一个菜单栏对象添加到框架上
frame. setJMenuBar ( menuBar) ;
●创建菜单对象,并将菜单对象添加到菜单栏中
JMenu editMenu=new Jmenu("Edit'");
menuBar. add(editMenu)
●向菜单对象添加-一个菜单项。
JMenItem pasteltem=new JMenItem();
editMenu.add(pasteltem);
●向菜单对象添加分隔符行。
editMenu. addSeperator();
●向菜单对象项添加子菜单。
JMenu optionsMenu=new Jmenu("option");
editMenu.add(optionsMenu);
菜单事件监听器的插入方法
●当用户选择菜单时,将触发一一个动作事件。 这里需要为每个菜单项安装一一个动作监听器。
ActionListener listener= .. ;
pasteltem.addActionListener(listener);
●菜单对象的add方法可返回创建的子菜单项。可以使用下列方式获取它,并添加监听器:
JMenultem pasteltem = editMenu.add("Paste");
pasteltem.addActionListener(listener);
弹出菜单(JPopupMenu)
●弹出菜单是不固定在菜单栏中随处浮动的菜单;.
创建弹出菜单
●创建一个弹出菜单与创建一一个常规菜单的方法类似但是弹出菜单没有标题:
JPopupMenu popup = new JPopupMenu();
●然后用常规方法为弹出菜单添加菜单项:
JMenultem item = new JMenultem("Cut");
item.addActionL istener(listener);
popup.add(item);
●弹出菜单调用show方法才能显示出来:
popup.show(panel,x,y);
弹出式触发器
●弹出式触发器(pop-up trigger):用户点击鼠标某个键时弹出菜单。
●在Windows或者Linux中,弹出式触发器是鼠标右键。
●要想在用户点击某-一个组件的时候弹出菜单,该组件就要调用以下方法设置弹出式触发器:
component.setComponentPopupMenu(popup);
快捷键
●可以为菜单项设置快捷键。在当前菜单打开的情况下,可以按下某菜单项的快捷键,相当于鼠标单击了该菜单项。
JMenultem Cutltem=new JMenultem(" Index ");
Cutltem.setMnemonic(" I");
●此快捷键就会自动显示在菜单项中,快捷键下面有一条下划线。
加速器
●加速器可以在不打开菜单的情况下选中菜单项的快捷键。例如,很多应用程序把CTRL + O和CTRL+S关联到菜单中的Open和Save项。
●使用SetAccelerator方法可以将加速器关联到一个菜单项。该方法使用KeyStroke类型的对象作为参数。
例如:
openltem.setAccelerator(KeyStroke.getKeyStroke("ctrlo" ));
●当用户按下加速器组合键时,就自动选择了相应的菜单项,同时激活一个动作事件。
●加速器只能关联在菜单项,而不能关联菜单; .
●加速器实际上并不打开菜单,而是直接激活菜单关联的动作事件。
启用和禁用菜单项
●在程序运行过程中,经常需要屏蔽某些暂时不适用的命令,待到条件允许时再使之重新可用。
●屏蔽/启用菜单项的方法:
—— aMenultem.setEnabled(boolean)
—— 当参数值为false时,屏蔽该菜单项;
—— 当参数值为true时,启用该菜单项:
●如果需要动态启用/屏蔽某菜单项,则需要为菜单项注册“menuSelected"事件监听器。
javax.swing.event包定义了MenuListener接口,它有三个方法:
- void menuSelected(MenuEvent event)
- void menuDeselected(MenuEvent event)
- void menuCanceled(MenuEvent event) ;
工具栏(JToolBar)
●工具栏在程序中提供快速访问常用命令的按钮栏。
●工具栏的优点在于可以移动,脱离菜单栏或拖拽到框架其他地方
JToolBar bar = new JToolBar);
bar add(blueAction);
bar ddyellwAction);
bar add(redAction);
bar. addSeparator0;
bar add(exitAction);
●关闭包含工具栏的框架后,工具栏回到原始的框架中。
工具提示
●工具提示(tooltips) 提示工具栏中某个按钮的含义。当光标停留在某个按钮上时,工具提示就会激活。
●工具提示文本显示在一个矩形里。当用户移开鼠标是,工具提示就会自动地消失;
●调用setToolTest方 法添加工具提示到JComponent;
exitButton.setToolTipText(" Exit");
●另外一种方法是,如果使用Action对象,就可以用SHORT-DESCRIPTION关联工具提示:
exitButton.putValue(Action.SHORT-DESCRIPTION," Exit ");
对话框.
●选项对话框
●创建对话框
●数据选择
●文件对话框
●颜色选择器
对话框(JDialog)
●对话框是一种大小不能变化、不能有菜单的容器窗口;
●对话框不能作为一个应用程序的主框架,而必须包含在其他的容器中。
●Java提供多种形式的对话框。
一JOpt ionPane类: 支持简单、标准的对话框;
一JFileChooser类: 支持文件打开、保存对话框:
一ProgressMoni tor类:支持操作进度条控制对话框等。
●对话框依赖于框架。当框架撤销时,依赖该框架的对话框也撤销。当框架窗口恢复时,依赖框架的对话框又返回屏幕。
●对话框分为无模式和有模式两种。
— 有模式的对话框处于激活状态时,程序只能响应对话框内部的事件,不能再激活它所依赖的窗口或组件,而且它将堵塞当前线程的执行,即堵塞使得对话框处于激活状态的线程,直到该对话框消失不可见。
— 无模式对话框处于激活状态时,程字仍能激活它所依赖的窗口或组件,它也不堵塞线程的执行。
●构造一个标题为“Dialog"的模式对话框,该对话框为框架frame所拥有
JDialog dialog = new JDialog(rame, "Dialog".true)
●当对话框的拥有者被清除(destroyed) 时,对话框也会被清除
●对话框在显示时,如果其拥有者被最小化,对话框也将变为不可见,当其拥有者再次显示时,对话框会随之变为可见
●setVisible(true)方 法显示,setVisible(false)方法可以将一个对话框隐藏起来
选项对话框( JOptionPane )
●J0ptionPane提供的对话框是模式对话框。当模式对话框显示时,它不允许用户输入到程序的其他的窗口。使用JOptionPane,可以创建和自定义问题、信息、警告和错误等几种类型的对话框。
选项对话框
●JOptionPane类
●定义了多个showXxxDialog形式的静态方法
- showMessageDialog ——信息对话框,显示信息,告知用户发生了什么情况
- showConfirmDialog——确认对话框,显示问题,要求用户进行确认(yes/no/cancel)
- showOptionDialog-——选项对话框,显示选项,要求用户进行选择
- ShowluputDialog——输入对话框,提示用户进行输入
创建对话框(JDialog)
●对话框的构造方法
- JDialog(F rame owner)——构造一个没有标题的非模式对话框
- JDialog(F rame owner, boolean modal)——构造一 个没有 标题的对话框,boolean 型参数modal指定对话框是否为模式窗口
- JDialog(F rame owner, String title)——构造一 个有 标题的非模式对话框
- JDialog(Frame owner, String title, boolean modal)——构造一 个有标题的对话框
showXxxDialog方法的参数
●Component parentComponent
— 对话框的父窗口对象,其屏幕坐标将决定对话框的显示位置:此参数也可以为nul1,表示采用缺省的Frame作为父窗口,此时对话框将设置在屏幕的正中
●Object message
— 显示在对话框中的描述信息。该参数通常是一个String对象, 但也可以是一个图标、一个组件或者一个对象数组
●String title
—对话框的标题
●int messageType
-对话框所传递的信息类型。可以为以下常量
ERROR MESSAGE
INFORMIATION MIESSAGE
WARNING MESSAGE
QUESTION MESSAGE
PLAIN MESSAGE
-除PLAIN MESSAGE之外,其他每种类型都对应于一个缺省图标
文件对话框
专门用于对文件(或目录)进行浏览和选择的对 话框,常用的构造方法: – JFileChooser():根据用户的缺省目录创建文件对话框 – JFileChooser(File currentDirectory):根据File型参数 currentDirectory指定的目录创建文件对话框
颜色对话框
JColorChooser中的静态方法:
public static Color showDialog(Component component,String title,Color initialColor)
●initialColor指定颜色对话框返回初始颜色。点击确定,showDialog()方法返回对话框所选择的颜色;点击撤销或关闭,颜色对话框消失,返回null
●参数component指定对话框所依赖的组件,title 指定对话框的标题
实验内容和步骤
实验1: 导入第12章示例程序,测试程序并进行组内讨论。
测试程序1
l 在elipse IDE中调试运行教材512页程序12-8,结合运行结果理解程序;
l 掌握菜单的创建、菜单事件监听器、复选框和单选按钮菜单项、弹出菜单以及快捷键和加速器的用法。
程序12-8:
package menu; import java.awt.*; import javax.swing.*; /** * @version 1.25 2018-04-10 * @author Cay Horstmann */ public class MenuTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new MenuFrame(); frame.setTitle("MenuTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package menu; import java.awt.event.*; import javax.swing.*; /** * A frame with a sample menu bar. (带有示例菜单栏的框架) */ public class MenuFrame extends JFrame { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; private Action saveAction; private Action saveAsAction; private JCheckBoxMenuItem readonlyItem;//用给定的readonlyItem标签构造一个复选框菜单项 private JPopupMenu popup;//弹出菜单 /** * A sample action that prints the action name to System.out.(将操作名称打印到System.out的示例操作。) */ class TestAction extends AbstractAction { public TestAction(String name) { super(name); } public void actionPerformed(ActionEvent event) { System.out.println(getValue(Action.NAME) + " selected.");//实现接口方法 } } public MenuFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); JMenu fileMenu = new JMenu("File");//创建一个菜单对象 File fileMenu.add(new TestAction("New"));//将TestAction对象添加到菜单中 //调用setAccelerator方法将加速器关联到一个菜单项上 var openItem = fileMenu.add(new TestAction("Open")); openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));//将加速器ctrl+O关联到openItem菜单项 fileMenu.addSeparator(); saveAction = new TestAction("Save"); JMenuItem saveItem = fileMenu.add(saveAction); saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));//将加速器ctrl+S关联到saveItem菜单项 saveAsAction = new TestAction("Save As");//将要执行的行为赋值给菜单子项 fileMenu.add(saveAsAction); fileMenu.addSeparator(); /* * 采用扩展抽象类AbstractAction来定义一个实现Action接口的类 * 在AbstractAction对象的构造器中指定菜单项标签并覆盖actionPerformed方法来获得菜单动作处理器 */ fileMenu.add(new AbstractAction("Exit") //动作监听器 { public void actionPerformed(ActionEvent event) { System.exit(0); } }); // 演示复选框和单选按钮菜单 /* * 调用setEnabled方法启用和禁用菜单项 save和save As */ readonlyItem = new JCheckBoxMenuItem("Read-only");//创建一个复选框单菜单项 readonlyItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { boolean saveOk = !readonlyItem.isSelected(); saveAction.setEnabled(saveOk);//调用setEnable方法,启用save菜单项 saveAsAction.setEnabled(saveOk);//调用setEnable方法,启用saveAs菜单项 } }); //将单选钮菜单项加入到按钮组中,当按钮组中的一个按钮被选中时,其他的按钮都自动的变为未选择项 ButtonGroup group = new ButtonGroup();//创建一个按钮组 JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");//单选钮菜单项 Insert insertItem.setSelected(true);//设置Insert为选定状态 JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");//单选钮菜单项 Overtype group.add(insertItem); group.add(overtypeItem); //演示图标 var cutAction = new TestAction("Cut"); cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));//使用关联的键设置此对象的一个属性 var copyAction = new TestAction("Copy"); copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif")); var pasteAction = new TestAction("Paste"); pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif")); JMenu editMenu = new JMenu("Edit");//创建一个菜单对象 Edit editMenu.add(cutAction); editMenu.add(copyAction); editMenu.add(pasteAction); // 演示嵌套菜单 JMenu optionMenu = new JMenu("Options");//创建子菜单 optionMenu.add(readonlyItem); optionMenu.addSeparator(); optionMenu.add(insertItem); optionMenu.add(overtypeItem); editMenu.addSeparator(); editMenu.add(optionMenu); // 说明助记符 JMenu helpMenu = new JMenu("Help");//创建一个菜单对象 Help helpMenu.setMnemonic('H');//调用setMnemonic方法为菜单设置快捷键 JMenuItem indexItem = new JMenuItem("Index"); indexItem.setMnemonic('I');//调用setMnemonic方法设置快捷键 helpMenu.add(indexItem); // 向操作添加助记键 /* * 指定希望加下划线的字符 * 把快捷键作为Action.MNEMONIC_KEY的键值添加到对象中 */ TestAction aboutAction = new TestAction("About"); aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A')); helpMenu.add(aboutAction); // 将所有顶级菜单添加到菜单栏 JMenuBar menuBar = new JMenuBar();//创建一个菜单栏 setJMenuBar(menuBar);//调用setJMenuBar方法将菜单栏添加到框架上 //将顶层菜单添加到菜单栏中 menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(helpMenu); // 显示弹出窗口 popup = new JPopupMenu();//创建一个弹出菜单 //添加菜单项 popup.add(cutAction); popup.add(copyAction); popup.add(pasteAction); var panel = new JPanel(); panel.setComponentPopupMenu(popup);//设置弹出式触发器(鼠标右键) add(panel); } }
运行结果:
测试程序2
l 在elipse IDE中调试运行教材517页程序12-9,结合运行结果理解程序;
l 掌握工具栏和工具提示的用法;
l 记录示例代码阅读理解中存在的问题与疑惑。
程序12-9:
package toolBar; import java.awt.*; import javax.swing.*; /** * @version 1.15 2018-04-10 * @author Cay Horstmann */ public class ToolBarTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new ToolBarFrame(); frame.setTitle("ToolBarTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package toolBar; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A frame with a toolbar and menu for color changes. */ public class ToolBarFrame extends JFrame { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; private JPanel panel; public ToolBarFrame() //定义工具提示类 { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add a panel for color change panel = new JPanel();//创建新的JPanel add(panel, BorderLayout.CENTER); //建立动作 var blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE); var yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"), Color.YELLOW); var redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED); var exitAction = new AbstractAction("Exit", new ImageIcon("exit.gif")) { public void actionPerformed(ActionEvent event) { System.exit(0); } }; //用SHORT_DESCRIPTION关联工具提示 exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit"); // populate toolbar var bar = new JToolBar(); bar.add(blueAction);//添加Action对象填充工具栏 bar.add(yellowAction); bar.add(redAction); bar.addSeparator();//用分隔符将按钮分组 bar.add(exitAction); add(bar, BorderLayout.NORTH); // populate menu var menu = new JMenu("Color");//显示颜色的菜单 menu.add(yellowAction);//在菜单添加颜色动作 menu.add(blueAction); menu.add(redAction); menu.add(exitAction); var menuBar = new JMenuBar(); menuBar.add(menu); setJMenuBar(menuBar);//设置菜单栏 } /** * The color action sets the background of the frame to a given color. */ class ColorAction extends AbstractAction { public ColorAction(String name, Icon icon, Color c) { putValue(Action.NAME, name);//动作名称,显示在按钮和菜单 putValue(Action.SMALL_ICON, icon);//存储小图标的地方;显示在按钮、菜单项或工具栏中 putValue(Action.SHORT_DESCRIPTION, name + " background"); putValue("Color", c); } public void actionPerformed(ActionEvent event) { Color c = (Color) getValue("Color"); panel.setBackground(c); } } }
运行结果:
测试程序3
l 在elipse IDE中调试运行教材544页程序12-15、12-16,结合运行结果理解程序;
l 掌握选项对话框的用法。
l 记录示例代码阅读理解中存在的问题与疑惑。
OptionDialogTest:
package optionDialog; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class OptionDialogTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new OptionDialogFrame(); frame.setTitle("OptionDialogTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
ButtonPanel:
package optionDialog; import javax.swing.*; /** * A panel with radio buttons inside a titled border. */ public class ButtonPanel extends JPanel { private ButtonGroup group; /** * Constructs a button panel. * @param title the title shown in the border * @param options an array of radio button labels */ public ButtonPanel(String title, String... options) { setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title)); //设置组件的边框,向现有边框添加一个标题 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));//从上到下垂直布置组件 group = new ButtonGroup(); // make one radio button for each option for (String option : options) { var button = new JRadioButton(option);//创建一个具有指定文本的状态为未选择的单选按钮 button.setActionCommand(option);//设置此按钮的动作命令 add(button); group.add(button);//将按钮添加到组中 button.setSelected(option == options[0]);//设置按钮的状态 } } /** * Gets the currently selected option. * @return the label of the currently selected radio button. */ public String getSelection() { return group.getSelection().getActionCommand();//返回该按钮的动作命令字符串 } }
OptionDialogFrame:
package optionDialog; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; /** * A frame that contains settings for selecting various option dialogs. */ public class OptionDialogFrame extends JFrame { private ButtonPanel typePanel; private ButtonPanel messagePanel; private ButtonPanel messageTypePanel; private ButtonPanel optionTypePanel; private ButtonPanel optionsPanel; private ButtonPanel inputPanel; private String messageString = "Message"; private Icon messageIcon = new ImageIcon("blue-ball.gif"); private Object messageObject = new Date(); private Component messageComponent = new SampleComponent(); public OptionDialogFrame() { var gridPanel = new JPanel(); gridPanel.setLayout(new GridLayout(2, 3));//创建2行3列的网格布局 typePanel = new ButtonPanel("Type", "Message", "Confirm", "Option", "Input"); messageTypePanel = new ButtonPanel("Message Type", "ERROR_MESSAGE", "INFORMATION_MESSAGE", "WARNING_MESSAGE", "QUESTION_MESSAGE", "PLAIN_MESSAGE"); messagePanel = new ButtonPanel("Message", "String", "Icon", "Component", "Other", "Object[]"); optionTypePanel = new ButtonPanel("Confirm", "DEFAULT_OPTION", "YES_NO_OPTION", "YES_NO_CANCEL_OPTION", "OK_CANCEL_OPTION"); optionsPanel = new ButtonPanel("Option", "String[]", "Icon[]", "Object[]"); inputPanel = new ButtonPanel("Input", "Text field", "Combo box"); gridPanel.add(typePanel); gridPanel.add(messageTypePanel); gridPanel.add(messagePanel); gridPanel.add(optionTypePanel); gridPanel.add(optionsPanel); gridPanel.add(inputPanel);//添加按钮 // add a panel with a Show button var showPanel = new JPanel(); var showButton = new JButton("Show");//创建一个带"Show"的按钮 showButton.addActionListener(new ShowAction()); showPanel.add(showButton); add(gridPanel, BorderLayout.CENTER);//中间区域的布局约束(容器中央) add(showPanel, BorderLayout.SOUTH);//南区域的布局约束(容器底部) pack(); } /** * Gets the currently selected message. * @return a string, icon, component, or object array, depending on the Message panel selection */ public Object getMessage() { String s = messagePanel.getSelection(); if (s.equals("String")) return messageString; //将此字符串与指定的对象比较 else if (s.equals("Icon")) return messageIcon; else if (s.equals("Component")) return messageComponent; else if (s.equals("Object[]")) return new Object[] { messageString, messageIcon, messageComponent, messageObject }; else if (s.equals("Other")) return messageObject; else return null; } /** * Gets the currently selected options. * @return an array of strings, icons, or objects, depending on the Option panel selection */ public Object[] getOptions() { String s = optionsPanel.getSelection(); if (s.equals("String[]")) return new String[] { "Yellow", "Blue", "Red" }; else if (s.equals("Icon[]")) return new Icon[] { new ImageIcon("yellow-ball.gif"), new ImageIcon("blue-ball.gif"), new ImageIcon("red-ball.gif") }; else if (s.equals("Object[]")) return new Object[] { messageString, messageIcon, messageComponent, messageObject }; else return null; } /** * Gets the selected message or option type * @param panel the Message Type or Confirm panel * @return the selected XXX_MESSAGE or XXX_OPTION constant from the JOptionPane class */ public int getType(ButtonPanel panel) { String s = panel.getSelection(); try { return JOptionPane.class.getField(s).getInt(null); } catch (Exception e) { return -1; } } /** * The action listener for the Show button shows a Confirm, Input, Message, or Option dialog * depending on the Type panel selection. */ private class ShowAction implements ActionListener { public void actionPerformed(ActionEvent event) { if (typePanel.getSelection().equals("Confirm")) JOptionPane.showConfirmDialog( OptionDialogFrame.this, getMessage(), "Title", getType(optionTypePanel), getType(messageTypePanel)); else if (typePanel.getSelection().equals("Input")) { if (inputPanel.getSelection().equals("Text field")) JOptionPane.showInputDialog( OptionDialogFrame.this, getMessage(), "Title", getType(messageTypePanel)); else JOptionPane.showInputDialog(OptionDialogFrame.this, getMessage(), "Title", getType(messageTypePanel), null, new String[] { "Yellow", "Blue", "Red" }, "Blue"); } else if (typePanel.getSelection().equals("Message")) JOptionPane.showMessageDialog( OptionDialogFrame.this, getMessage(), "Title", getType(messageTypePanel)); else if (typePanel.getSelection().equals("Option")) JOptionPane.showOptionDialog( OptionDialogFrame.this, getMessage(), "Title", getType(optionTypePanel), getType(messageTypePanel), null, getOptions(), getOptions()[0]); } } } /** * A component with a painted surface */ class SampleComponent extends JComponent { public void paintComponent(Graphics g) { var g2 = (Graphics2D) g; var rect = new Rectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1); //根据指定的 double 坐标构造和初始化 Rectangle2D g2.setPaint(Color.YELLOW); g2.fill(rect); g2.setPaint(Color.BLUE); g2.draw(rect); } public Dimension getPreferredSize() { return new Dimension(10, 10);//构造一个 Dimension,并将其初始化为指定宽度和高度 } }
运行结果:
测试程序4
l 在elipse IDE中调试运行教材552页程序12-17、12-18,结合运行结果理解程序;
l 掌握对话框的创建方法;
l 记录示例代码阅读理解中存在的问题与疑惑。
package dataExchange; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class DataExchangeTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new DataExchangeFrame(); frame.setTitle("DataExchangeTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package dataExchange; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A frame with a menu whose File->Connect action shows a password dialog. */ public class DataExchangeFrame extends JFrame { public static final int TEXT_ROWS = 20; public static final int TEXT_COLUMNS = 40; private PasswordChooser dialog = null; private JTextArea textArea; public DataExchangeFrame() { // 构造文件菜单 var mbar = new JMenuBar(); setJMenuBar(mbar); var fileMenu = new JMenu("File"); mbar.add(fileMenu); // 添加“连接”和“退出”菜单项 var connectItem = new JMenuItem("Connect"); connectItem.addActionListener(new ConnectAction()); fileMenu.add(connectItem); // 退出项退出程序 var exitItem = new JMenuItem("Exit"); exitItem.addActionListener(event -> System.exit(0)); fileMenu.add(exitItem); textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS); add(new JScrollPane(textArea), BorderLayout.CENTER); pack(); } /** * The Connect action pops up the password dialog. */ private class ConnectAction implements ActionListener { public void actionPerformed(ActionEvent event) { // if first time, construct dialog if (dialog == null) dialog = new PasswordChooser(); // 设置默认值 dialog.setUser(new User("yourname", null)); // 弹出对话 if (dialog.showDialog(DataExchangeFrame.this, "Connect")) { // 如果接受,检索用户输入 User u = dialog.getUser(); textArea.append("user name = " + u.getName() + ", password = " + (new String(u.getPassword())) + " "); } } } }
package dataExchange; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Frame; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingUtilities; /** *对话框中显示的密码选择器。 */ public class PasswordChooser extends JPanel { private JTextField username; private JPasswordField password; private JButton okButton; private boolean ok; private JDialog dialog; public PasswordChooser() { setLayout(new BorderLayout()); // 用用户名和密码字段构造一个面板 var panel = new JPanel(); panel.setLayout(new GridLayout(2, 2)); panel.add(new JLabel("User name:")); panel.add(username = new JTextField("")); panel.add(new JLabel("Password:")); panel.add(password = new JPasswordField("")); add(panel, BorderLayout.CENTER); // 创建终止对话框的Ok和Cancel按钮 okButton = new JButton("Ok"); okButton.addActionListener(event -> { ok = true; dialog.setVisible(false); }); var cancelButton = new JButton("Cancel"); cancelButton.addActionListener(event -> dialog.setVisible(false)); // 添加按钮到南部边界 var buttonPanel = new JPanel(); buttonPanel.add(okButton); buttonPanel.add(cancelButton); add(buttonPanel, BorderLayout.SOUTH); } /** * 设置对话框的默认值。 * @param u the default user information */ public void setUser(User u) { username.setText(u.getName()); } /** * 获取对话框条目。 * @return a User object whose state represents the dialog entries */ public User getUser() { return new User(username.getText(), password.getPassword()); } /** * 在对话框中显示选择面板。 * @param parent a component in the owner frame or null * @param title the dialog window title */ public boolean showDialog(Component parent, String title) { ok = false; // locate the owner frame Frame owner = null; if (parent instanceof Frame) owner = (Frame) parent; else owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); // 如果是第一次,或者如果所有者改变了,创建一个新的对话框 if (dialog == null || dialog.getOwner() != owner) { dialog = new JDialog(owner, true); dialog.add(this); dialog.getRootPane().setDefaultButton(okButton);//在对话框的根窗格中设置默认按钮 dialog.pack(); } // 设置标题和显示对话框 dialog.setTitle(title); dialog.setVisible(true); return ok; } }
package dataExchange; /** * A user has a name and password. For security reasons, the password is stored as a char[], not a * String. */ public class User { private String name; private char[] password; public User(String aName, char[] aPassword) { name = aName; password = aPassword; } public String getName() { return name; } public char[] getPassword() { return password; } public void setName(String aName) { name = aName; } public void setPassword(char[] aPassword) { password = aPassword; } }
运行结果:
测试程序5
l 在elipse IDE中调试运行教材556页程序12-19、12-20,结合运行结果理解程序;
l 掌握对话框的数据交换用法;
l 记录示例代码阅读理解中存在的问题与疑惑。
package dataExchange; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class DataExchangeTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new DataExchangeFrame(); frame.setTitle("DataExchangeTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package dataExchange; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A frame with a menu whose File->Connect action shows a password dialog. */ public class DataExchangeFrame extends JFrame { public static final int TEXT_ROWS = 20; public static final int TEXT_COLUMNS = 40; private PasswordChooser dialog = null; private JTextArea textArea; public DataExchangeFrame() { // 构造文件菜单 var mbar = new JMenuBar(); setJMenuBar(mbar); var fileMenu = new JMenu("File"); mbar.add(fileMenu); // 添加“连接”和“退出”菜单项 var connectItem = new JMenuItem("Connect"); connectItem.addActionListener(new ConnectAction()); fileMenu.add(connectItem); // 退出项退出程序 var exitItem = new JMenuItem("Exit"); exitItem.addActionListener(event -> System.exit(0)); fileMenu.add(exitItem); textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS); add(new JScrollPane(textArea), BorderLayout.CENTER); pack(); } /** * The Connect action pops up the password dialog. */ private class ConnectAction implements ActionListener { public void actionPerformed(ActionEvent event) { // if first time, construct dialog if (dialog == null) dialog = new PasswordChooser(); // 设置默认值 dialog.setUser(new User("yourname", null)); // 弹出对话 if (dialog.showDialog(DataExchangeFrame.this, "Connect")) { // 如果接受,检索用户输入 User u = dialog.getUser(); textArea.append("user name = " + u.getName() + ", password = " + (new String(u.getPassword())) + " "); } } } }
package dataExchange; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Frame; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingUtilities; /** *对话框中显示的密码选择器。 */ public class PasswordChooser extends JPanel { private JTextField username; private JPasswordField password; private JButton okButton; private boolean ok; private JDialog dialog; public PasswordChooser() { setLayout(new BorderLayout()); // 用用户名和密码字段构造一个面板 var panel = new JPanel(); panel.setLayout(new GridLayout(2, 2)); panel.add(new JLabel("User name:")); panel.add(username = new JTextField("")); panel.add(new JLabel("Password:")); panel.add(password = new JPasswordField("")); add(panel, BorderLayout.CENTER); // 创建终止对话框的Ok和Cancel按钮 okButton = new JButton("Ok"); okButton.addActionListener(event -> { ok = true; dialog.setVisible(false); }); var cancelButton = new JButton("Cancel"); cancelButton.addActionListener(event -> dialog.setVisible(false)); // 添加按钮到南部边界 var buttonPanel = new JPanel(); buttonPanel.add(okButton); buttonPanel.add(cancelButton); add(buttonPanel, BorderLayout.SOUTH); } /** * 设置对话框的默认值。 * @param u the default user information */ public void setUser(User u) { username.setText(u.getName()); } /** * 获取对话框条目。 * @return a User object whose state represents the dialog entries */ public User getUser() { return new User(username.getText(), password.getPassword()); } /** * 在对话框中显示选择面板。 * @param parent a component in the owner frame or null * @param title the dialog window title */ public boolean showDialog(Component parent, String title) { ok = false; // locate the owner frame Frame owner = null; if (parent instanceof Frame) owner = (Frame) parent; else owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); // 如果是第一次,或者如果所有者改变了,创建一个新的对话框 if (dialog == null || dialog.getOwner() != owner) { dialog = new JDialog(owner, true); dialog.add(this); dialog.getRootPane().setDefaultButton(okButton);//在对话框的根窗格中设置默认按钮 dialog.pack(); } // 设置标题和显示对话框 dialog.setTitle(title); dialog.setVisible(true); return ok; } }
package dataExchange; /** * A user has a name and password. For security reasons, the password is stored as a char[], not a * String. */ public class User { private String name; private char[] password; public User(String aName, char[] aPassword) { name = aName; password = aPassword; } public String getName() { return name; } public char[] getPassword() { return password; } public void setName(String aName) { name = aName; } public void setPassword(char[] aPassword) { password = aPassword; } }
运行结果:
测试程序6
l 在elipse IDE中调试运行教材564页程序12-21、12-22、12-23,结合程序运行结果理解程序;
l 掌握文件对话框的用法;
l 记录示例代码阅读理解中存在的问题与疑惑。
package fileChooser; import java.awt.*; import javax.swing.*; /** * @version 1.26 2018-04-10 * @author Cay Horstmann */ public class FileChooserTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { var frame = new ImageViewerFrame(); frame.setTitle("FileChooserTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package fileChooser; import java.io.*; import javax.swing.*; import javax.swing.filechooser.*; import javax.swing.filechooser.FileFilter; /** *一个文件视图,显示与文件过滤器匹配的所有文件的图标。 */ public class FileIconView extends FileView { private FileFilter filter; private Icon icon; /** * 构造一个FileIconView。 * @param aFilter a file filter--all files that this filter accepts will be shown * with the icon. * @param anIcon--the icon shown with all accepted files. */ public FileIconView(FileFilter aFilter, Icon anIcon) { filter = aFilter; icon = anIcon; } public Icon getIcon(File f) { if (!f.isDirectory() && filter.accept(f)) return icon; else return null; } }
package fileChooser; import java.awt.*; import java.io.*; import javax.swing.*; /** * A file chooser accessory that previews images. */ public class ImagePreviewer extends JLabel { /** * 构造一个ImagePreviewer。 * @param chooser the file chooser whose property changes trigger an image * change in this previewer */ public ImagePreviewer(JFileChooser chooser) { setPreferredSize(new Dimension(100, 100)); setBorder(BorderFactory.createEtchedBorder()); chooser.addPropertyChangeListener(event -> { if (event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) { // 用户选择了一个新文件 File f = (File) event.getNewValue(); if (f == null) { setIcon(null); return; } // 将图像读入一个图标 var icon = new ImageIcon(f.getPath()); // 如果图标太大,不适合,进行缩放 if (icon.getIconWidth() > getWidth()) icon = new ImageIcon(icon.getImage().getScaledInstance( getWidth(), -1, Image.SCALE_DEFAULT)); setIcon(icon); } }); } }
package fileChooser; import java.io.*; import javax.swing.*; import javax.swing.filechooser.*; import javax.swing.filechooser.FileFilter; /** * A frame that has a menu for loading an image and a display area for the * loaded image. */ public class ImageViewerFrame extends JFrame { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 400; private JLabel label; private JFileChooser chooser; public ImageViewerFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // 设置菜单栏 var menuBar = new JMenuBar(); setJMenuBar(menuBar); var menu = new JMenu("File"); menuBar.add(menu); var openItem = new JMenuItem("Open"); menu.add(openItem); openItem.addActionListener(event -> { chooser.setCurrentDirectory(new File("."));//调用setCurrentDirectory方法设置当前目录 // 显示文件选择器对话框 int result = chooser.showOpenDialog(ImageViewerFrame.this); // 如果图像文件被接受,将其设置为标签的图标 if (result == JFileChooser.APPROVE_OPTION) { String name = chooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(name)); pack(); } }); var exitItem = new JMenuItem("Exit"); menu.add(exitItem); exitItem.addActionListener(event -> System.exit(0)); // 使用标签来显示图像 label = new JLabel(); add(label); //设置文件选择器 chooser = new JFileChooser(); // accept all image files ending with .jpg, .jpeg, .gif var filter = new FileNameExtensionFilter( "Image files", "jpg", "jpeg", "gif"); chooser.setFileFilter(filter); chooser.setAccessory(new ImagePreviewer(chooser)); chooser.setFileView(new FileIconView(filter, new ImageIcon("palette.gif"))); //调用setFileView方法将对象安装到文件选择器对象中 } }
运行结果:
测试程序7
l 在elipse IDE中调试运行教材570页程序12-24,结合运行结果理解程序;
l 了解颜色选择器的用法。
l 记录示例代码阅读理解中存在的问题与疑惑。
package colorChooser; import java.awt.*; import javax.swing.*; /** * @version 1.04 2015-06-12 * @author Cay Horstmann */ public class ColorChooserTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new ColorChooserFrame(); frame.setTitle("ColorChooserTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package colorChooser; import java.awt.Color; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JDialog; import javax.swing.JPanel; /** * 一个带有按钮的面板,可以弹出三种颜色选择器 */ public class ColorChooserPanel extends JPanel { public ColorChooserPanel() { JButton modalButton = new JButton("Modal"); modalButton.addActionListener(new ModalListener()); add(modalButton); JButton modelessButton = new JButton("Modeless"); modelessButton.addActionListener(new ModelessListener()); add(modelessButton); JButton immediateButton = new JButton("Immediate"); immediateButton.addActionListener(new ImmediateListener()); add(immediateButton); } /** * 这个监听器弹出一个模式颜色选择器 */ private class ModalListener implements ActionListener { public void actionPerformed(ActionEvent event) { Color defaultColor = getBackground(); Color selected = JColorChooser.showDialog(ColorChooserPanel.this, "Set background", defaultColor);//利用颜色选择器显示模式对话框 if (selected != null) setBackground(selected); } } /** * This listener pops up a modeless color chooser. The panel color is changed when the user * clicks the OK button. */ private class ModelessListener implements ActionListener { private JDialog dialog; private JColorChooser chooser; //创建一个无模式对话框。当用户按下OK键时,对话框的背景颜色就会被设置成所选择的颜色 public ModelessListener() { chooser = new JColorChooser(); dialog = JColorChooser.createDialog(ColorChooserPanel.this, "Background Color", false /*非模态 */, chooser, event -> setBackground(chooser.getColor()), null /* 无取消按钮监听器 */); } public void actionPerformed(ActionEvent event) { chooser.setColor(getBackground()); dialog.setVisible(true); } } /** * This listener pops up a modeless color chooser. The panel color is changed immediately when * the user picks a new color. */ private class ImmediateListener implements ActionListener { private JDialog dialog; private JColorChooser chooser; public ImmediateListener() { chooser = new JColorChooser(); chooser.getSelectionModel().addChangeListener( //监听颜色的选择,获得选择器的选择模型并添加改变监听器,将颜色选择反馈给用户 event -> setBackground(chooser.getColor())); //将颜色选择器组件直接添加到一个无模式对话框中 dialog = new JDialog((Frame) null, false /* 非模态 */); dialog.add(chooser); dialog.pack(); } public void actionPerformed(ActionEvent event) { chooser.setColor(getBackground()); dialog.setVisible(true); } } }
package colorChooser; import javax.swing.*; /** * A frame with a color chooser panel */ public class ColorChooserFrame extends JFrame { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public ColorChooserFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); //添加颜色选择面板到框架 ColorChooserPanel panel = new ColorChooserPanel(); add(panel); } }
运行结果:
实验总结:
本周学习了菜单组件的用途及常用的API,对话框组件用途及常用API,学习了设计简单应用程序的GUI,在学习了十一章的基础上,通过本章的学习,对用户界面有了更加复杂的设计 ,使得用户界面更加的美观,功能更加丰富。