相同的动作处理程序,多个按钮
Same actionhandler, multiple buttons
我正在使用 JFrame 创建一个应用程序,我想知道如何为不同的按钮设置相同的动作处理程序。在我的应用程序中,我有 8 个按钮,一旦你点击它们,它们就会全部消失。 ''disappearing thing'' 发生在每个按钮上,但之后每个按钮都会执行不同的操作。所以我想通过这样做来节省一些行,但我不知道如何。
这是解释我的意思的代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class WhosebugExample {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WhosebugExample window = new WhosebugExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public WhosebugExample() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("New button");
JButton btnNewButton_1 = new JButton("New button");
JButton btnNewButton_2 = new JButton("New button");
JButton btnNewButton_3 = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
initialize2();
}
});
btnNewButton.setBounds(75, 51, 89, 23);
frame.getContentPane().add(btnNewButton);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnNewButton_2.setVisible(false);
btnNewButton_3.setVisible(false);
initialize2();
}
});
btnNewButton_1.setBounds(75, 85, 89, 23);
frame.getContentPane().add(btnNewButton_1);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnNewButton_2.setVisible(false);
btnNewButton_3.setVisible(false);
initialize2();
}
});
btnNewButton_2.setBounds(75, 119, 89, 23);
frame.getContentPane().add(btnNewButton_2);
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnNewButton_2.setVisible(false);
btnNewButton_3.setVisible(false);
initialize2();
}
});
btnNewButton_3.setBounds(75, 153, 89, 23);
frame.getContentPane().add(btnNewButton_3);
}
private void initialize2(){
/*here I'll put the code for my new frame.
I'll make like some more buttons and a textfield.
*/
}
}
如果您试图让所有组件消失并被不同的 "view" 替换,那么最简单的方法是使用 CardLayout,然后调用此对象的 show(...)
按下按钮时显示选择视图的方法,如下所示。请注意,每个按钮都有自己的 ButtonAction
对象,该对象扩展了 AbstractAction。这有点像 ActionListener "on steroids" 因为它有一个 actionPerformed(...)
方法,当按钮被按下时调用,就像 ActionListener 一样,但这里我们还设置了按钮的文本,我们可以如果需要,可以设置它的助记符 and/or 图标,我们可以与其他按钮共享操作,例如 JMenuItems.
另请注意,我不惜一切代价努力避免 null
布局和 setBounds(...)
。虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用它们时 运行 就会遇到更严重的困难。它们不会在 GUI 调整大小时调整您的组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失败,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕从原来的。
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class SeveralButtons extends JPanel {
// preferred size dimensions
private static final int PREF_W = 450;
private static final int PREF_H = 300;
// number of buttons displayed
private static final int BUTTON_COUNT = 4;
public static final String BLANK_PANEL = "blank";
public static final String MAIN_PANEL = "main panel";
private CardLayout cardLayout = new CardLayout();
public SeveralButtons() {
// create JPanel to hold our buttons. use a grid layout with 1 column
JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5));
for (int i = 0; i < BUTTON_COUNT; i++) {
// create a new JButton and give it a an Action
JButton button = new JButton(new ButtonAction(i));
// add it to the buttonPanel
buttonPanel.add(button);
}
// main JPanel to hold the buttonPanel
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
mainPanel.add(buttonPanel);
// set this class's layout
setLayout(cardLayout);
add(mainPanel, MAIN_PANEL); // add mainPanel
add(new JPanel(), BLANK_PANEL); // add a blank JPanel
}
@Override // so JPanel will be at least our desired size
public Dimension getPreferredSize() {
Dimension superSz = super.getPreferredSize();
if (isPreferredSizeSet()) {
return superSz;
}
int prefW = Math.max(superSz.width, PREF_W);
int prefH = Math.max(superSz.height, PREF_H);
return new Dimension(prefW, prefH);
}
// our AbstractAction class, an ActionListener "on steroids"
private class ButtonAction extends AbstractAction {
private int value;
public ButtonAction(int i) {
String name = "Button " + i;
putValue(NAME, name);
this.value = i;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO: do some number specific action based on value
// For a trivial example:
String message = "Button pressed: " + value;
JOptionPane.showMessageDialog((Component) e.getSource(), message);
// swap view to a blank view
cardLayout.show(SeveralButtons.this, BLANK_PANEL);
}
}
// create and display GUI in a thread-safe manner
private static void createAndShowGui() {
SeveralButtons mainPanel = new SeveralButtons();
JFrame frame = new JFrame("SeveralButtons");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
您应该首先创建一个动作侦听器:
CustomActionListener actionListener = new CustomActionListener();
然后将 actionListener 添加到每个按钮,例如:
btnNewButton.addActionListener(actionListener);
然后创建一个名为 CustomActionListener 的内部 class,如下所示:
public class CustomActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
//Code you want to perform when a button is pressed.
}
}
我正在使用 JFrame 创建一个应用程序,我想知道如何为不同的按钮设置相同的动作处理程序。在我的应用程序中,我有 8 个按钮,一旦你点击它们,它们就会全部消失。 ''disappearing thing'' 发生在每个按钮上,但之后每个按钮都会执行不同的操作。所以我想通过这样做来节省一些行,但我不知道如何。
这是解释我的意思的代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class WhosebugExample {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WhosebugExample window = new WhosebugExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public WhosebugExample() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("New button");
JButton btnNewButton_1 = new JButton("New button");
JButton btnNewButton_2 = new JButton("New button");
JButton btnNewButton_3 = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
initialize2();
}
});
btnNewButton.setBounds(75, 51, 89, 23);
frame.getContentPane().add(btnNewButton);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnNewButton_2.setVisible(false);
btnNewButton_3.setVisible(false);
initialize2();
}
});
btnNewButton_1.setBounds(75, 85, 89, 23);
frame.getContentPane().add(btnNewButton_1);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnNewButton_2.setVisible(false);
btnNewButton_3.setVisible(false);
initialize2();
}
});
btnNewButton_2.setBounds(75, 119, 89, 23);
frame.getContentPane().add(btnNewButton_2);
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButton.setVisible(false);
btnNewButton_1.setVisible(false);
btnNewButton_2.setVisible(false);
btnNewButton_3.setVisible(false);
initialize2();
}
});
btnNewButton_3.setBounds(75, 153, 89, 23);
frame.getContentPane().add(btnNewButton_3);
}
private void initialize2(){
/*here I'll put the code for my new frame.
I'll make like some more buttons and a textfield.
*/
}
}
如果您试图让所有组件消失并被不同的 "view" 替换,那么最简单的方法是使用 CardLayout,然后调用此对象的 show(...)
按下按钮时显示选择视图的方法,如下所示。请注意,每个按钮都有自己的 ButtonAction
对象,该对象扩展了 AbstractAction。这有点像 ActionListener "on steroids" 因为它有一个 actionPerformed(...)
方法,当按钮被按下时调用,就像 ActionListener 一样,但这里我们还设置了按钮的文本,我们可以如果需要,可以设置它的助记符 and/or 图标,我们可以与其他按钮共享操作,例如 JMenuItems.
另请注意,我不惜一切代价努力避免 null
布局和 setBounds(...)
。虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用它们时 运行 就会遇到更严重的困难。它们不会在 GUI 调整大小时调整您的组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失败,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕从原来的。
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class SeveralButtons extends JPanel {
// preferred size dimensions
private static final int PREF_W = 450;
private static final int PREF_H = 300;
// number of buttons displayed
private static final int BUTTON_COUNT = 4;
public static final String BLANK_PANEL = "blank";
public static final String MAIN_PANEL = "main panel";
private CardLayout cardLayout = new CardLayout();
public SeveralButtons() {
// create JPanel to hold our buttons. use a grid layout with 1 column
JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5));
for (int i = 0; i < BUTTON_COUNT; i++) {
// create a new JButton and give it a an Action
JButton button = new JButton(new ButtonAction(i));
// add it to the buttonPanel
buttonPanel.add(button);
}
// main JPanel to hold the buttonPanel
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
mainPanel.add(buttonPanel);
// set this class's layout
setLayout(cardLayout);
add(mainPanel, MAIN_PANEL); // add mainPanel
add(new JPanel(), BLANK_PANEL); // add a blank JPanel
}
@Override // so JPanel will be at least our desired size
public Dimension getPreferredSize() {
Dimension superSz = super.getPreferredSize();
if (isPreferredSizeSet()) {
return superSz;
}
int prefW = Math.max(superSz.width, PREF_W);
int prefH = Math.max(superSz.height, PREF_H);
return new Dimension(prefW, prefH);
}
// our AbstractAction class, an ActionListener "on steroids"
private class ButtonAction extends AbstractAction {
private int value;
public ButtonAction(int i) {
String name = "Button " + i;
putValue(NAME, name);
this.value = i;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO: do some number specific action based on value
// For a trivial example:
String message = "Button pressed: " + value;
JOptionPane.showMessageDialog((Component) e.getSource(), message);
// swap view to a blank view
cardLayout.show(SeveralButtons.this, BLANK_PANEL);
}
}
// create and display GUI in a thread-safe manner
private static void createAndShowGui() {
SeveralButtons mainPanel = new SeveralButtons();
JFrame frame = new JFrame("SeveralButtons");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
您应该首先创建一个动作侦听器:
CustomActionListener actionListener = new CustomActionListener();
然后将 actionListener 添加到每个按钮,例如:
btnNewButton.addActionListener(actionListener);
然后创建一个名为 CustomActionListener 的内部 class,如下所示:
public class CustomActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
//Code you want to perform when a button is pressed.
}
}