過去数週間、他のいくつかの単純な GUI プロジェクトを行ってきましたが、ほとんど問題はありませんでしたが、コンパイル後にプログラムを表示するのに苦労しています。エラーがスローされることはなく、学校で与えられた他のプロジェクトとほぼ同じ形式であるため、何が問題なのかわかりません。
これを提出する前に、必要なものを調べて微調整できるように、画面上で GUI を表示する場所についてのガイダンスをいただければ幸いです。
import java.util.Random;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/***********************************************************************
Program Name: ProgramName.java
Programmer's Name: Student Name
Program Description: Describe here what this program will do
***********************************************************************/
public class GuessGame extends JFrame{
//Declare GUI components
private JFrame mainFrame;
private JButton guessButton;
private JButton exitButton;
private JTextField guessField;
private JTextField answerField;
private JLabel directionsLabel;
private JLabel guessLabel;
private JLabel answerLabel;
public GuessGame(){
//Initialize window components
mainFrame = new JFrame("Number Guessing Game");
exitButton = new JButton("Exit");
guessButton = new JButton("Try your luck");
guessField = new JTextField(4);
answerField = new JTextField(50);
guessLabel = new JLabel("What is your guess?");
answerLabel = new JLabel("Now it is/isn't");
directionsLabel = new JLabel("Enter a number and then press the" +
"guess button until you are correct");
//Build the GUI
Container c = mainFrame.getContentPane();
c.setLayout(new FlowLayout());
c.add(directionsLabel);
c.add(guessLabel);
c.add(guessField);
c.add(answerLabel);
c.add(answerField);
c.add(guessButton);
c.add(exitButton);
//Set Mnemonics
guessButton.setMnemonic('G');
exitButton.setMnemonic('E');
mainFrame.setSize(450, 300);
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//Call the handler methods for specific functions
GuessButtonHandler ghandler = new GuessButtonHandler();
guessButton.addActionListener(ghandler);
ExitButtonHandler ehandler = new ExitButtonHandler();
exitButton.addActionListener(ehandler);
FocusHandler fhandler = new FocusHandler();
guessField.addFocusListener(fhandler);
answerField.addFocusListener(fhandler);
}
//Implement actionListener for the Guess button
class GuessButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
String instring;
int counter = 0;
int guess;
Random rand = new Random();
int numberToGuess = rand.nextInt(1000);
instring = guessField.getText();
guess = Integer.parseInt(instring);
counter++;
if (guess == numberToGuess){
answerLabel = new JLabel("You win! " +
"\nThe number was: " + numberToGuess +
"\nIt took you " + counter + " tries");
}
else if (guess < numberToGuess){
answerLabel = new JLabel("Too low");
}
else if (guess > numberToGuess){
answerLabel = new JLabel("Too high");
}
}
}
//Implement ActionListener for the exit button
class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
//Implement focus listener
class FocusHandler implements FocusListener{
public void focusGained(FocusEvent e){
if (e.getSource()== guessField){
answerField.setText("");
}
else if (e.getSource() == answerField){
guessButton.requestFocus();
}
}
public void focusLost(FocusEvent arg0) {
}
}
//Main to run program, call GuessGame method
public static void main(String[] args) {
new GuessGame();
}
}