1

(关于 Stack 的第一篇文章,woopee!)我编写了一个 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();
    }

}
4

1 回答 1

1

您在方法中调用以下两次start()

    logIn.addActionListener(this);

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

于 2013-04-19T23:23:09.473 回答