0

昨天我的代码被嵌套并且静态类太多时遇到了问题。我已经清理了代码,现在正尝试向 JButtons 添加一个动作侦听器。我在 GUI 上有 5 个不同的按钮,“个人资料、市场、用户、注释、信息”。每个都将是 GUI 上的一个按钮。用户将能够单击其中一个 JButton,例如“Profile”,它将打开另一个 GUI。“我在 Eclipse 中写这个。” . “我也没有使用 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 添加到所有 JButtons 的视觉想法或链接,那将不胜感激。这是我所有的 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);   
}
}

在此处输入图像描述 这是按钮的图片。用户将能够单击其中一个按钮,它将打开一个新的 GUI

4

1 回答 1

4

据我了解,您想JFrame在单击这些按钮中的任何一个时打开一个新按钮。如果是这样,那么进行接下来的两个更改:

第一:在你的类中实现ActionListener接口Stock

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

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

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

此外,我几乎忘记了强制性答案:多个 JFrame 的使用:好的还是坏的做法?

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

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);
    }
}
于 2013-10-11T15:05:08.043 回答