gpt4 book ai didi

java - e.getActionCommand() 不起作用?

转载 作者:行者123 更新时间:2023-12-01 17:26:36 26 4
gpt4 key购买 nike

我试图做一些简单的事情来编辑文本,但我的按钮不起作用......我有一个

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(submit.getActionCommand())){
JOptionPane.showMessageDialog(null,"you tried to submit");
}
}

方法,但似乎不起作用。请帮忙?

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(editor)

关于java - e.getActionCommand() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14473891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com