[英]JButton ActionListener doesn't work (in Eclipse plugin)
(只有通過“意外”將鼠標停在按鈕上並看到它無法編譯才能發現)
原始問題
我有一個非常具體的問題:
我有兩個不同的班級:
類1在JFrame中實現JPanel。
類2僅實現JFrame。
在第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);
}
}
現在,我也想在Class 1中實現完全相同的功能。
我嘗試了以下方法:
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);
}
}
其中panel
是JFrame中的JPanel。
我已經堅持了幾個小時。 我只是似乎無法使任何ActionListener
正常工作。 是什么原因引起的?
所有幫助,不勝感激!
問題是您從未初始化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();
}
}
順便說一下,同樣重要
1)遵循Java Code Conventions類名以UpperCase開頭。
2)如果您不覆蓋JFrame
行為,請不要擴展JFrame
而不是使用合成來繼承。
例:
public class FrontPanel{
private JFrame frame;
private JPanel panel;
}
3)不要使用setBounds
而不是將該工作委托給LayoutManager
。 使用布局管理器
提供下面經過精心修改的代碼以適合我的代碼測試用例,添加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.