【问题标题】:Event Handler Program in java...Programs give no errors but after the event action is not displayedjava中的事件处理程序...程序没​​有错误但在事件操作后不显示
【发布时间】:2014-07-11 13:34:24
【问题描述】:

我导入了所有必要的类并且没有错误。但是当我在任何文本字段上按 enter 时,提示窗口是空白的,而不是它打算显示的字符串。 如果问题出在 JOptionPane 之类的,我不知道。当我第一次运行我的代码时,它在第二次运行时运行良好,有一个空白提示。

public class fish extends JFrame{

    private JTextField item1;
    private JTextField item2;
    private JTextField item3;
    private JPasswordField password;

    public fish(){
        super("Title");
        setLayout(new FlowLayout());

        item1=new JTextField(10);
        add (item1);
        item2=new JTextField("Enter Txt here");
        add (item2);
        item3=new JTextField("Uneditable",20);
        item3.setEditable(false);
        add (item3);

        password = new JPasswordField("MyPass");
        add(password);

        thehandler handler = new thehandler();
        item1.addActionListener(handler);
        item3.addActionListener(handler);
        item2.addActionListener(handler);
        password.addActionListener(handler);
    }

    private class thehandler implements ActionListener{

        public void actionPerformed(ActionEvent event){

            String string = "";

            if(event.getSource()==item1)
                String.format("field1: %s", event.getActionCommand());
            else if(event.getSource()==item2)
                    String.format("field2: %s", event.getActionCommand());
            else if(event.getSource()==item3)
                    String.format("field3: %s", event.getActionCommand());
            else if(event.getSource()==password)
                    String.format("Password Field: %s", event.getActionCommand());

            JOptionPane.showMessageDialog(null, string);
        }
    }
}

public class apples{
    public static void main(String args[]){

        fish tuna = new fish();
        tuna.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tuna.setSize(350,100);
        tuna.setVisible(true);
    }
}

【问题讨论】:

    标签: java swing events event-handling jframe


    【解决方案1】:

    您正在显示一个空字符串。这里:

    public void actionPerformed(ActionEvent event){
    
        String string = "";
    
        if(event.getSource()==item1) 
            String.format("field1: %s", event.getActionCommand());
        else if(event.getSource()==item2)
                String.format("field2: %s", event.getActionCommand());
        else if(event.getSource()==item3)
                String.format("field3: %s", event.getActionCommand());
        else if(event.getSource()==password)
                String.format("Password Field: %s", event.getActionCommand());
    
        JOptionPane.showMessageDialog(null, string);
    }
    

    您在哪里为字符串变量string 赋值?此外,您永远不会从 JTextField 中获取文本,而是从未分配过的 actionCommand 字符串。

    即,

    public void actionPerformed(ActionEvent event){
    
        String string = "";
    
        if(event.getSource()==item1) {
            string = String.format("field1: %s", item1.getText())
        }
        else if(event.getSource()==item2) {
            string = String.format("field2: %s", item2.getText());
        }
        else if(event.getSource()==item3) {
            string = String.format("field3: %s", item3.getText());
        }
        else if(event.getSource()==password) {
            string = String.format("Password Field: %s", new String(password.getPassword()));
        }
    
        JOptionPane.showMessageDialog(null, string);
    }
    
    • 顺便说一句,在真正的程序中,你永远不会想要new String(password.getPassword()),因为这会使你的程序非常不安全,因为密码很容易被窃取。
    • 除了 #2:在编程教育的这个阶段,您需要将每个块都用 { } 花括号括起来。安全总比后悔好。
    • 除了 #3:您将需要学习和使用 Java 编码约定,包括以大写字母开头的类名。这样其他人就能更好地一目了然地理解您的代码。

    【讨论】:

    • actioncommand() 字符串将显示命令(或在本例中为字符串),因此我不必使用 getText()。
    • 感谢您的帮助,我得到了我没有将字符串分配给任何东西的错误。
    • 感谢您的建议...我还在学习 java...但尚未有经验。
    猜你喜欢
    • 2016-04-29
    • 2022-08-09
    • 1970-01-01
    • 2014-06-13
    • 2023-03-07
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2018-09-22
    相关资源
    最近更新 更多