[英]Create a selected sequence or inner class
我想為每個按鈕創建一個選擇序列或創建一個內部類,以處理每個按鈕的事件單擊。 我的任務是單擊一個按鈕以顯示在文本框中,或標記在界面中單擊的按鈕。
到目前為止,我制作了六個按鈕和兩個面板(每個面板中三個按鈕)。 我仍在努力進行內部學習。 我不確定如何使用ActionListener和ActionPerformed。 我需要使內部類在文本框中顯示所選按鈕的幫助。 謝謝!
import java.awt.*;
import javax.swing.*;
public class PracticeEventDriven extends JFrame {
public PracticeEventDriven()
{
JButton firstButton = new JButton("Button 1");
JButton secondButton = new JButton("Button 2");
JButton thirdButton = new JButton("Button 3");
JButton fourthButton = new JButton("Button 4");
JButton fifthButton = new JButton("Button 5");
JButton sixthButton = new JButton("Button 6");
/*Something is wrong with this. I want to use Button 4 to display.*/
ActionListener listener = new ActionListener();
fourthButton.addActionListener(listener);
JPanel group1 = new JPanel();
setLayout(new FlowLayout());
group1.add(firstButton);
group1.add(secondButton);
group1.add(thirdButton);
JPanel group2 = new JPanel();
setLayout(new FlowLayout());
group2.add(fourthButton);
group2.add(fifthButton);
group2.add(sixthButton);
this.getContentPane().add(group1);
this.getContentPane().add(group2);
}
public static void main(String[] args)
{
PracticeEventDriven frame = new PracticeEventDriven();
frame.setTitle("Button Panel Example");
frame.setSize(600, 85);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
為此,您需要在按鈕上使用ActionListener
。 閱讀有關ActionListener的教程,並閱讀oracle文檔 。 我已經更改了您的代碼,請對其進行檢查:
public class PracticeEventDriven extends JFrame {
private JTextField field;
public PracticeEventDriven() {
field = new JTextField(10);
JButton firstButton = new JButton("Button 1");
JButton secondButton = new JButton("Button 2");
JButton thirdButton = new JButton("Button 3");
JButton fourthButton = new JButton("Button 4");
JButton fifthButton = new JButton("Button 5");
JButton sixthButton = new JButton("Button 6");
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JButton b = (JButton) arg0.getSource();
field.setText(b.getText());
}
};
firstButton.addActionListener(listener);
secondButton.addActionListener(listener);
thirdButton.addActionListener(listener);
fifthButton.addActionListener(listener);
fourthButton.addActionListener(listener);
sixthButton.addActionListener(listener);
JPanel group1 = new JPanel();
setLayout(new FlowLayout());
group1.add(firstButton);
group1.add(secondButton);
group1.add(thirdButton);
JPanel group2 = new JPanel();
setLayout(new FlowLayout());
group2.add(fourthButton);
group2.add(fifthButton);
group2.add(sixthButton);
this.getContentPane().add(group1);
this.getContentPane().add(group2);
this.getContentPane().add(field);
}
public static void main(String[] args) {
PracticeEventDriven frame = new PracticeEventDriven();
frame.setTitle("Button Panel Example");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.