201874040116-李鑫《面向对象程序设计(java)》第十四周学习总结
内容 |
|
这个作业属于哪个课程 |
https://www.cnblogs.com/nwnu-daizh/ |
这个作业的要求在哪里 |
https://www.cnblogs.com/nwnu-daizh/p/11953993.html |
作业学习目标 |
(1)掌握GUI布局管理器用法; (2)掌握Java Swing文本输入组件用途及常用API; (3)掌握Java Swing选择输入组件用途及常用API。 |
第一部分:总结第十二章本周理论知识
设计模式: 一种以结构化的方式展示专家们的心血的方法。
“模型-视图-控制器”模式: Swing框架 中最具影响力的设计模式。
布局管理器:控制容器中组件的位置与大小
1)边框布局(border layout manager):JFrame的默认管理器。
2) 流式布局(FlowLayout):从第一行开始,从左向右依次排列,碰到边界时转到下一行继续。
3)网格布局(GridLayout):按行排列,每个单元都相同大小。
第二部分:实验部分
实验1:测试程序1
package calculator; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class Calculator { public static void main(String[] args) { EventQueue.invokeLater(() -> { CalculatorFrame frame = new CalculatorFrame(); frame.setTitle("Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package calculator; import javax.swing.*; /** * A frame with a calculator panel. */ public class CalculatorFrame extends JFrame { public CalculatorFrame() { add(new CalculatorPanel()); pack(); } }
package calculator; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A panel with calculator buttons and a result display. */ public class CalculatorPanel extends JPanel { private JButton display; private JPanel panel; private double result; private String lastCommand; private boolean start; public CalculatorPanel() { setLayout(new BorderLayout()); //构造一个边框布局管理器对象 result = 0; lastCommand = "="; start = true; // add the display display = new JButton("0"); display.setEnabled(false); add(display, BorderLayout.NORTH); ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); // add the buttons in a 4 x 4 grid panel = new JPanel(); panel.setLayout(new GridLayout(4, 4));//构造一个4x4网格布局管理器对象并添加按钮 addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command); addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command); addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command); addButton("0", insert); addButton(".", insert); addButton("=", command); addButton("+", command); add(panel, BorderLayout.CENTER); } /** * Adds a button to the center panel. * @param label the button label * @param listener the button listener */ private void addButton(String label, ActionListener listener) { JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); } /** * This action inserts the button action string to the end of the display text. */ private class InsertAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start) { display.setText(""); start = false; } display.setText(display.getText() + input); } } /** * This action executes the command that the button action string denotes. */ private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (start) { if (command.equals("-")) { display.setText(command); start = false; } else lastCommand = command; } else { calculate(Double.parseDouble(display.getText())); lastCommand = command; start = true; } } } /** * Carries out the pending calculation. * @param x the value to be accumulated with the prior result. */ public void calculate(double x) { if (lastCommand.equals("+")) result += x; else if (lastCommand.equals("-")) result -= x; else if (lastCommand.equals("*")) result *= x; else if (lastCommand.equals("/")) result /= x; else if (lastCommand.equals("=")) result = x; display.setText("" + result); } }
测试程序2
package text; import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingConstants; /** * A frame with sample text components. */ public class TextComponentFrame extends JFrame { public static final int TEXTAREA_ROWS = 8; public static final int TEXTAREA_COLUMNS = 20; public TextComponentFrame() { JTextField textField = new JTextField(); JPasswordField passwordField = new JPasswordField(); JPanel northPanel = new JPanel(); northPanel.setLayout(new GridLayout(2, 2)); //构造一个2x2网格布局管理器对象 northPanel.add(new JLabel("User name: ", SwingConstants.RIGHT)); northPanel.add(textField); northPanel.add(new JLabel("Password: ", SwingConstants.RIGHT)); northPanel.add(passwordField); add(northPanel, BorderLayout.NORTH); JTextArea textArea = new JTextArea(TEXTAREA_ROWS, TEXTAREA_COLUMNS); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); // add button to append text into the text area JPanel southPanel = new JPanel(); JButton insertButton = new JButton("Insert"); southPanel.add(insertButton); insertButton.addActionListener(event -> textArea.append("User name: " + textField.getText() + " Password: " + new String(passwordField.getPassword()) + "\n")); add(southPanel, BorderLayout.SOUTH); pack(); } }
package text; import java.awt.*; import javax.swing.*; /** * @version 1.42 2018-04-10 * @author Cay Horstmann */ public class TextComponentTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new TextComponentFrame(); frame.setTitle("TextComponentTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
测试程序3
package checkBox; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A frame with a sample text label and check boxes for selecting font * attributes. */ public class CheckBoxFrame extends JFrame { private JLabel label; private JCheckBox bold; private JCheckBox italic; private static final int FONTSIZE = 24; public CheckBoxFrame() { // add the sample text label label = new JLabel("The quick brown fox jumps over the lazy dog."); label.setFont(new Font("Serif", Font.BOLD, FONTSIZE)); add(label, BorderLayout.CENTER); // this listener sets the font attribute of // the label to the check box state //查询复选框的状态并对字体格式进行调整 ActionListener listener = event -> { int mode = 0; if (bold.isSelected()) mode += Font.BOLD; if (italic.isSelected()) mode += Font.ITALIC; label.setFont(new Font("Serif", mode, FONTSIZE)); }; // add the check boxes //添加复选框 JPanel buttonPanel = new JPanel(); bold = new JCheckBox("Bold"); bold.addActionListener(listener); bold.setSelected(true); buttonPanel.add(bold); italic = new JCheckBox("Italic"); italic.addActionListener(listener); buttonPanel.add(italic); add(buttonPanel, BorderLayout.SOUTH); pack(); } }
package checkBox; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class CheckBoxTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { CheckBoxFrame frame = new CheckBoxFrame(); frame.setTitle("CheckBoxTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
测试程序4
package radioButton; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A frame with a sample text label and radio buttons for selecting font sizes. */ public class RadioButtonFrame extends JFrame { private JPanel buttonPanel; private ButtonGroup group; private JLabel label; private static final int DEFAULT_SIZE = 36; public RadioButtonFrame() { // add the sample text label label = new JLabel("The quick brown fox jumps over the lazy dog."); label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE)); add(label, BorderLayout.CENTER); // add the radio buttons buttonPanel = new JPanel(); group = new ButtonGroup(); addRadioButton("Small", 8); addRadioButton("Medium", 12); addRadioButton("Large", 18); addRadioButton("Extra large", 36); add(buttonPanel, BorderLayout.SOUTH); pack(); } /** * Adds a radio button that sets the font size of the sample text. * @param name the string to appear on the button * @param size the font size that this button sets */ public void addRadioButton(String name, int size) { boolean selected = size == DEFAULT_SIZE; JRadioButton button = new JRadioButton(name, selected); group.add(button); buttonPanel.add(button); // this listener sets the label font size //改变标签格式的监听器 ActionListener listener = event -> label.setFont(new Font("Serif", Font.PLAIN, size)); button.addActionListener(listener); } }
package radioButton; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class RadioButtonTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new RadioButtonFrame(); frame.setTitle("RadioButtonTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
测试程序5
package border; import java.awt.*; import javax.swing.*; import javax.swing.border.*; /** * A frame with radio buttons to pick a border style. */ public class BorderFrame extends JFrame { private JPanel demoPanel; private JPanel buttonPanel; private ButtonGroup group; public BorderFrame() { demoPanel = new JPanel(); buttonPanel = new JPanel(); group = new ButtonGroup(); //单选项设置 addRadioButton("Lowered bevel", BorderFactory.createLoweredBevelBorder()); addRadioButton("Raised bevel", BorderFactory.createRaisedBevelBorder()); addRadioButton("Etched", BorderFactory.createEtchedBorder()); addRadioButton("Line", BorderFactory.createLineBorder(Color.BLUE)); addRadioButton("Matte", BorderFactory.createMatteBorder(10, 10, 10, 10, Color.BLUE)); addRadioButton("Empty", BorderFactory.createEmptyBorder()); Border etched = BorderFactory.createEtchedBorder(); Border titled = BorderFactory.createTitledBorder(etched, "Border types"); buttonPanel.setBorder(titled); setLayout(new GridLayout(2, 1)); //布局管理器设置 add(buttonPanel); add(demoPanel); pack(); } public void addRadioButton(String buttonName, Border b) { JRadioButton button = new JRadioButton(buttonName); button.addActionListener(event -> demoPanel.setBorder(b)); group.add(button); buttonPanel.add(button); } }
package border; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class BorderTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { BorderFrame frame = new BorderFrame(); frame.setTitle("BorderTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
测试程序6
package comboBox; import java.awt.BorderLayout; import java.awt.Font; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * A frame with a sample text label and a combo box for selecting font faces. */ public class ComboBoxFrame extends JFrame { private JComboBox<String> faceCombo; private JLabel label; private static final int DEFAULT_SIZE = 24; public ComboBoxFrame() { // add the sample text label label = new JLabel("The quick brown fox jumps over the lazy dog."); label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE)); add(label, BorderLayout.CENTER); // make a combo box and add face names //设置组合框 faceCombo = new JComboBox<>(); faceCombo.addItem("Serif"); faceCombo.addItem("SansSerif"); faceCombo.addItem("Monospaced"); faceCombo.addItem("Dialog"); faceCombo.addItem("DialogInput"); // the combo box listener changes the label font to the selected face name //根据选择的选项改变字体格式 faceCombo.addActionListener(event -> label.setFont( new Font(faceCombo.getItemAt(faceCombo.getSelectedIndex()), Font.PLAIN, DEFAULT_SIZE))); // add combo box to a panel at the frame's southern border JPanel comboPanel = new JPanel(); comboPanel.add(faceCombo); add(comboPanel, BorderLayout.SOUTH); pack(); } }
package comboBox; import java.awt.*; import javax.swing.*; /** * @version 1.36 2018-04-10 * @author Cay Horstmann */ public class ComboBoxTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { ComboBoxFrame frame = new ComboBoxFrame(); frame.setTitle("ComboBoxTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
实验2:结对编程练习
1) 程序设计思路简述;
使用两个类。 进行框架对象构造的 主类 InputTable 类,进行布局管理与功能实现的 继承自JFrame类的 NFrame类。
NFrame: 使用两个面板组件
2) 符合编程规范的程序代码;
1 package paratice; 2 3 import java.awt.EventQueue; 4 5 import javax.swing.JFrame; 6 7 public class InputTable { 8 9 public static void main(String[] args) { 10 11 EventQueue.invokeLater(() -> { 12 JFrame frame = new NFrame(); 13 frame.setTitle("NFrame"); 14 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15 frame.setVisible(true); 16 }); 17 18 } 19 20 }
1 package paratice; 2 3 import java.awt.FlowLayout; 4 import java.awt.GridLayout; 5 import java.util.Enumeration; 6 7 import javax.swing.AbstractButton; 8 import javax.swing.BorderFactory; 9 import javax.swing.ButtonGroup; 10 import javax.swing.JButton; 11 import javax.swing.JCheckBox; 12 import javax.swing.JFrame; 13 import javax.swing.JLabel; 14 import javax.swing.JPanel; 15 import javax.swing.JRadioButton; 16 import javax.swing.JScrollPane; 17 import javax.swing.JTextArea; 18 import javax.swing.JTextField; 19 20 public class NFrame extends JFrame{ 21 22 public static final int TEXTAREA_ROWS = 6; 23 public static final int TEXTAREA_COLUMNS = 50; 24 25 String cbString = " 喜好:"; 26 27 NButtonGroup rbGroup = new NButtonGroup(); 28 JPanel Npanel = new JPanel(); 29 JPanel Spanel = new JPanel(); 30 JPanel rbpanel = new JPanel(); 31 JPanel cbpanel = new JPanel(); 32 JPanel rcbpanel = new JPanel(); 33 JPanel irbPanel = new JPanel(); 34 35 public NFrame(){ 36 37 setLayout(new GridLayout(4,1,0,20)); 38 setBounds(400, 300, 100, 100);; 39 40 rbpanel.setBorder(BorderFactory.createTitledBorder("性别")); 41 cbpanel.setBorder(BorderFactory.createTitledBorder("爱好")); 42 43 //姓名,地址输入框 44 JTextField textName = new JTextField(8); 45 JTextField textAddres = new JTextField(12); 46 47 //性别单选 48 addRadioButton("男"); 49 addRadioButton("女"); 50 51 //喜好多选 52 addJCheckBox("阅读"); 53 addJCheckBox("唱歌"); 54 addJCheckBox("跳舞"); 55 56 //录入信息显示区 57 JTextArea textArea = new JTextArea(TEXTAREA_ROWS, TEXTAREA_COLUMNS); 58 textArea.setEditable(false); 59 JScrollPane scrollPane = new JScrollPane(textArea); 60 61 //提交和重置 62 JButton insertButton = new JButton("提交"); 63 64 insertButton.addActionListener(event ->{ 65 if(!(textName.getText().equalsIgnoreCase("")||textAddres.getText().equalsIgnoreCase("")||rbGroup.toString().equalsIgnoreCase(""))) 66 { 67 textArea.append("姓名: " + textName.getText() + " 地址: " + textAddres.getText() + rbGroup.toString() + cbString +"\n");} 68 else { 69 70 }}); 71 72 JButton cleverButton = new JButton("重置"); 73 cleverButton.addActionListener(event ->{ 74 textArea.setText("录入信息显示区!\n"); 75 textName.setText(""); 76 textAddres.setText(""); 77 rbGroup.clearSelection(); 78 }); 79 textArea.append("录入信息显示区!\n"); 80 81 //布局设置 82 Npanel.setLayout(new FlowLayout(FlowLayout.CENTER,0,0)); 83 Npanel.add(new JLabel("姓名: ")); 84 Npanel.add(textName); 85 Npanel.add(new JLabel("地址: ")); 86 Npanel.add(textAddres); 87 88 add(Npanel); 89 90 rcbpanel.add(rbpanel); 91 rcbpanel.add(cbpanel); 92 93 add(rcbpanel); 94 95 irbPanel.add(insertButton); 96 irbPanel.add(cleverButton); 97 add(irbPanel); 98 99 add(scrollPane); 100 101 pack(); 102 } 103 104 private void addRadioButton(String ButtonName) { 105 JRadioButton button = new JRadioButton(ButtonName); 106 rbGroup.add(button); 107 rbpanel.add(button); 108 } 109 110 private void addJCheckBox(String ButtonName) { 111 JCheckBox button = new JCheckBox(ButtonName); 112 button.addActionListener(event->{ 113 if(button.isSelected()) { 114 cbString += " "+button.getText(); 115 } 116 else { 117 cbString = cbString.replaceAll(" "+button.getText(),""); 118 } 119 }); 120 cbpanel.add(button); 121 } 122 123 private static class NButtonGroup extends ButtonGroup{ 124 125 public String toString() { 126 String model = ""; 127 //获取被选中的项的Text 128 Enumeration<AbstractButton> modelBtns = getElements(); 129 while (modelBtns.hasMoreElements()) { 130 AbstractButton modelbtn = modelBtns.nextElement(); 131 if(modelbtn.isSelected()) { 132 model+=" 性别:"+modelbtn.getText()+" "; 133 return model; 134 } 135 } 136 return model; 137 } 138 } 139 }
3) 程序运行功能界面截图;