Ex5_1.java
练习编写实现简单的文字变换功能的图形界面。
// Ex5_1.java /** * 题目要求:练习编写实现简单的文字变换功能的图形界面。 * * 设计思路: * 1. 从布局上看:该窗口共分上中下三层, * 第一层,包含组件一个JTextArea和一个JButton, * 第二层,包含组件三个JRadioButton和两个JCheckBox和一个JComboBox, * 第三层,只包含一组件JTextArea. * 2. 从功能实现上看, * 从JTextArea中输入文字,然后,按下JButton,此时,程序响应事件并将 * JTextArea中的内容复制至最下层的JTextArea中,随后,可以使用第二层的 * JRadioButton和JCheckBox和JComboBox对最下层的JTextArea中的字体调整。 * 3. 程序设计: * 首先,定义三个布局变量,用于管理每一层的组件布局, * 然后,给每个组件布局中的组件初始化, * 最后,创建内嵌类fontFormatHandler用于监听事件。 * **/ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class Ex5_1 extends JFrame { // 布局 private JPanel topFrame; private JPanel midFrame; private JPanel wholeFrame; // 最上方组件 private JTextArea textArea; private JButton enterButton; // 中间组件 private JRadioButton songFont; private JRadioButton diFont; private JRadioButton blackFont; private ButtonGroup buttonGroup; private JCheckBox boldFont; private JCheckBox italicFont; private String [] pointFontString = new String[]{"16", "18", "20", "24", "26", "28", "30"}; private JComboBox pointFont; // 下方组件 private JTextArea showArea; // 构造函数 public Ex5_1(String name){ super(name); topFrameConstruction(); midFrameConstruction(); downFrameConstruction(); setBounds(100, 100, 450, 360); setFrame(); setVisible(true); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 布局设定 private void setFrame(){ topFrame = new JPanel(); topFrame.setLayout(null); textArea.setBounds(15, 10, 280, 50); topFrame.add(textArea); enterButton.setBounds(330, 15, 70, 40); topFrame.add(enterButton); enterButton.setHorizontalAlignment(SwingConstants.CENTER); midFrame = new JPanel(); midFrame.setLayout(new GridLayout(1, 6, 3, 3)); midFrame.add(songFont); midFrame.add(diFont); midFrame.add(blackFont); midFrame.add(boldFont); midFrame.add(italicFont); midFrame.add(pointFont); wholeFrame = new JPanel(); wholeFrame.setLayout(null); topFrame.setBounds(0, 0, 420, 70); wholeFrame.add(topFrame); midFrame.setBounds(5, 75, 420, 20); wholeFrame.add(midFrame); showArea.setBounds(30, 120, 350, 180); wholeFrame.add(showArea); Container c = getContentPane(); c.setLayout(new GridLayout(1, 1)); c.add(wholeFrame); return; } // 最上层布局及相关组件初始化 private void topFrameConstruction(){ textArea = new JTextArea(); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); enterButton = new JButton("输入"); enterHandler h = new enterHandler(); // 创建临听器 enterButton.addActionListener(h); return; } // 中间的布局及相关的组件初始化 private void midFrameConstruction(){ songFont = new JRadioButton("宋体", true); diFont = new JRadioButton("隶书"); blackFont = new JRadioButton("黑体"); buttonGroup = new ButtonGroup(); buttonGroup.add(songFont); buttonGroup.add(diFont); buttonGroup.add(blackFont); boldFont = new JCheckBox("粗体"); italicFont = new JCheckBox("斜体"); pointFont = new JComboBox(pointFontString); pointFont.setSelectedIndex(0); fontFormatHandler h = new fontFormatHandler(); // 创建监听器 songFont.addItemListener(h); diFont.addItemListener(h); blackFont.addItemListener(h); boldFont.addItemListener(h); italicFont.addItemListener(h); pointFont.addItemListener(h); return; } // 下方的布局及相关的组件初始化 private void downFrameConstruction(){ showArea = new JTextArea(); showArea.setEditable(false); showArea.setLineWrap(true); showArea.setWrapStyleWord(true); showArea.setBackground(Color.green); return; } // 设定两种监听器事件类 private class enterHandler implements ActionListener { public void actionPerformed(ActionEvent event){ showArea.setText(textArea.getText()); return; } } private class fontFormatHandler implements ItemListener { public void itemStateChanged(ItemEvent event){ // 字体类型设置 String FontName = showArea.getFont().getFontName(); if (songFont.isSelected()){ diFont.setSelected(false); blackFont.setSelected(false); songFont.setSelected(true); FontName = "宋体"; } else if(diFont.isSelected()){ songFont.setSelected(false); blackFont.setSelected(false); diFont.setSelected(true); FontName = "隶书"; } else if(blackFont.isSelected()){ songFont.setSelected(false); diFont.setSelected(false); blackFont.setSelected(true); FontName = "黑体"; } // 字体风格设置 if(event.getSource() == boldFont){ boldFont.setBorderPaintedFlat(!boldFont.isBorderPaintedFlat()); } if(event.getSource() == italicFont){ italicFont.setBorderPaintedFlat(!italicFont.isBorderPaintedFlat()); } int fontStyle = Font.PLAIN; if (boldFont.isBorderPaintedFlat()){fontStyle += Font.BOLD;} if (italicFont.isBorderPaintedFlat()){fontStyle += Font.ITALIC;} // 字体大小设置 int fontPoint = Integer.parseInt(pointFontString[pointFont.getSelectedIndex()]); showArea.setFont( new Font(FontName, fontStyle, fontPoint) ); return; } } // 主函数 public static void main(String[] args) { Ex5_1 test = new Ex5_1("字体变换窗口演示"); return; } } // end-class
Ex5_2.java
设计一个简单的计算器。
初步UML设计图:
源代码:
// Ex5_2.java /** * 题目要求:设计一个简单的计算器。 **/ // 注:程序尚未完善,功能未完成,只完成了布局 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class Ex5_2 extends JFrame { // 与运算相关的变量 public String NumberA; public String NumberB; public String calculateHistory; public String calculateResult; // 布局 private JPanel topFrame; private JPanel downFrame; // 显示文本框组件 private JTextArea cHistory; private JTextArea cResult; // 按钮组件 private JButton num1; private JButton num2; private JButton num3; private JButton num4; private JButton num5; private JButton num6; private JButton num7; private JButton num8; private JButton num9; private JButton num0; private JButton operCancel; private JButton operClear; private JButton operEqual; private JButton operPlus; private JButton operSub; private JButton operMul; private JButton operDiv; private JButton operSqrt; private JButton operSign; private JButton operPoint; // 构造函数 public Ex5_2(String name){ super(name); buttonConstruction(); buttonFunction(); setBounds(100, 100, 330, 250); setFrame(); setVisible(true); setResizable(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 布局设定 private void setFrame(){ topFrame = new JPanel( new GridLayout(2, 1, 0, 0) ); topFrame.add(cHistory); topFrame.add(cResult); downFrame = new JPanel( new GridLayout(4, 5, 5, 5) ); downFrame.add(num7); downFrame.add(num8); downFrame.add(num9); downFrame.add(operDiv); downFrame.add(operCancel); downFrame.add(num4); downFrame.add(num5); downFrame.add(num6); downFrame.add(operMul); downFrame.add(operClear); downFrame.add(num1); downFrame.add(num2); downFrame.add(num3); downFrame.add(operSub); downFrame.add(operSqrt); downFrame.add(num0); downFrame.add(operSign); downFrame.add(operPoint); downFrame.add(operPlus); downFrame.add(operEqual); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add("North", topFrame); c.add("Center", downFrame); return; } // 组件初始化 private void buttonConstruction(){ cHistory = new JTextArea(); cHistory.setEditable(false); cResult = new JTextArea(); cResult.setEditable(false); num1 = new JButton("1"); num2 = new JButton("2"); num3 = new JButton("3"); num4 = new JButton("4"); num5 = new JButton("5"); num6 = new JButton("6"); num7 = new JButton("7"); num8 = new JButton("8"); num9 = new JButton("9"); num0 = new JButton("0"); operCancel = new JButton("C"); operClear = new JButton("CE"); operEqual = new JButton("="); operPlus = new JButton("+"); operSub = new JButton("-"); operMul = new JButton("*"); operDiv = new JButton("/"); operSqrt = new JButton("Sqrt"); operSign = new JButton("-/+"); operPoint = new JButton("."); return; } // 组件功能设置 private void buttonFunction(){ return; } // 设定监听器事件类 private class buttonItemListener implements ActionListener { public void actionPerformed(ActionEvent event){ return; } } // 主函数 public static void main(String[] args) { Ex5_2 test = new Ex5_2("简单的计算器"); return; } } // end-class