Java 如何使Madlibs程序可以从此登录/密码程序访问?
(堆栈上的第一个帖子,woopee!) 我编写了一个Madlibs程序,它接受用户输入,并将其编成故事。我还制作了一个用户登录程序,我想用Mad libs程序测试它。它基本上工作正常,但有一个问题,我会解决它。 问题从登录屏幕开始。一旦我输入了正确的关键字(dodo和foo),登录屏幕就会登录并关闭,然后调用MadLibsGUI程序。但我的问题是:当我这样做时,出于某种原因,MadLibsGUI程序会生成两个窗口。我怀疑问题在于MadLibsGUI的Java 如何使Madlibs程序可以从此登录/密码程序访问?,java,swing,user-interface,window,Java,Swing,User Interface,Window,(堆栈上的第一个帖子,woopee!) 我编写了一个Madlibs程序,它接受用户输入,并将其编成故事。我还制作了一个用户登录程序,我想用Mad libs程序测试它。它基本上工作正常,但有一个问题,我会解决它。 问题从登录屏幕开始。一旦我输入了正确的关键字(dodo和foo),登录屏幕就会登录并关闭,然后调用MadLibsGUI程序。但我的问题是:当我这样做时,出于某种原因,MadLibsGUI程序会生成两个窗口。我怀疑问题在于MadLibsGUI的main方法。我已经试着修好了,但似乎不起作用
main
方法。我已经试着修好了,但似乎不起作用。这个程序运行得很好,但这两个窗口确实让我很烦恼。我将在下面发布这两个代码类,供您阅读和查看。两者都相当简单(我是一个初学者程序员),所以您不应该对它们有太大的问题。如果您有任何其他意见或更正,请随时更正
登录筛选:
package passwordProgram;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import madLibs.MadLibsGUI;
public class LogInScreen implements ActionListener {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
LogInScreen logger = new LogInScreen();
logger.start();
}
JButton logIn;
JFrame frame;
JTextField username;
JPasswordField password;
JLabel title;
public void start() {
frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
JButton logIn = new JButton("Log In");
logIn.addActionListener(this);
title = new JLabel("Welcome to the Username/Password System");
JLabel usernameTxt = new JLabel("Username: ");
username = new JTextField(15);
JLabel passwordTxt = new JLabel("Password: ");
password = new JPasswordField(15);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.SOUTH, logIn);
frame.getContentPane().add(BorderLayout.NORTH, title);
panel.setLayout(new GridBagLayout());
GridBagConstraints left = new GridBagConstraints();
left.anchor = GridBagConstraints.EAST;
GridBagConstraints right = new GridBagConstraints();
right.weightx = (int) 2;
right.fill = GridBagConstraints.HORIZONTAL;
right.gridwidth = GridBagConstraints.REMAINDER;
panel.add(usernameTxt, left);
panel.add(passwordTxt, right);
panel.add(username, right);
panel.add(passwordTxt, left);
panel.add(password, right);
logIn.addActionListener(this);
frame.setVisible(true);
frame.setSize(500, 300);
}
public void actionPerformed(ActionEvent event) {
if (username.getText().equals("dodo") && new String(password.getPassword()).equals("foo")) {
MadLibsGUI mLibs = new MadLibsGUI();
mLibs.start();
frame.setVisible(false);
} else {
title.setText("Invalid username/password. Please try again.");
}
}
}
MadLibsGUI类:
package madLibs;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class MadLibsGUI implements ActionListener {
JFrame frame;
JPanel panel;
JTextField nameTxt;
JTextField verbTxt1;
JTextField adjTxt;
JTextField verbTxt2;
JTextField nounTxt;
JTextArea story;
public void start() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton madLibButton = new JButton("Lib it!");
story = new JTextArea();
JLabel title = new JLabel("Welcome to mad libs! \n Put in your words and press the 'Lib It' button to play!");
JLabel nameLabel = new JLabel("Name: ");
JLabel verbLabel1 = new JLabel("Verb: ");
JLabel adjLabel = new JLabel("Adjective: ");
JLabel verbLabel2 = new JLabel("Verb: ");
JLabel nounLabel = new JLabel("Noun: ");
nameTxt = new JTextField(25);
verbTxt1 = new JTextField(25);
adjTxt = new JTextField(25);
verbTxt2 = new JTextField(25);
nounTxt = new JTextField(25);
frame.getContentPane().add(BorderLayout.SOUTH, story);
frame.getContentPane().add(BorderLayout.NORTH, title);
panel.setLayout(new GridBagLayout());
panel.setBackground(Color.green);
frame.getContentPane().add(panel);
GridBagConstraints left = new GridBagConstraints();
left.anchor = GridBagConstraints.EAST;
GridBagConstraints right = new GridBagConstraints();
right.weighty = 1.2;
GridBagConstraints middle = new GridBagConstraints();
middle.anchor = GridBagConstraints.CENTER;
right.fill = GridBagConstraints.HORIZONTAL;
right.gridwidth = GridBagConstraints.REMAINDER;
panel.add(nameLabel, left);
panel.add(nameTxt, right);
panel.add(verbLabel1, left);
panel.add(verbTxt1, right);
panel.add(adjLabel, left);
panel.add(adjTxt, right);
panel.add(verbLabel2, left);
panel.add(verbTxt2, right);
panel.add(nounLabel, left);
panel.add(nounTxt, right);
panel.add(madLibButton, right);
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
frame.pack();
frame.setVisible(true);
frame.setSize(615, 500);
madLibButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
String text = ("\tThere once was a boy named " + nameTxt.getText() + " who loved to " + verbTxt1.getText()
+ ". \n\tOne day, " + nameTxt.getText() + " was walking down the street when he saw a " +
adjTxt.getText() + " bird who \n\twas hurt. He quietely said, \" It's okay \n\tlittle bird, I " +
"won't hurt you!\" Instead, " + nameTxt.getText() + "\n\tdecided that he was going " +
" to " + verbTxt2.getText() + " the bird! Sadly, the bird \n\t" + verbTxt2.getText() + "ed too" +
" much. " + nameTxt.getText() + " was very sad. \n\tHe sat in his room, playing with his " +
nounTxt.getText() + ". \n\n \t\t\t|THE END|");
story.append(text);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
MadLibsGUI main = new MadLibsGUI();
main.start();
}
}
在方法
start()
中调用以下两次:
这意味着每次单击按钮时,将执行两次
actionPerformed()
方法。在方法start()
中调用以下两次:
这意味着每次单击按钮时,
actionPerformed()
方法将执行两次。谢谢,非常感谢。如果可以的话,我会投赞成票,但我没有代表。谢谢,我很感激。如果可以的话我会投票,但我没有代表。
logIn.addActionListener(this);