運行初始狀態:
計算結果如下:
代碼如下:
package jisuanqi; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; class counter1 extends JFrame { public counter1() { super("計算器"); this.setSize(400,100); this.setLocation(300,240); this.setLayout(new FlowLayout()); //確定窗口格式為流輸入 TextField text1=new TextField(4); text1.setText("1"); this.add(text1); String proList[] = { "+","-","x" ,"%"}; //將"+","-","x" ,"%"作為列表元素 TextField text; JComboBox comboBox; //創建下拉復選框 Container conPane = getContentPane(); //創建一個名為conPane的容器 comboBox = new JComboBox(proList); //把列表元素加入下拉復選框 comboBox.setEditable(true); //使復選框變為可操作 conPane.add(comboBox); //將復選框加入容器中 TextField text2=new TextField(4); text2.setText("1"); this.add(text2); JButton button = new JButton("="); this.add(button); TextField text3=new TextField(4); text3.setText("2"); button.addActionListener(new ActionListener(){ //添加按鈕監聽事件 public void actionPerformed(ActionEvent e) //創建事件響應函數 { String s=comboBox.getEditor().getItem().toString(); //獲取復選框中的元素 double a= Integer.parseInt(text1.getText()); //將兩個文本框中的字符串強制轉換成浮點型,以便於之后的計算 double b= Integer.parseInt(text2.getText()); if(s.equals("+")) { //判斷復選框中的元素種類 double t=a+b; String m=String.valueOf(t); //由於文本框中的數據流只能為字符串,這里就需要將計算得到的浮點型數據強制轉換成字符串型 text3.setText(m); //將最后的結果放在文本框中 } else if(s.equals("-")) //后面的與之前的類似,不在注釋 {double t=a-b; String m=String.valueOf(t); text3.setText(m);} else if(s.equals("x")) {double t=a*b; String m=String.valueOf(t); text3.setText(m);} else {double t=a/b; String m=String.valueOf(t); text3.setText(m);} }}); conPane.add(text3); this.setVisible(true); //將窗口設置為可視化 } } public class Counter { public static void main(String[] args) { new counter1(); } }
修改版如下:
源碼如下:
package jisuanqi; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; class counter1 extends JFrame { public counter1() { super("計算器"); this.setSize(400,130); this.setLocation(300,240); this.setLayout(new FlowLayout()); //確定窗口格式為流輸入 TextField text1=new TextField(4); text1.setText("1"); JLabel label1 = new JLabel(""); text1.addFocusListener(new FocusListener() { //添加焦點事件監聽器 public void focusGained(FocusEvent arg0) { //設置獲得焦點時的事件 label1.setText("正在輸入中..."); } public void focusLost(FocusEvent arg0) { //設置失去焦點時的事件 if(!text1.getText().matches("\\d+")) //使用正則表達式判斷該字符串是否為數字,第一個\是轉義符,\d+表示匹配1個或 //多個連續數字,"+"和"*"類似,"*"表示0個或多個 label1.setText("輸入的第一個數據有誤"); else label1.setText(""); } }); this.add(text1); String proList[] = { "+","-","x" ,"%"}; //將"+","-","x" ,"%"作為列表元素 TextField text; JComboBox comboBox; //創建下拉復選框 Container conPane = getContentPane(); //創建一個名為conPane的容器 comboBox = new JComboBox(proList); //把列表元素加入下拉復選框 comboBox.setEditable(true); //使復選框變為可操作 conPane.add(comboBox); //將復選框加入容器中 TextField text2=new TextField(4); text2.setText("1"); this.add(text2); text2.addFocusListener(new FocusListener() { public void focusGained(FocusEvent arg0) { label1.setText("正在輸入中..."); } public void focusLost(FocusEvent arg0) { if(!text2.getText().matches("\\d+")) label1.setText("輸入的第二個數據有誤"); else label1.setText(""); } }); JButton button = new JButton("="); this.add(button); TextField text3=new TextField(4); text3.setText("2"); button.addActionListener(new ActionListener(){ //添加按鈕監聽事件 public void actionPerformed(ActionEvent e) //創建事件響應函數 { String s=comboBox.getEditor().getItem().toString(); //獲取復選框中的元素 double a= Integer.parseInt(text1.getText()); //將兩個文本框中的字符串強制轉換成浮點型,以便於之后的計算 double b= Integer.parseInt(text2.getText()); if(s.equals("+")) { //判斷復選框中的元素種類 double t=a+b; String m=String.valueOf(t); //由於文本框中的數據流只能為字符串,這里就需要將計算得到的浮點型數據強制轉換成字符串型 text3.setText(m); //將最后的結果放在文本框中 } else if(s.equals("-")) //后面的與之前的類似,不在注釋 {double t=a-b; String m=String.valueOf(t); text3.setText(m);} else if(s.equals("x")) {double t=a*b; String m=String.valueOf(t); text3.setText(m);} else {double t=a/b; String m=String.valueOf(t); text3.setText(m);} }}); conPane.add(text3); this.add(label1); this.setVisible(true); //將窗口設置為可視化 } } public class Counter { public static void main(String[] args) { new counter1(); } }
總結心得:
(1)在創建選項框時,要將所有的選項放在一個“容器”里,並把這個容器添加到程序中,這里我用的容器為JComboBox comboBox,同時需要導入包javax.swing.ComboBoxEditor和javax.swing.JComboBox;
(2)由於設置了按鈕響應功能,所以要設置按鍵動作和響應,這里導入了包java.awt.event.ActionEvent和java.awt.event.ActionListener
(3)因為文本框中輸入讀取到的是字符串,所以要進行計算時,要先將其轉為整形,在文本框輸出時,同理要將整形轉換為字符串
(4)注意:當光標移動到文本框上面時,獲得焦點事件,當光標移走時,便失去焦點事件,所以要注意兩個函數的作用。