Java-GUI赢得';编译后不运行
在过去的几周里,我已经完成了一些其他简单的GUI项目,没有什么问题,但是现在我在编译后让程序显示方面遇到了困难。我不知道什么可能是错误的,因为没有错误被抛出,而且它的格式与我在学校得到的其他项目几乎相同 任何关于如何在屏幕上显示GUI的指导都将不胜感激,这样我就可以在上交之前浏览并浏览我想要的东西Java-GUI赢得';编译后不运行,java,user-interface,Java,User Interface,在过去的几周里,我已经完成了一些其他简单的GUI项目,没有什么问题,但是现在我在编译后让程序显示方面遇到了困难。我不知道什么可能是错误的,因为没有错误被抛出,而且它的格式与我在学校得到的其他项目几乎相同 任何关于如何在屏幕上显示GUI的指导都将不胜感激,这样我就可以在上交之前浏览并浏览我想要的东西 import java.util.Random; import java.awt.Container; import java.awt.FlowLayout; import java.awt.even
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();
}
}
import java.util.Random;
导入java.awt.Container;
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.FocusEvent;
导入java.awt.event.FocusListener;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
/***********************************************************************
程序名:ProgramName.java
程序员姓名:学生姓名
程序描述:在此描述此程序将执行的操作
***********************************************************************/
公共类猜谜游戏扩展JFrame{
//声明GUI组件
专用JFrame主机;
私有JButton按钮;
私有JButton exitButton;
私有JTextField猜测字段;
私人JTextField answerField;
私人JLabel方向标签;
私人JLabel标签;
私人JLabel-answerLabel;
公众猜谜游戏{
//初始化窗口组件
大型机=新JFrame(“数字猜测游戏”);
exitButton=新JButton(“退出”);
猜按钮=新的JButton(“试试你的运气”);
猜测字段=新的JTextField(4);
answerField=新的JTextField(50);
guessLabel=new JLabel(“您的猜测是什么?”);
answerLabel=new JLabel(“现在是/不是”);
directionsLabel=new JLabel(“输入一个数字,然后按”+
“猜猜按钮直到你猜对为止”);
//构建GUI
容器c=mainFrame.getContentPane();
c、 setLayout(新的FlowLayout());
c、 添加(方向标签);
c、 添加(标签);
c、 添加(猜测字段);
c、 添加(应答标签);
c、 添加(应答字段);
c、 添加(猜测按钮);
c、 添加(退出按钮);
//设置助记符
猜按钮。设置助记符('G');
exitButton.set助记符('E');
大型机。设置大小(450300);
mainFrame.addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
//为特定函数调用处理程序方法
GuessButtonHandler ghandler=新的GuessButtonHandler();
guessButton.addActionListener(ghandler);
ExitButtonHandler ehandler=新的ExitButtonHandler();
addActionListener(ehandler);
FocusHandler fhandler=新的FocusHandler();
addFocusListener(fhandler);
addFocusListener(fhandler);
}
//为猜测按钮实现actionListener
类GuessButtonHandler实现ActionListener{
已执行的公共无效操作(操作事件e){
弦安装;
int计数器=0;
智力猜测;
Random rand=新的Random();
int numberToGuess=rand.nextInt(1000);
instring=guessField.getText();
guess=Integer.parseInt(instring);
计数器++;
如果(猜测==数字猜测){
answerLabel=new JLabel(“你赢了!”+
“\n号码是:”+numberToGuess+
“\n它占用了您”+计数器+“尝试”);
}
else if(猜测<数字猜测){
answerLabel=新的JLabel(“过低”);
}
否则如果(猜测>数字猜测){
answerLabel=新的JLabel(“过高”);
}
}
}
//为退出按钮实现ActionListener
类ExitButtonHandler实现ActionListener{
已执行的公共无效操作(操作事件e){
系统出口(0);
}
}
//实现焦点侦听器
类FocusHandler实现FocusListener{
获得公共无效焦点(焦点事件e){
如果(例如getSource()==guessField){
answerField.setText(“”);
}
else if(e.getSource()==应答字段){
questFocus();
}
}
public void focusLost(FocusEvent arg0){
}
}
//主运行程序,调用猜游戏方法
公共静态void main(字符串[]args){
新猜谜游戏();
}
}
将所有组件添加到JFrame后,您需要在JFrame上调用setVisible(true)
。程序不会从数学上知道您想要显示任何内容,除非您明确告诉它这样做
i、 e
public GuessGame() {
// Initialize window components
mainFrame = new JFrame("Number Guessing Game");
// ..... etc....
FocusHandler fhandler = new FocusHandler();
guessField.addFocusListener(fhandler);
answerField.addFocusListener(fhandler);
mainFrame.setVisible(true);
}
另外,您不想让这个类扩展JFrame,因为这不是必需的。您已经在构造函数中使用了JFrame,因此不需要在类本身中使用另一个JFrame。将所有组件添加到JFrame后,您需要在JFrame上调用
setVisible(true)
。程序不会从数学上知道您想要显示任何内容,除非您明确告诉它这样做
i、 e
public GuessGame() {
// Initialize window components
mainFrame = new JFrame("Number Guessing Game");
// ..... etc....
FocusHandler fhandler = new FocusHandler();
guessField.addFocusListener(fhandler);
answerField.addFocusListener(fhandler);
mainFrame.setVisible(true);
}
另外,您不想让这个类扩展JFrame,因为这不是必需的。您已经在构造函数中使用了一个JFrame,因此不需要在类本身中使用另一个JFrame。知道它是如此简单。我可以发誓它就在那里,但我没意识到在这段时间之后它就不是了。非常感谢。至于扩展的JFrame,我不太清楚这一部分。我想这是我认为需要的东西,就像我们在其他实验室做的一样,尽管有其他的JFrame在里面。我会的