java - 当我点击java按钮时什么也没有发生

标签 java swing

我有一个应用程序,当您运行它时,您会看到一个面板来添加 3 个值,然后您需要按“确定”按钮才能继续。

我放置了一个 Click() 方法,但是当我按“确定”时什么也没有发生。

还要提一下,当我进行 deluging 时,它可以正常工作,但当我将其导出为可执行 jar 时,则不能。

JFrame frame = new JFrame();
JLabel mlabel = new JLabel("Please provide xxxx",JLabel.CENTER);
JLabel uLabel = new JLabel("User ID:",JLabel.LEFT);
JPanel buttonField = new JPanel(new GridLayout (1,3));
JPanel userArea =  new JPanel(new GridLayout (0,3));


frame.setLayout(new GridLayout (0,1));
buttonField.setLayout(new FlowLayout());
JButton confirm =new JButton("OK");
confirm.addMouseListener((MouseListener) new mouseClick());
buttonField.add(confirm);

App.insertText = new JTextField(20);
frame.add(mlabel);
userArea.add(uLabel);
userArea.add(insertText);
frame.add(buttonField);
frame.setSize(300,600);
App.credGet = false;
frame.setVisible(true); 

然后点击:

public void mouseClicked(MouseEvent e) {
    App.un = App.insertText.getText();
    App.project = ((JTextComponent) App.insertProject).getText();
    //App.pw = char[] App.insertPass.getPassword();
    char[] input = App.insertPass.getPassword();
    App.pw = "";
    for (int i1 = 0; i1 < input.length; i1++){
        App.pw = App.pw + input[i1];
    }
} 

public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

最佳答案

线路

confirm.addMouseListener((MouseListener) new mouseClick());

我想 mouseClick 是您在下面的示例中发布的类。为什么将其转换为MouseListener?它没有实现MouseListener吗?

无论如何,您最好将其替换为 ActionListener (匿名类在这里可以很好地工作),例如

confirm.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        ...         
    }
});

阅读Pros and cons of using ActionListener vs. MouseListener for capturing clicks on a JButton了解更多信息

关于java - 当我点击java按钮时什么也没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386165/

相关文章:

Java,解析 xml 属性的原始、未转换值。贾克森或贾克斯布

java - 如何组织java swing应用程序架构?

java - Freemarker 在 ftl 上嵌入图像

java - 检索分成 2 个或更多字符串的文本行

java - Jython GUI 的问题

java - 在 JTable 中使列不可编辑

java - 将焦点设置在 JDateChooser 上

java - 如何获取文件的所有不同大小的系统图标

java - java.util.regex.Pattern.quote(String arg) 的 GWT 替代品是什么

java - 后台蓝牙通信。线程、服务、IntentService、AsyncTask..?