(Stack の最初の投稿、woopee!) ユーザー入力を取り込み、そこからストーリーを作成する Madlibs プログラムを作成しました。また、Mad libs プログラムを使用してテストしたいユーザー ログイン プログラムも作成しました。ほとんど問題なく動作しますが、問題が 1 つあります。問題はログイン画面から始まります。適切なキーワード (dodo と foo) を入力すると、ログイン画面がログインし、オフになり、MadLibsGUI プログラムが呼び出されます。しかし、ここに問題があります。これを行うと、何らかの理由で MadLibsGUI プログラムが 2 つのウィンドウを生成します。問題が MadLibsGUI にあるのではないかと疑っていますmain
方法 。私はすでにそれを修正しようとしましたが、うまくいかないようです。プログラムは正常に動作しますが、2 つのウィンドウが気になります。読んで見られるように、両方のコード クラスを以下に掲載します。どちらもかなり単純なので (私は初心者のプログラマーです)、それほど問題にならないはずです。追加のコメントや修正がある場合は、遠慮なく修正してください。
ログイン画面:
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();
}
}