0

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

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);

}

}
4

3 回答 3

3

您永远不会将 a 附加ActionListenersubmit按钮上。

如果您不注册,Swing 将无法传递事件通知

尝试类似的东西submit.addActionListener(editor)

于 2013-01-23T06:47:33.497 回答
1

注意信息。关于链接构造函数和使用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);
    }
}
于 2013-01-23T07:00:02.890 回答
1

您忘记将 ActionListener 添加到提交按钮。

submit.addActionListener(Editor.this);
于 2013-01-23T06:48:06.863 回答