我的教授告诉我把这两个放在一起制作一个正在运行的程序。我正在使用Netbeans,它一直告诉我没有主类。我应该创建一个还是我错过了什么?如何将这些组合到一个工作的gui java应用程序中?
这是第一个文件,名为NumberGame
import java.util.Random;
public class NumberGame {
private Random rand = new Random();
private int min, max;
private int num1, num2;
public NumberGame(int min, int max) {
this.min = min;
this.max = max;
newNums();
}
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
public void newNums() {
num1 = rand.nextInt(max - min + 1) + min;
num2 = rand.nextInt(max - min + 1) + min;
}
public int calcSum() {
return num1 + num2;
}
public boolean checkSum(int num) {
return num == calcSum();
}
}
第二个文件在这里被称为App
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class App extends JFrame implements ActionListener {
/* Canvas ============================================ */
private NumberGame numbers;
private Font mainFont = new Font(Font.SANS_SERIF, Font.PLAIN,16);
/* Components for Canvas ============================= */
private JTextField messageField;
private JLabel lblNum1;
private JLabel lblNum2;
private JTextField sumField;
private JButton btnNext;
private JButton btnCheck;
public App(String title, int width, int height) {
// initialize new number game
numbers = new NumberGame(10, 49);
// initialize components
messageField = new JTextField("", 10);
messageField.setEditable(false);
messageField.setHorizontalAlignment(JTextField.CENTER);
messageField.setFont(mainFont);
// add components to board
add(messageField, BorderLayout.NORTH);
add(createCenter(), BorderLayout.CENTER);
add(createButtonPanel(), BorderLayout.SOUTH);
// add action listeners
btnCheck.addActionListener(this);
btnNext.addActionListener(this);
sumField.addActionListener(this);
// create the window
createWindow(title, width, height);
pack();
}
private void createWindow(String title, int width, int height) {
setVisible(true);
setTitle(title);
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel createCenter() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
JLabel lblNumPrompt1 = new JLabel("Number 1 = ", JLabel.RIGHT);
lblNumPrompt1.setFont(mainFont);
lblNum1 = new JLabel(Integer.toString(numbers.getNum1()), JLabel.CENTER);
lblNum1.setFont(mainFont);
JLabel lblNumPrompt2 = new JLabel("Number 2 = ", JLabel.RIGHT);
lblNumPrompt2.setFont(mainFont);
lblNum2 = new JLabel(Integer.toString(numbers.getNum2()), JLabel.CENTER);
lblNum2.setFont(mainFont);
JLabel lblSumPrompt = new JLabel("Sum = ", JLabel.RIGHT);
lblSumPrompt.setFont(mainFont);
sumField = new JTextField("0", 10);
sumField.setHorizontalAlignment(JTextField.CENTER);
sumField.setFont(mainFont);
// add objects to the panel
panel.add(lblNumPrompt1);
panel.add(lblNum1);
panel.add(lblNumPrompt2);
panel.add(lblNum2);
panel.add(lblSumPrompt);
panel.add(sumField);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
btnNext = new JButton("Next");
btnCheck = new JButton("Check");
panel.add(btnNext);
panel.add(btnCheck);
return panel;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (button.equals(btnNext)) {
numbers.newNums();
lblNum1.setText(Integer.toString(numbers.getNum1()));
lblNum2.setText(Integer.toString(numbers.getNum2()));
} else {
int num = Integer.parseInt(sumField.getText());
if (numbers.checkSum(num))
messageField.setText("Correct!");
else
messageField.setText("Try Again!");
}
}
}
答案 0 :(得分:0)
只需创建一个主类并初始化app类,如下所示:HTH。
public static void main(String args[])
{
//Put in the title and size of the Panel
App app1 = new App("MY game", 1000, 900);
}
答案 1 :(得分:0)