關於我

就讀逢甲大學
正在修java


2015年4月10日 星期五

按鈕*9

1.程式碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JButtonDemo2 {

JFrame jtfMainFrame;
JButton jbnButton1, jbnButton2, jbnButton3, jbnButton4, jbnButton5, jbnButton6, jbnButton7, jbnButton8, jbnButton9;
JTextField jtfInput;
JPanel jplPanel;
public JButtonDemo2() {
jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(50, 50);
jbnButton1 = new JButton("Button 1");
jbnButton2 = new JButton("Button 2");

jbnButton3 = new JButton("Button 3");
jbnButton4 = new JButton("Button 4");

jbnButton5 = new JButton("Button 5");
jbnButton6 = new JButton("Button 6");

jbnButton7 = new JButton("Button 7");
jbnButton8 = new JButton("Button 8");

jbnButton9 = new JButton("Button 9");

jtfInput = new JTextField(20);
jplPanel = new JPanel();
jbnButton1.setMnemonic(KeyEvent.VK_I); //Set ShortCut Keys
jbnButton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 1!");
}
});
jbnButton2.setMnemonic(KeyEvent.VK_I);
jbnButton2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 2!");
}
});
jbnButton2.setMnemonic(KeyEvent.VK_I);
jbnButton2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 2!");
}
});

jbnButton3.setMnemonic(KeyEvent.VK_I);
jbnButton3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 3!");
}
});

jbnButton4.setMnemonic(KeyEvent.VK_I);
jbnButton4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 4!");
}
});

jbnButton5.setMnemonic(KeyEvent.VK_I);
jbnButton5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 5!");
}
});

jbnButton6.setMnemonic(KeyEvent.VK_I);
jbnButton6.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 6!");
}
});

jbnButton7.setMnemonic(KeyEvent.VK_I);
jbnButton7.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 7!");
}
});

jbnButton8.setMnemonic(KeyEvent.VK_I);
jbnButton8.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 8!");
}
});
jbnButton9.setMnemonic(KeyEvent.VK_I);
jbnButton9.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 9!");
}
});


jplPanel.setLayout(new FlowLayout());
jplPanel.add(jtfInput);
jplPanel.add(jbnButton1);
jplPanel.add(jbnButton2);
jplPanel.add(jbnButton3);
jplPanel.add(jbnButton4);
jplPanel.add(jbnButton5);
jplPanel.add(jbnButton6);
jplPanel.add(jbnButton7);
jplPanel.add(jbnButton8);
jplPanel.add(jbnButton9);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtfMainFrame.pack();
jtfMainFrame.setVisible(true);
}
public static void main(String[] args) {
// Set the look and feel to Java Swing Look
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
JButtonDemo2 application = new JButtonDemo2();
}
}


2.完成圖













沒有留言:

張貼留言