向JButtons添加动作事件

时间:2013-10-11 15:02:03

标签: java eclipse user-interface

昨天我遇到了嵌套代码和静态类太多的问题。我已经清理了代码,现在我正在尝试向JButtons添加一个动作监听器。我在GUI上有5个不同的按钮,“个人资料,市场,用户,笔记,信息。”每个按钮都是GUI上的一个按钮。用户可以单击其中一个JButton,例如“Profile”,它将打开另一个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

1 个答案:

答案 0 :(得分:4)

据我所知,当您点击其中任何一个按钮时,您想要打开一个新的JFrame。如果是,则进行下两个更改:

首先:在ActionListener

中实施Stock界面
public class Stock implements ActionListener {
    //Rest of the code...
}

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

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