1.程式碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
window.setDefaultLookAndFeelDecorated(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(3,3));
JButton button[]=new JButton[10];
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].setActionCommand("d1");
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd == "d1") {
System.out.println("Yes....");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
2.
沒有留言:
張貼留言