事件監聽和計算器界面
1.事件監聽
package 事件監聽;
import java.awt.*;
import java.awt.event.*;
public class Frame2 extends Frame implements ActionListener{
private Button button1;
public Frame2()
{
super("個人信息");
this.setSize(250,200);//設計組件的尺寸
this.setLocation(800,600);//設計組件顯示的位置
this.setBackground(Color.green);//設計背景顏色
this.setLayout(new FlowLayout());//設計容器為流布局,居中
this.add(new Label("姓名"));
this.add(new TextField("卓摸卟透",20));
this.add(new Label("性別"));
this.add(new TextField("男",20));
this.add(new Label("民族"));
this.add(new TextField("回",20));
this.add(new Label("年齡"));
this.add(new TextField("19",20));
button1=new Button("OK");
this.add(button1);
button1.addActionListener(this);
this.addWindowListener(new WinClose());
this.setVisible(true);
}
public void actionPerformed(ActionEvent ev)
{
if(ev.getSource()==button1)
{
System.out.print("welcome");
}
}
public static void main(String arg[])
{
new Frame2();
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent ev)
{
System.exit(0);
}
public void windowOpened(WindowEvent ev) {}
public void windowActivated(WindowEvent ev) {}
public void windowDeactivated(WindowEvent ev) {}
public void windowClose(WindowEvent ev) {}
public void windowIconified(WindowEvent ev) {}
public void windowDeiconified(WindowEvent ev) {}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO 自動生成的方法存根
}
}
}
2.計算器界面
package 計算器;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Jisuanqi extends JFrame {
public static void main(String[] args) {
Jisuanqi frame = new Jisuanqi();
frame.setTitle("計算器");
frame.setSize(300,400);
frame.setResizable(false);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.init();
frame.setVisible(true);
}
private void init()
{
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment (JTextField.RIGHT);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
Container container = getContentPane();
container.add(textField, BorderLayout.NORTH);
container.add(panel, BorderLayout.CENTER);
panel.add(useButton(‘7‘));
panel.add(useButton(‘8‘));
panel.add(useButton(‘9‘));
panel.add(useButton(‘+‘));
panel.add(useButton(‘4‘));
panel.add(useButton(‘5‘));
panel.add(useButton(‘6‘));
panel.add(useButton(‘-‘));
panel.add(useButton(‘1‘));
panel.add(useButton(‘2‘));
panel.add(useButton(‘3‘));
panel.add(useButton(‘*‘));
panel.add(useButton(‘0‘));
panel.add(useButton(‘.‘));
panel.add(useButton(‘=‘));
panel.add(useButton(‘/‘));
}
public JButton useButton (final char key)
{
JButton button = new JButton(String.valueOf(key));
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JButton btn = (JButton) event.getSource();
char key2 = btn.getText().charAt(0);
action(key2);
}
});
button.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent event)
{
char key2 = event.getKeyChar ();
action(key2);
}
});
return button;
}
private void action(char key2)
{
if(reop)
{
textField.setText("");
reop = false;
}
switch(key2)
{
case ‘+‘:
a1 = Double.parseDouble(textField.getText());
textField.setText("");
operator = ‘+‘;
break;
case ‘-‘:
a1 = Double.parseDouble(textField.getText());
textField.setText("");
operator = ‘-‘;
break;
case ‘*‘:
a1= Double.parseDouble(textField.getText());
textField.setText("");
operator = ‘*‘;
break;
case ‘/‘:
a1= Double.parseDouble(textField.getText());
textField.setText("");
operator = ‘/‘;
break;
case ‘=‘:
reop = true;
a2 = Double.parseDouble(textField.getText());
switch(operator)
{
case ‘+‘:
result = a1 + a2;
break;
case ‘-‘:
result= a1 - a2;
break;
case ‘*‘:
result = a1 * a2;
break;
case ‘/‘:
result = a1 / a2;
break;
default: ;
}
textField.setText(String.valueOf(result));
break;
case ‘1‘:
case ‘2‘:
case ‘3‘:
case ‘4‘:
case ‘5‘:
case ‘6‘:
case ‘7‘:
case ‘8‘:
case ‘9‘:
case ‘0‘:
case ‘.‘:
String text = textField.getText() + key2;
textField.setText(text);
break;
default: ;
}
}
private JTextField textField;
private double a1, a2, result;
private char operator;
private boolean reop = false;
}}
事件監聽和計算器界面