private JButton sum_button, average_button, clear_button; private JPanel panelNorth, panelSouth;
public Work5_9() { result_label = new JLabel(\请选择你的操作\); showResult_textField = new JTextField(12); input_textArea = new JTextArea(); sum_button = new JButton(\求 和\); average_button = new JButton(\平均值\); clear_button = new JButton(\清空\); panelNorth = new JPanel(); panelSouth = new JPanel(); sum_button.addActionListener(this); average_button.addActionListener(this); clear_button.addActionListener(this); panelNorth.add(sum_button); panelNorth.add(average_button); panelSouth.add(result_label); panelSouth.add(showResult_textField); panelSouth.add(clear_button); this.add(panelNorth, BorderLayout.NORTH); this.add(new JScrollPane(input_textArea), BorderLayout.CENTER); this.add(panelSouth, BorderLayout.SOUTH); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); this.setBounds(100, 100, 350, 200); this.setVisible(true); this.validate(); }
public void actionPerformed(ActionEvent e) { if (e.getSource() == clear_button) { input_textArea.setText(\); showResult_textField.setText(\); result_label.setText(\请选择你的操作\); } int number_array[] = getNumber_array(); if (e.getSource() == sum_button) { result_label.setText(\ 输入数的和\); int sum = 0; for (int i = 0; i < number_array.length; i++) sum = sum + number_array[i]; showResult_textField.setText(\ + sum); } if (e.getSource() == average_button) { result_label.setText(\输入数的平均值\); int sum = 0; for (int i = 0; i < number_array.length; i++) sum = sum + number_array[i]; double average = 1.0 * sum / (number_array.length);
46
showResult_textField.setText(\ + average); } } public int[] getNumber_array() { int numArray[] = null; try { String s = input_textArea.getText(); String temp = new String(); StringTokenizer t = new StringTokenizer(s, \\); int n = t.countTokens(); numArray = new int[n]; for (int i = 0; i < n; i++) { temp = t.nextToken(); numArray[i] = Integer.parseInt(temp); } } catch( NumberFormatException e ) { JOptionPane.showMessageDialog(null, \请确认输入的是数字字符\\n\, \错误警告\, JOptionPane.ERROR_MESSAGE); } return numArray; } public static void main(String args[]) { new Work5_9(); } }
5.10 布局设计
程序运行结果:
源文件:Work5_10.java
import java.awt.*; import javax.swing.*; /**
* @author 段智敏 */
class Work5_10 extends JFrame { private static final long serialVersionUID = 1L; private JPanel panel2, panel3, panel4, panel6;// 整体布局部件 private JTextArea jsp1_Text, jsp5_Text; private JLabel panel2_Label; private JTextField panel2_Field;
47
private JPanel panel3_p1, panel3_p2, panel4_p1, panel4_p2, panel4_p3, panel6_p1, panel6_p2, panel6_p3; private JButton panel4_b1, panel4_b2; Work5_10() { jsp1_Text = new JTextArea(\文本区(带滚动条)\); JScrollPane jsp1 = new JScrollPane(jsp1_Text);// 第一个 panel2 = new JPanel(); panel2_Label = new JLabel(\标签\); panel2_Field = new JTextField(\文本框\); panel3 = new JPanel(); panel3_p1 = new JPanel(); panel3_p2 = new JPanel(); panel4 = new JPanel(); panel4_b1 = new JButton(\按钮\); panel4_b2 = new JButton(\按钮\); panel4_p1 = new JPanel(); panel4_p2 = new JPanel(); panel4_p3 = new JPanel(); jsp5_Text = new JTextArea(\文本区(带滚动条)\); JScrollPane jsp5 = new JScrollPane(jsp5_Text); panel6 = new JPanel(); panel6_p1 = new JPanel(); panel6_p2 = new JPanel(); panel6_p3 = new JPanel(); // 设置滚动条为一直可见 jsp1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jsp1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); jsp5.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jsp5.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); panel2.setLayout(new GridLayout(2, 1));//第二个面板表格布局,2行1列 panel2.add(panel2_Label); panel2.add(panel2_Field); panel3.setLayout(new GridLayout(1, 2));//第三个面板表格布局,1行2列 panel3_p1.setBackground(Color.CYAN); panel3_p2.setBackground(Color.RED); panel3.add(panel3_p1); panel3.add(panel3_p2); panel4.setLayout(new GridLayout(1, 3));//第四个面板表格布局,1行3列 panel4_p1.setLayout(new GridLayout(2, 1));//第四个面板中的第一个面板,2行1列 panel4_p1.add(panel4_b1);
48
panel4_p1.add(panel4_b2); panel4_p2.setBackground(Color.green); panel4_p3.setBackground(Color.YELLOW); panel4.add(panel4_p1); panel4.add(panel4_p2); panel4.add(panel4_p3); panel6.setLayout(new GridLayout(1, 3));//第六个面板表格布局,1行3列 panel6_p1.setBackground(Color.PINK); panel6_p2.setBackground(Color.BLUE); panel6_p3.setBackground(Color.orange); panel6.add(panel6_p1); panel6.add(panel6_p2); panel6.add(panel6_p3); this.setLayout(new GridLayout(2, 3));// 设置整体布局为表格布局,2行3列 this.add(jsp1); this.add(panel2); this.add(panel3); this.add(panel4); this.add(jsp5); this.add(panel6); this.validate(); this.setSize(600, 120); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String args[]) { new Work5_10(); } }
49
第六章 图形界面设计
6.1 设计一个面板,该面板中有四个运动项目选择框和一个文本区。当某个选择项目被选中时,在文本区中显示该选择项目。
程序运行结果:
源文件:Work6_1.java
import javax.swing.*; import java.awt.*;
import java.awt.event.*; /**
* @author 段智敏 */
public class Work6_1 extends JFrame { private static final long serialVersionUID = 1L; private MyPanel6_1 panel;// 此面板 public Work6_1() { panel = new MyPanel6_1(); this.add(panel); this.setBounds(100, 100, 400, 150); this.setVisible(true); this.validate(); this.addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent e) { System.exit(0); } }); } public static void main(String args[]) { new Work6_1(); } }
面板类源文件:MyPanel6_1.java
50