简体   繁体   English

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

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

Here is the full code:这是完整的代码:

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

I have String command = e.getActionCommand();我有String command = e.getActionCommand(); inside my public void actionPerformed(ActionEvent e) { above in in my public static main I have在我的public void actionPerformed(ActionEvent e) {上面在我的公共 static 主要我有

reset = new JButton("reset");

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

panel.add(reset);

where all my in my buttons, text fields, and labels have been created.我的按钮、文本字段和标签中的所有 my 都已创建。 I also have我也有

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

in my @Override but when I click the reset button nothing happens.在我的@Override中,但是当我单击重置按钮时,什么也没有发生。

In actionPerformed do在行动中执行

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

}

For example:例如:

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

}

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

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