[英]JButton ActionListener doesn't work (in Eclipse plugin)
(only found out by 'accidentally' pausing my mouse on the button and seeing that it doesn't compile)
(只有通过“意外”将鼠标停在按钮上并看到它无法编译才能发现)
Original question 原始问题
I have a very specific issue: 我有一个非常具体的问题:
I have two different classes: 我有两个不同的班级:
Class 1 implements a JPanel within a JFrame. 类1在JFrame中实现JPanel。
Class 2 implements a JFrame only. 类2仅实现JFrame。
In Class 2, the following code works perfectly: 在第2类中,以下代码可以完美运行:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class frameTest extends JFrame {
private JPanel contentPane;
private JTextField txtGeefLiefde;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frameTest frame = new frameTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frameTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnTest = new JButton("press");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testFunction();
}
});
btnTest.setBounds(162, 188, 89, 23);
contentPane.add(btnTest);
}
public void testFunction()
{
JTextPane textPane = new JTextPane();
textPane.setBounds(162, 231, 89, 20);
textPane.setText(":)");
contentPane.add(textPane);
}
}
Now I want to implement the exact same functionality in Class 1 as well. 现在,我也想在Class 1中实现完全相同的功能。
I tried the following: 我尝试了以下方法:
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
public class frontpanel extends JFrame {
private JPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frontpanel frame = new frontpanel();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frontpanel() {
JButton btnTest = new JButton("press");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testFunction();
}
});
btnTest.setBounds(162, 188, 89, 23);
panel.add(btnTest);
}
public void testFunction()
{
JTextPane textPane = new JTextPane();
textPane.setBounds(162, 231, 89, 20);
textPane.setText(":)");
panel.add(textPane);
}
}
where panel
is the JPanel in the JFrame. 其中
panel
是JFrame中的JPanel。
I have been stuck on this for hours. 我已经坚持了几个小时。 I just can't seem to get ANY
ActionListener
to work. 我只是似乎无法使任何
ActionListener
正常工作。 What is causing the problem? 是什么原因引起的?
All help greatly appreciated! 所有帮助,不胜感激!
The problem is that you never initialized panel
and also you have to add to the jframe
问题是您从未初始化
panel
而且还必须添加到jframe
public FrontPanel() {
JButton btnTest = new JButton("press");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testFunction();
}
});
// btnTest.setBounds(162, 188, 89, 23); you should avoid using this
panel = new JPanel();
panel.add(btnTest);
this.add(panel);
this.pack();
}
}
By the way and not less important, 顺便说一下,同样重要
1)Follow Java Code Conventions class names starts with UpperCase. 1)遵循Java Code Conventions类名以UpperCase开头。
2) Don't extend JFrame
instead of that use composition against inheritance if you are not overriding JFrame
behaviour. 2)如果您不覆盖
JFrame
行为,请不要扩展JFrame
而不是使用合成来继承。
Example: 例:
public class FrontPanel{
private JFrame frame;
private JPanel panel;
}
3) Don't use setBounds
instead of that delegate this job to LayoutManager
. 3)不要使用
setBounds
而不是将该工作委托给LayoutManager
。 Using Layout Managers 使用布局管理器
Giving the slighly modified code below to fit my test case of your code, adding panel
initialition and adding it to Test
frame, that you mentionned was a copy/paste error, with a dimension for ease of testing, the code works well on my side. 提供下面经过精心修改的代码以适合我的代码测试用例,添加
panel
初始化并将其添加到Test
frame,您提到的是复制/粘贴错误,其尺寸易于测试,该代码对我而言效果很好。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
public class Test extends JFrame {
private JPanel panel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JButton btnTest = new JButton("press");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testFunction();
}
});
btnTest.setBounds(162, 188, 89, 23);
panel.add(btnTest);
this.setSize(new Dimension(300, 300));
this.add(panel);
}
public void testFunction()
{
JTextPane textPane = new JTextPane();
textPane.setBounds(162, 231, 89, 20);
textPane.setText(":)");
panel.add(textPane);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.