Java 向 JButton 添加动作事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19321367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Action Events to JButtons
提问by Aaron
Yesterday I had a problem with my code being nested and having too many static classes. I have cleaned up the code and am now trying to add an action listener to the JButtons. i have 5 different buttons that is on the GUI, "Profile, Market, Users, Notes, Information."each one will be a button on a GUI. The user will be able to click on one of the JButtons such as "Profile" and it will open another GUI. "I'm writing this in eclipse.". "I'm also not using a GUI builder."I have made another GUI that will open when one of the buttons are clicked :
昨天我的代码被嵌套并且有太多静态类时遇到了问题。我已经清理了代码,现在正在尝试向 JButton 添加一个动作侦听器。我在 GUI 上有 5 个不同的按钮,“个人资料、市场、用户、注释、信息”。每个都是 GUI 上的一个按钮。用户将能够单击 JButton 之一,例如“配置文件”,它将打开另一个 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);
}
I just cannot figure out how to add it to each JButton. If there is anyways someone could give a visual idea or link on how to add the GUI above to all of the JButtons, that would be greatly appreciated. Here is the code with all of my JButtons, and the GUI ActionEvent :
我只是不知道如何将它添加到每个 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);
}
}
Here is a picture of the buttons. The user will be able to click one of the buttons and it will open a new GUI
这是按钮的图片。用户将能够单击其中一个按钮,它将打开一个新的 GUI
采纳答案by Branislav Lazic
As I understood, you want to open a new JFrame
when you click any of these buttons. If so then make next two changes:
据我了解,JFrame
当您单击这些按钮中的任何一个时,您都想打开一个新的。如果是这样,则进行接下来的两个更改:
First: Implement ActionListener
interface in your Stock
class
第一:ActionListener
在你的Stock
类中实现接口
public class Stock implements ActionListener {
//Rest of the code...
}
Second: Pass this
keyword in addActionListener
method's for each of your buttons:
第二:this
在addActionListener
方法中为每个按钮传递关键字:
button1.addActionListener(this);
button2.addActionListener(this);
....
Also, I almost forgot obligatory answer: The Use of Multiple JFrames: Good or Bad Practice?
另外,我几乎忘记了强制性答案:多个 JFrame 的使用:好的或坏的做法?
EDIT:There you go, whole solution (if this doesn't help, I give up):
编辑:你去,整个解决方案(如果这没有帮助,我放弃):
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);
}
}