java - 向 JButton 添加操作事件

标签 java eclipse user-interface

昨天我遇到了一个问题,我的代码被嵌套并且静态类太多。我已经清理了代码,现在正在尝试向 JButton 添加一个操作监听器。我的 GUI 上有 5 个不同的按钮,“配置文件、市场、用户、注释、信息”。每个按钮都是 GUI 上的一个按钮。用户将能够单击其中一个 JButton,例如“Profile”,它将打开另一个 GUI。 “我在 eclipse 中写这个。”“我也没有使用 GUI 构建器。”我制作了另一个 GUI,单击其中一个按钮时将打开该 GUI:

public void actionPerformed (ActionEvent e) {
    JFrame frame2 = new JFrame("Your Stocks");
    frame2.setVisible(true);
    frame2.setSize(600,600);
    JLabel label = new JLabel("Your Personal Stocks");
    JPanel panel = new JPanel();
    frame2.add(panel);
    panel.add(label);   
}

我只是不知道如何将它添加到每个 JButton。如果有人可以提供有关如何将上面的 GUI 添加到所有 JButton 的视觉想法或链接,我们将不胜感激。这是包含我所有 JButton 和 GUI ActionEvent 的代码:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Stocks {

public static void main(String [] args) {

    JFrame frame = new JFrame ("Java Stocks");
    frame.setSize(700,700);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel (new GridBagLayout());
    frame.add(panel);
    frame.getContentPane().add(panel, BorderLayout.WEST);
    GridBagConstraints c = new GridBagConstraints ();

    JButton button1 = new JButton("Profile");
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(40, 40, 40, 40);
    panel.add(button1, c);
    button1.addActionListener(new Action());

    JButton button2 = new JButton("Market");
    c.gridx = 0;
    c.gridy = 1;
    panel.add(button2, c);  
    button2.addActionListener(new Action());

    JButton button3 = new JButton("Users");
    c.gridx = 0;
    c.gridy = 2;
    panel.add(button3, c);
    button3.addActionListener(new Action());

    JButton button4 = new JButton("Notes");
    c.gridx = 0;
    c.gridy = 3;
    panel.add(button4, c);
    button4.addActionListener(new Action());

    JButton button5 = new JButton("Information");
    c.gridx = 0;
    c.gridy = 4;
    panel.add(button5, c);
    button5.addActionListener(new Action());
}

public void actionPerformed (ActionEvent e) {
    JFrame frame2 = new JFrame("Your Stocks");
    frame2.setVisible(true);
    frame2.setSize(600,600);
    JLabel label = new JLabel("Your Personal Stocks");
    JPanel panel = new JPanel();
    frame2.add(panel);
    panel.add(label);   
}
}

enter image description here 这是按钮的图片。用户将能够单击其中一个按钮,它将打开一个新的 GUI

最佳答案

据我了解,当您单击这些按钮中的任何一个时,您希望打开一个新的JFrame。如果是这样,请进行以下两项更改:

首先:在 Stock 类中实现 ActionListener 接口(interface)

public class Stock implements ActionListener {
    //Rest of the code...
}

第二:在每个按钮的 addActionListener 方法中传递 this 关键字:

button1.addActionListener(this);
button2.addActionListener(this);
....

另外,我差点忘了必填答案:The Use of Multiple JFrames: Good or Bad Practice?

编辑: 这就是整个解决方案(如果这没有帮助,我放弃):

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Stocks implements ActionListener {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Stocks().createGui();
            }
        });

    }

    public void createGui() {
        JFrame frame = new JFrame("Java Stocks");
        frame.setSize(700, 700);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridBagLayout());
        frame.add(panel);
        frame.getContentPane().add(panel, BorderLayout.WEST);
        GridBagConstraints c = new GridBagConstraints();

        JButton button1 = new JButton("Profile");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(40, 40, 40, 40);
        panel.add(button1, c);
        button1.addActionListener(this);

        JButton button2 = new JButton("Market");
        c.gridx = 0;
        c.gridy = 1;
        panel.add(button2, c);
        button2.addActionListener(this);

        JButton button3 = new JButton("Users");
        c.gridx = 0;
        c.gridy = 2;
        panel.add(button3, c);
        button3.addActionListener(this);

        JButton button4 = new JButton("Notes");
        c.gridx = 0;
        c.gridy = 3;
        panel.add(button4, c);
        button4.addActionListener(this);

        JButton button5 = new JButton("Information");
        c.gridx = 0;
        c.gridy = 4;
        panel.add(button5, c);
        button5.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        JFrame frame2 = new JFrame("Your Stocks");
        frame2.setVisible(true);
        frame2.setSize(600, 600);
        JLabel label = new JLabel("Your Personal Stocks");
        JPanel panel = new JPanel();
        frame2.add(panel);
        panel.add(label);
    }
}

关于java - 向 JButton 添加操作事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19321367/

相关文章:

java - Gui附加输出清晰度

java - IResourceChangeEvent - 如何识别项目删除/重命名

java - 天气图标字体

c - 如何从 c 头文件中提取函数原型(prototype)以实现自动完成功能

java - NSIS Eclipse 插件不工作

java - 将 AcionListener 与 GUI 分离

c++ - Qt 对象/类到 Qt ui 文件

java - 即使使用 dp,布局也不适合屏幕截图

java - Java 的 PreparedStatement 是如何工作的?

java - KeyPairGenerator 无法解析