不同类中的Java动作侦听器不起作用 [英] Java Action Listeners in a different class not working

查看:155
本文介绍了不同类中的Java动作侦听器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用JFrame,当我将它们放在一个类中时,它具有简单的登录,注册和弹出框架的功能.我想使其更好,而不是全部打包在一个类中,所以我为框架,按钮,面板,变量和主类创建了一个类.我的问题是,框架本身可以正常工作,并且可以加载和显示,但是按钮上的ActionListeners根本无法工作.当我按下按钮等时,什么都没有改变.我是Java的新手,而JFrames和JButtons的新手.我可以做些什么使它更简单或使我的代码看起来更好吗?该代码将在每个单独的类中:

I've been working with JFrame recently and had a simple login, register and popup frames work when I had them in a single class. I wanted to make it nicer and not all packed inside one class so I made a class for the frames, buttons, panels, variables and the main class. My problem is that The frames themselves are working fine and loading up and displaying, but the ActionListeners on the buttons aren't working at all. Nothing changes when I hit a button etc. I'm fairly new to Java and very new to the JFrames and JButtons. Is there anything I can be doing to make this simpler or make my code look better? The code will be in each seperate class:

现在什么也没有运行,即使在应该调用LoginScreen()之前main中的"This is running"也没有运行.我不确定为实现此目的需要进行哪些更改?

Right now nothing is running, even the "This is running" in main before it's supposed to call LoginScreen() doesn't run. I'm not sure what I changed to make this happen?

主类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class myGame {


   public static void main(String[] args){ 
      buttons myButtons = new buttons();
      frames myFrames = new frames();
      panels myPanels = new panels();
      variables myVariables = new variables();    

      System.out.println("This is running");  
      myFrames.loginScreenFrame();     
      System.out.println("This is also running");                                                                      }
}

框架类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class frames{


   public JFrame loginScreenFrame(){
      variables myVariables = new variables();
      panels myPanels = new panels();
      buttons myButtons = new buttons();


      myVariables.loginFrame.setSize(300,125);
      myVariables.loginFrame.setLocation(550,250);

      myVariables.loginFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

      Container content = myVariables.loginFrame.getContentPane();
      content.add(myPanels.loginScreenPanel(), BorderLayout.CENTER);
      content.add(myPanels.loginScreenButtonsPanel(), BorderLayout.SOUTH);
      myButtons.registerButton.addActionListener(myButtons.registerListener);

      myVariables.loginFrame.setVisible(true);

      return myVariables.loginFrame;
   }

   public JFrame registerFrame(){

      variables myVariables = new variables();
      panels myPanels = new panels();

      myVariables.registerFrame.setSize(400,125);
      myVariables.registerFrame.setLocation(550,250);
      myVariables.registerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container content = myVariables.registerFrame.getContentPane();

      content.add(myPanels.registerScreenPanel(), BorderLayout.CENTER);
      content.add(myPanels.registerScreenButtonsPanel(), BorderLayout.SOUTH);

      myVariables.registerFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

      myVariables.registerFrame.setVisible(true); 

      return myVariables.registerFrame;
   }
}

按钮类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class buttons{
   JButton loginButton = new JButton("Login");
   JButton registerButton = new JButton("Register");
   JButton cancelButton = new JButton("Cancel");
   JButton checkUsernameButton = new JButton("Check Username");


   public void actionListeners(){

   variables myVariables = new variables();
   frames myFrames = new frames();
   panels myPanels = new panels();

      ActionListener cancelListener = new ActionListener(){
         public void actionPerformed(ActionEvent ae){
            frames myFrames = new frames();
            myFrames.registerFrame().dispose();
         }
      };

      ActionListener usernameListener = new ActionListener(){
         public void actionPerformed(ActionEvent ae){

         }
      };

      ActionListener passwordListener = new ActionListener(){
         public void actionPerformed(ActionEvent ae){

         }
      };

      ActionListener passwordCheckListener = new ActionListener(){
         public void actionPerformed(ActionEvent ae){

         }
      };

      ActionListener checkUsernameListener = new ActionListener(){
         public void actionPerformed(ActionEvent ae){
            variables myVariables = new variables();
            if (myVariables.usernameAndPassword.get(myVariables.username) == null){
               JPanel okButtonPanel = new JPanel();
               final JFrame invalidUsernameFrame = new JFrame();
               invalidUsernameFrame.setSize(400,75);
               invalidUsernameFrame.setLocation(550,250);
               JButton okButton = new JButton("Ok");
               Container invalidUsernameContainer =  invalidUsernameFrame.getContentPane();
               okButtonPanel.add(okButton);
               JLabel invalidUsernameLabel = new JLabel();
               invalidUsernameLabel.setText("                            Username is Available!");
               invalidUsernameContainer.add(invalidUsernameLabel);
               invalidUsernameContainer.add(okButtonPanel, BorderLayout.SOUTH);
               invalidUsernameFrame.setVisible(true);
               ActionListener okListener = new ActionListener(){
                  public void actionPerformed(ActionEvent ae){
                     myGame mainClass = new myGame();
                     invalidUsernameFrame.dispose();
                  }
               };
               okButton.addActionListener(okListener);
            }

            else{
               JPanel okButtonPanel = new JPanel();
               final JFrame invalidUsernameFrame = new JFrame();
               invalidUsernameFrame.setSize(400,75);
               invalidUsernameFrame.setLocation(550,250);
               JButton okButton = new JButton("Ok");
               Container invalidUsernameContainer = invalidUsernameFrame.getContentPane();
               okButtonPanel.add(okButton);
               JLabel invalidUsernameLabel = new JLabel();
               invalidUsernameLabel.setText("                         Username is not Available");
               invalidUsernameContainer.add(invalidUsernameLabel);
               invalidUsernameContainer.add(okButtonPanel, BorderLayout.SOUTH);
               invalidUsernameFrame.setVisible(true);
               ActionListener okListener = new ActionListener(){
                  public void actionPerformed(ActionEvent ae){
                     myGame mainClass = new myGame();
                     invalidUsernameFrame.dispose();
                  }
               };
               okButton.addActionListener(okListener);
            }
         }
      };
      ActionListener registerListener = new ActionListener(){
         public void actionPerformed(ActionEvent ae){
            frames myFrames = new frames();
            myFrames.registerFrame();
         }
      };
      cancelButton.addActionListener(cancelListener);
      myVariables.usernameField.addActionListener(usernameListener);
      myVariables.passwordField.addActionListener(passwordListener);
      myVariables.passwordCheckField.addActionListener(passwordCheckListener);
      registerButton.addActionListener(registerListener);

      checkUsernameButton.addActionListener(checkUsernameListener);
   }


}

面板类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class panels{


      buttons myButtons = new buttons();
      frames myFrames = new frames();
      variables myVariables = new variables();

   public JPanel loginScreenPanel(){

      buttons myButtons = new buttons();
      frames myFrames = new frames();
      variables myVariables = new variables();

      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(2,2));    
      panel.add(new JLabel("Username:"));  
      panel.add(myVariables.usernameFrame);
      panel.add(new JLabel("Password:"));
      panel.add(myVariables.passwordFrame);

      return panel;
   }

   public JPanel loginScreenButtonsPanel(){


      JPanel buttons = new JPanel();
      buttons.add(myButtons.loginButton);
      buttons.add(myButtons.registerButton);
      buttons.add(myButtons.cancelButton);


      return buttons;
   }

   public JPanel registerScreenPanel(){



      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(3,2));    
      panel.add(new JLabel("Username:"));  
      panel.add(myVariables.usernameField);
      panel.add(new JLabel("Password:"));
      panel.add(myVariables.passwordField);
      panel.add(new JLabel("Re-Enter Password:"));
      panel.add(myVariables.passwordCheckField);


      return panel;
   }

   public JPanel registerScreenButtonsPanel(){


      JPanel buttons = new JPanel();
      buttons.add(myButtons.registerButton);
      buttons.add(myButtons.checkUsernameButton);
      buttons.add(myButtons.cancelButton);


      return buttons;
   }
}

新代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ExampleGame {

java.util.HashMap<String,char[]> usernamesAndPasswords = new         java.util.HashMap<String,char[]>();
public static void main(String[] args) {
    new ExampleGame();
}

public ExampleGame() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            LoginPane pane = new LoginPane();
            storeInfo info = new storeInfo();
            int result = JOptionPane.showOptionDialog(null, pane, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new String[]{"Login", "Cancel"}, 0);
            if (result == 0) {

                User user = pane.getUser();
                // Perform the login...


                usernamesAndPasswords = info.storeInfo(user.name, user.password, usernamesAndPasswords);
                System.out.println("Name entered: " + user.name);
                System.out.print("Password entered: ");
                System.out.println(user.password);
                System.out.println(usernamesAndPasswords.get(user.name));

            }

        }
    });
}

public class LoginPane extends JPanel {

    private JTextField userName;
    private JPasswordField password;

    public LoginPane() {

        userName = new JTextField(10);
        password = new JPasswordField(10);

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(4, 4, 4, 4);
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("Username:"), gbc);

        gbc.gridx++;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(userName, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        gbc.fill = GridBagConstraints.NONE;
        add(new JLabel("Password:"), gbc);

        gbc.gridx++;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(password, gbc);

    }

    public User getUser() {

        return new User(userName.getText(), password.getPassword());

    }

}

public class User {

    private String name;
    private char[] password;

    public User(String name, char[] password) {
        this.name = name;
        this.password = password;
        }

    }

    public class storeInfo{

      public java.util.HashMap storeInfo(String name, char[] password, java.util.HashMap <String, char[]> usernamesAndPasswords){

         usernamesAndPasswords.put(name, password);
         return usernamesAndPasswords;
      }

    }

}

我在您的示例中添加了一个类,以使其将值存储在HashMap中,我想知道自己是否做得正确,或者是否还有其他更好的方法?现在,它确实将值存储在HashMap中,但是它给了我一个警告:注意:ExampleGame.java使用未经检查或不安全的操作. 注意:使用-Xlint:unchecked重新编译以获取详细信息.

I added a class to your example to get it to store the values in a HashMap, I'm wondering if I did this right or if there is some other better way to do it? Right now it does store the values in a HashMap, but it gives me a warning: Note: ExampleGame.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

我仔细阅读了您提供的链接,其中的某些链接没有任何意义,但我认为在大多数情况下都可以.在我开始尝试使其工作之前,我想确保我正确地完成了HashMap.

I read up on the links you gave, some of it didn't make sense but I think for the most part it did. just before I start going to try and get this to work I want to make sure I did that HashMap right.

我也获得您使用您编写的代码的许可吗?我自己也可以做类似的事情,但可能不会那么干净.

Also do I have your permission to use the code you made? I could make something similar myself but it probably wouldn't be as nice and clean.

推荐答案

基本上,您有一碗紧密相连的意大利面,在当前状态下,可能无法理清.

Basically, you have a tightly coupled bowl of spaghetti which in it's current state, probably can't be untangled.

您遇到的主要问题是您正在各处创建framespanelsbuttonsvariables的新实例.这意味着从您的应用程序的一部分到下一部分,它们都使用它们自己的这些类的实例,这些实例彼此不相关.

The main problem you have is the fact that you are creating new instance of frames, panels, buttons and variables all over the place. This means that from one part of your application to the next, they are all using their own instance of these classes, which don't relate to each other.

另一个问题是,应用程序的许多这些部分实际上不需要直接访问您尝试访问的对象.

The other problem is that many of these parts of the application actually shouldn't need direct access to the objects that you are try to access.

相反,首先将应用程序按职责划分为可用的组件/元素.

Instead, start by breaking down you application into usable components/elements, with defined responsibilities.

例如,UI部件应收集信息,这些信息将反馈到系统中进行处理.处理层不应在乎这些信息来自何处,而只是在乎它是否符合其需求.

For example, the UI parts should gather information which is feed back into the system for processing. The processing layer shouldn't care where this information came from, only that it conforms to it's needs.

例如...

以下仅生成一个登录对话框,该对话框将返回一个User对象,该对象代表用户键入的用户名和密码(这是一个非常基本的示例,因此验证很简单)

The following simply generates a login dialog which will return a User object which represents the username and password that the user typed in (this is a very basic example, so validation is light)

登录对话框并不关心登录过程如何发生,只负责获取信息

The login dialog does not care about how the login process occurs, only it is only responsible for getting the information

同样,应用程序的其他任何部分都不需要访问LoginPane中的字段,他们应该只关心结果

Equally, no other part of the application needs access to the fields within the LoginPane, they should only care about the result

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ExampleGame {

    public static void main(String[] args) {
        new ExampleGame();
    }

    public ExampleGame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                LoginPane pane = new LoginPane();
                int result = JOptionPane.showOptionDialog(null, pane, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new String[]{"Login", "Cancel"}, 0);
                if (result == 0) {

                    User user = pane.getUser();
                    // Perform the login...

                }

            }
        });
    }

    public class LoginPane extends JPanel {

        private JTextField userName;
        private JPasswordField password;

        public LoginPane() {

            userName = new JTextField(10);
            password = new JPasswordField(10);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.EAST;
            add(new JLabel("Username:"), gbc);

            gbc.gridx++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(userName, gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.fill = GridBagConstraints.NONE;
            add(new JLabel("Password:"), gbc);

            gbc.gridx++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(password, gbc);

        }

        public User getUser() {

            return new User(userName.getText(), password.getPassword());

        }

    }

    public class User {

        private String name;
        private char[] password;

        public User(String name, char[] password) {
            this.name = name;
            this.password = password;
        }

    }

}

您应该看一下Model-View-Control模式

You should take a look at the Model-View-Control pattern

  • MVC Pattern
  • Model–view–controller
  • Understanding Model-View-Controller

程序连接

  • Code to Interface, Access by name and Instance Data
  • Program to an interface, not an implementation

这篇关于不同类中的Java动作侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆