繁体   English   中英

希望在 JFrame 的同一面板中为多个按钮添加功能

[英]Looking to add functionality to more than one button in the same panel in JFrame

这是完整的代码:

 public class HelloWorld implements ActionListener{
    private static JLabel label;
    private static JTextField usernameText;
    private static JLabel password;
    private static JPasswordField passwordText;
    private static JButton login;
    private static JLabel success;
    private static JButton Reset;
    @Override
    public void actionPerformed(ActionEvent e) {
            String user = usernameText.getText();
            String password = passwordText.getText();
            System.out.println(user + "," + password);
            String command = e.getActionCommand();
        if(command.equals("Reset")){
            System.out.print("Hello");

我有String command = e.getActionCommand(); 在我的public void actionPerformed(ActionEvent e) {上面在我的公共 static 主要我有

reset = new JButton("reset");

reset.setBounds(85,170,100,25);

panel.add(reset);

我的按钮、文本字段和标签中的所有 my 都已创建。 我也有

if(command.equals("reset")){

在我的@Override中,但是当我单击重置按钮时,什么也没有发生。

在行动中执行

if(e.getSoruce == buttonNmae) {
   // Your code

}

例如:

if(e.getSource == reset) {
  resetGame();

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM