gpt4 book ai didi

java - 相同的 Action 处理程序,多个按钮

转载 作者:行者123 更新时间:2023-11-30 08:13:02 25 4
gpt4 key购买 nike

我正在使用 JFrame 创建一个应用程序,我想知道如何为不同的按钮设置相同的操作处理程序。在我的应用程序中,我有 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.
*/
}
}

最佳答案

如果您试图让所有组件消失并被不同的“ View ”取代,那么最简单的方法是使用 CardLayout,然后调用该对象的 show(...) 方法在按下按钮时显示选择的 View ,如下所示。请注意,每个按钮都有自己的 ButtonAction 对象,该对象扩展了 AbstractAction。这有点像“打了类固醇”的 ActionListener,因为它有一个在按下按钮时调用的 actionPerformed(...) 方法,就像 ActionListener 一样,但在这里我们也设置按钮的文本,如果需要,我们可以设置它的助记符和/或图标,我们可以与其他按钮共享操作,例如 JMenuItems 等对象。

另请注意,我不惜一切代价努力避免 null 布局和 setBounds(...)。虽然空布局和 setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。它们不会在 GUI 调整大小时调整组件的大小,它们是增强或维护的皇家女巫,当放置在滚动 Pane 中时它们会完全失效,当在所有平台或与原始屏幕分辨率不同的屏幕分辨率上查看时它们看起来很糟糕.

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();
}
});
}
}

关于java - 相同的 Action 处理程序,多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30143110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com