我最近一直在使用JFrame,当我将它们放在一个类中时,它有一个简单的登录,注册和弹出框架。我想让它更好,而不是所有包装在一个类中,所以我为框架,按钮,面板,变量和主类创建了一个类。我的问题是框架本身工作正常并加载和显示,但按钮上的ActionListeners根本不起作用。当我按下按钮等时没有任何变化。我对Java很新,对JFrame和JButton来说还是新手。我可以做些什么来使这更简单或让我的代码看起来更好?代码将在每个单独的类中:
现在没有任何东西正在运行,即使在它应该调用LoginScreen()之前,main中的“This is running”也没有运行。我不确定我做了什么改变才能实现这个目标?
主类:
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重新编译:取消选中以获取详细信息。
我读到你给出的链接,其中一些没有意义,但我认为大部分都是这样。就在我开始尝试让它工作之前,我想确保我正确地完成了HashMap。
我是否也允许您使用您制作的代码?我可以自己制作类似的东西,但它可能不会那么漂亮和干净。
答案 0 :(得分:0)
基本上,你有一个紧密耦合的意大利面条碗,它的当前状态,可能无法解开。
您遇到的主要问题是您要在所有地方创建frames
,panels
,buttons
和variables
的新实例。这意味着从应用程序的一个部分到下一个部分,它们都使用它们自己的这些类的实例,这些实例彼此无关。
另一个问题是应用程序的许多部分实际上不需要直接访问您尝试访问的对象。
相反,首先将应用程序分解为具有已定义职责的可用组件/元素。
例如,UI部分应该收集反馈到系统中进行处理的信息。处理层不应该关心这些信息的来源,只要它符合它的需要。
例如......
以下只会生成一个登录对话框,该对话框将返回一个User
对象,该对象代表用户输入的用户名和密码(这是一个非常基本的示例,因此验证很轻)
登录对话框不关心登录过程是如何发生的,只是它只负责获取信息
同样,应用程序的其他任何部分都不需要访问LoginPane
中的字段,他们应该只关心结果
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;
}
}
}
您应该看看模型 - 视图 - 控制模式
编程接口