Java e、 getActionCommand()不工作?
我试图使一些简单的编辑文本,但我的按钮不工作。。。 我有一个 方法,但它似乎不起作用。请帮忙Java e、 getActionCommand()不工作?,java,swing,events,awt,actionlistener,Java,Swing,Events,Awt,Actionlistener,我试图使一些简单的编辑文本,但我的按钮不工作。。。 我有一个 方法,但它似乎不起作用。请帮忙 public class Editor implements ActionListener{ static JFrame frame = new JFrame(); static Container contentPane = frame.getContentPane(); static int line; static JTextField lineNumber = new JTextField(
public class Editor implements ActionListener{
static JFrame frame = new JFrame();
static Container contentPane = frame.getContentPane();
static int line;
static JTextField lineNumber = new JTextField("Line number here");
static JTextField editField = new JTextField("Data here", 48);
static JButton submit = new JButton("Save");
public Editor(){
frame.setTitle("Editor (Lnull)");
frame.setSize(400,600);
frame.setVisible(true);
}
public Editor(String title){
frame.setTitle(title);
frame.setSize(400,600);
frame.setVisible(true);
}
public Editor(String title, int width, int height){
frame.setTitle(title);
frame.setSize(width, height);
frame.setVisible(true);
}
@SuppressWarnings("static-access")
public void setLine(int line){
this.line = line;
}
public void changeTitle(String title){
frame.setTitle(title);
}
public static void addComponent(Component thing){
contentPane.add(thing);
frame.repaint();
}
public static void setContentsOfFrame(Container cont){
frame.setContentPane(contentPane);
}
public static void setAction(JButton comp, String action){
comp.setActionCommand(action);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(submit.getActionCommand())){
JOptionPane.showMessageDialog(null,"you tried to submit");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Editor editor = new Editor();
addComponent(editField);
editField.setBounds(0,0,300,50);
addComponent(submit);
submit.setBounds(300,0,100,50);
submit.setActionCommand("submit");
frame.setLayout(null);
setContentsOfFrame(contentPane);
}
}
您从未将
ActionListener
附加到submit
按钮
如果未注册,Swing将无法传递事件通知
尝试类似于
submit.addActionListener(编辑器)
的操作您永远不会将ActionListener
附加到submit
按钮
如果未注册,Swing将无法传递事件通知
尝试类似于
submit.addActionListener(编辑器)
您忘记了添加ActionListener以提交按钮
submit.addActionListener(Editor.this);
您忘记添加ActionListener以提交按钮
submit.addActionListener(Editor.this);
注意信息。关于链接构造函数&使用
static
import java.awt.Container;
import javax.swing.*;
public class Editor {
// none of these should be static!
static JFrame frame = new JFrame();
static Container contentPane = frame.getContentPane();
static int line;
static JTextField lineNumber = new JTextField("Line number here");
static JTextField editField = new JTextField("Data here", 48);
static JButton submit = new JButton("Save");
public Editor(){
// chain the constructor
new Editor("Editor (Lnull)");
}
public Editor(String title){
// chain the constructor
new Editor(title, 600, 400);
}
public Editor(String title, int width, int height){
// Just do it!
frame.setTitle(title);
frame.setSize(width,height);
frame.setVisible(true);
}
}
注意信息。关于链接构造函数&使用
static
import java.awt.Container;
import javax.swing.*;
public class Editor {
// none of these should be static!
static JFrame frame = new JFrame();
static Container contentPane = frame.getContentPane();
static int line;
static JTextField lineNumber = new JTextField("Line number here");
static JTextField editField = new JTextField("Data here", 48);
static JButton submit = new JButton("Save");
public Editor(){
// chain the constructor
new Editor("Editor (Lnull)");
}
public Editor(String title){
// chain the constructor
new Editor(title, 600, 400);
}
public Editor(String title, int width, int height){
// Just do it!
frame.setTitle(title);
frame.setSize(width,height);
frame.setVisible(true);
}
}
通过将
public void actionPerformed(ActionEvent e){
更改为public void actionPerformed(ActionEvent e){System.out.println(e);
(或使用调试器并单步执行代码)进行少量调试,将很容易显示此类错误(通过不编写任何内容/单击按钮时被调用)。通过将public void actionPerformed(ActionEvent e){
更改为public void actionPerformed(ActionEvent e){System.out.println(e);
(或使用调试器并单步执行代码)进行少量调试,将很容易显示此类错误(通过不编写任何内容/单击按钮时被调用)。不要实现ActionListener
,而是查看。不要实现ActionListener
,而是查看。