// A code block var foo = 'bar';
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class sole { public int num1 = 0,num2 = 0,result = 0; public String bufferString = ""; public String operString = ""; public JTextField text1; public sole(){ Frame myframe=new Frame("简易计算器"); myframe.setBounds(300,100,200,200); myframe.setResizable(false); Button button0=new Button("0"); Button button1=new Button("1"); Button button2=new Button("2"); Button button3=new Button("3"); Button button4=new Button("4"); Button button5=new Button("5"); Button button6=new Button("6"); Button button7=new Button("7"); Button button8=new Button("8"); Button button9=new Button("9"); NumberAdapter numberAdapter = new NumberAdapter(); button0.addActionListener(numberAdapter); button1.addActionListener(numberAdapter); button2.addActionListener(numberAdapter); button3.addActionListener(numberAdapter); button4.addActionListener(numberAdapter); button5.addActionListener(numberAdapter); button6.addActionListener(numberAdapter); button7.addActionListener(numberAdapter); button8.addActionListener(numberAdapter); button9.addActionListener(numberAdapter); Button b1=new Button("+"); Button b2=new Button("-"); Button b3=new Button("*"); Button b4=new Button("/"); Button b5=new Button("="); Button b6=new Button("C"); OperAdapter operAdapter = new OperAdapter(); b1.addActionListener(operAdapter); b2.addActionListener(operAdapter); b3.addActionListener(operAdapter); b4.addActionListener(operAdapter); b5.addActionListener(operAdapter); b6.addActionListener(new ResetAdapter()); Panel panel1=new Panel(); Panel panel2=new Panel(); myframe.add(panel1,"South"); myframe.add(panel2,"North"); b1.setForeground(Color.red); b2.setForeground(Color.cyan); b3.setForeground(Color.orange); b4.setForeground(Color.magenta); b5.setForeground(Color.green); b6.setForeground(Color.blue); panel1.setLayout(new GridLayout(4,4)); panel1.add(b1); panel1.add(b2); panel1.add(b3); panel1.add(b4); panel1.add(button0); panel1.add(button1); panel1.add(button2); panel1.add(button3); panel1.add(button4); panel1.add(button5); panel1.add(button6); panel1.add(button7); panel1.add(button8); panel1.add(button9); panel1.add(b6); panel1.add(b5); text1= new JTextField("0",15); text1.setHorizontalAlignment(JTextField.RIGHT); panel2.add(text1); myframe.setVisible(true); myframe.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); } class NumberAdapter implements ActionListener{ public void actionPerformed(ActionEvent e) { Button button=(Button)e.getSource(); if(bufferString.equals("0")&&button.getLabel().equals("0")) { bufferString+=button.getLabel(); text1.setText(bufferString); } else { bufferString += button.getLabel(); text1.setText(bufferString); } } } class ResetAdapter implements ActionListener{ public void actionPerformed(ActionEvent e) { text1.setText("0"); num1 = 0; num2 = 0; result = 0; bufferString = ""; operString = ""; } } class OperAdapter implements ActionListener{ public void actionPerformed(ActionEvent e) { Button b = (Button)e.getSource(); if(b.getLabel().equals("+")){ operString = "+"; num1 = Integer.parseInt(text1.getText()); text1.setText("0"); bufferString = ""; } else if (b.getLabel().equals("-")){ operString = "-"; num1 = Integer.parseInt(text1.getText()); text1.setText("0"); bufferString = ""; } else if(b.getLabel().equals("*")){ operString = "*"; num1 = Integer.parseInt(text1.getText()); text1.setText("0"); bufferString = ""; } else if(b.getLabel().equals("/")){ operString = "/"; num1 = Integer.parseInt(text1.getText()); text1.setText("0"); bufferString = ""; } else{ num2 = Integer.parseInt(text1.getText()); if(operString == "+"){ result = num1 + num2; } else if(operString == "-"){ result = num1 - num2; } else if(operString == "*"){ result = num1 * num2; } else if(operString == "/"){ if(0 == num2){ new JOptionPane(); JOptionPane.showMessageDialog(null, "除数不能为0!"); } else{ result = num1 / num2; } } bufferString = Integer.toString(result); text1.setText(bufferString); } } } public static void main(String[] args) { new sole(); } } // An highlighted block var foo = 'bar';
(1).定义初始化一个Frame类。
(2).再定义初始化两个Panel类,并将它们通过add放入Frame框架中。
(3).然后定义初始化各组件,通过add放入Panel面板中。
(4).最后使用布局管理器来布局各组件位置。
2.
(1).编写实现加、减、乘、除运算的事件的监听器。
(2).在xxxListener类中用于处理该事件的方法中,编写处理代码。
(3).组件实例化后,注册该类事件的监听器。使用addxxxListener方法来注册监听器。
新手初次尝试博客,不足处见谅