gpt4 book ai didi

java - 我该怎么做才能让我的 Madlibs 程序从此登录/密码程序访问?

转载 作者:太空宇宙 更新时间:2023-11-04 07:40:35 25 4
gpt4 key购买 nike

(Stack 上的第一篇文章,哇!)我编写了一个 Madlibs 程序,它接收用户输入,并从中编写一个故事。我还制作了一个用户登录程序,我想使用 Mad libs 程序对其进行测试。它基本上工作得很好,但有一个问题,我会解决它。问题始于登录屏幕。一旦我输入正确的关键字(dodo 和 foo),登录屏幕就会登录,自行关闭,然后调用 MadLibsGUI 程序。但这是我的问题:当我这样做时,由于某种原因,MadLibsGUI 程序会生成两个窗口。我怀疑问题出在 MadLibsGUI 的 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() 中调用以下两次:

    logIn.addActionListener(this);

这意味着每次单击按钮时,actionPerformed() 方法都会执行两次。

关于java - 我该怎么做才能让我的 Madlibs 程序从此登录/密码程序访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16114676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com