我正在使用JFrame创建一个应用程序,我想知道如何将相同的actionhandler设置为不同的按钮。在我的应用程序中,我有8个按钮,一旦你点击它们,它们都应该消失。这个“消失的东西”会发生在每个按钮上,但之后每个按钮都会有不同的效果。所以我想通过这样做来保存一些行,但我不知道如何。
这是解释我的意思的代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class StackOverflowExample {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StackOverflowExample window = new StackOverflowExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public StackOverflowExample() {
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.
*/
}
}
答案 0 :(得分:2)
如果您尝试使所有组件消失并被其他“视图”替换,那么最简单的方法是使用CardLayout,然后调用此对象的show(...)
方法来显示按下按钮时的选择视图,如下所示。请注意,每个按钮都有自己的ButtonAction
对象,该对象扩展了AbstractAction。这有点像ActionListener“on steroids”,因为它有一个actionPerformed(...)
方法,当按下按钮时调用它,就像ActionListener一样,但是在这里我们也设置了按钮的文本,我们可以设置它的助记符和/或图标(如果需要),我们可以与其他按钮共享动作,例如JMenuItems。
另请注意,我会不惜一切代价避免null
布局和setBounds(...)
。虽然null布局和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();
}
});
}
}
答案 1 :(得分:1)
您应首先创建一个actionlistener:
CustomActionListener actionListener = new CustomActionListener();
然后将actionListener添加到每个按钮,如:
btnNewButton.addActionListener(actionListener);
然后创建一个名为CustomActionListener的内部类,如下所示:
public class CustomActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
//Code you want to perform when a button is pressed.
}
}