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(4,4));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4));
JButton button[]=new JButton[17];
for (int i = 1; i <= 16; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
button[1].setActionCommand("d1");
button[2].setActionCommand("d2");
button[3].setActionCommand("d3");
button[4].setActionCommand("d4");
button[5].setActionCommand("d5");
button[6].setActionCommand("d6");
button[7].setActionCommand("d7");
button[8].setActionCommand("d8");
button[9].setActionCommand("d9");
button[10].setActionCommand("d10");
button[11].setActionCommand("d11");
button[12].setActionCommand("d12");
button[13].setActionCommand("d13");
button[14].setActionCommand("d14");
button[15].setActionCommand("d15");
button[16].setActionCommand("d16");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String cmd2 = e.getActionCommand();
String cmd3 = e.getActionCommand();
String cmd4 = e.getActionCommand();
String cmd5 = e.getActionCommand();
String cmd6 = e.getActionCommand();
String cmd7 = e.getActionCommand();
String cmd8 = e.getActionCommand();
String cmd9 = e.getActionCommand();
String cmd10 = e.getActionCommand();
String cmd11 = e.getActionCommand();
String cmd12 = e.getActionCommand();
String cmd13 = e.getActionCommand();
String cmd14 = e.getActionCommand();
String cmd15 = e.getActionCommand();
String cmd16 = e.getActionCommand();
if (cmd == "d1") {
System.out.println("1");
}
if (cmd2 == "d2") {
System.out.println("2");
}
if (cmd3 == "d3") {
System.out.println("3");
}
if (cmd4 == "d4") {
System.out.println("4");
}
if (cmd5 == "d5") {
System.out.println("5");
}
if (cmd6 == "d6") {
System.out.println("6");
}
if (cmd7 == "d7") {
System.out.println("7");
}
if (cmd8 == "d8") {
System.out.println("8");
}
if (cmd9 == "d9") {
System.out.println("9");
}
if (cmd10 == "d10") {
System.out.println("10");
}
if (cmd11 == "d11") {
System.out.println("11");
}
if (cmd12 == "d12") {
System.out.println("12");
}
if (cmd13 == "d13") {
System.out.println("13");
}
if (cmd14 == "d14") {
System.out.println("14");
}
if (cmd15 == "d15") {
System.out.println("15");
}
if (cmd16 == "d16") {
System.out.println("16");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlFpvtR5NAM_OwhOX_IoQdgfzcB34YufJliYmjaI_v_rW2UfxI6F44u0ydUN7Sd9o0ErEqYY5ls2nB8P579RE8tX6Tu0YTdLo_2CYOlr4YNv_aFFICUf1l9c5CffYzpab4UAT2hN05cZhu/s320/AAA.png)
沒有留言:
張貼留言