我正在尝试制作一个带有按钮的gui程序,当你点击它时,你会从1到6掷出一个模具,然后会出现一个模具图像。但是当我尝试生成1到6之间的数字时,会出现一个无法找到符号的错误。
int rolledNumber = random.nextInt(max - min + 1) + min;
我已经宣布最大和最小为6和1。
我也在尝试获得1到6之间的随机int的部分出错。而我用来打开图像的代码似乎是错误的。
`
img = ImageIO.read(new File("dice 1.jpeg"));
这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
public class Butttin {
public static void main(String[] args) {
JFrame frame = new JFrame("Rolling Dice Game");
JPanel panel = new JPanel();
JButton buttonRoll = new JButton("Roll!");
buttonRoll.addActionListener(new buttonRoll());
panel.setLayout(new GridLayout(5, 2, 5, 5));
frame.setVisible(true);
frame.setSize(500, 500);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
}
private static class buttonRoll implements ActionListener {
public void actionPerformed(ActionEvent event) {
int max = 6;
int min = 1;
int rolledNumber = random.nextInt(max - min + 1) + min;
String command = event.getActionCommand();
if (command == "Roll!") {
if (rolledNumber == 1) {
JLabel die = new JLabel();
img = ImageIO.read(new File("dice 1.jpeg"));
} else if (rolledNumber == 2){
JLabel die = new JLabel();
img = ImageIO.read(new File("dice 2.jpg"));
} else if (rolledNumber == 3){
JLabel die = new JLabel();
img = ImageIO.read(new File("dice 3.jpg"));
} else if (rolledNumber == 4){
JLabel die = new JLabel();
img = ImageIO.read(new File("dice 4.jpg"));
} else if (rolledNumber == 5){
JLabel die = new JLabel();
img = ImageIO.read(new File("dice 5.jpg"));
} else if (rolledNumber == 6){
JLabel die = new JLabel();
}
return die;
}
}
}
}
答案 0 :(得分:2)
random
不是有效的类或变量,我认为你的意思是java.util.Random
,你需要去掉变量并创建一个......的实例。
private static class ButtonRoll implements ActionListener {
private java.util.Random random = new java.util.Random();
接下来,if (command == "Roll!") {
不是您在Java中比较String
的方式,您应该使用String#equals
,例如if ("Roll!".equals(command)) {
接下来,img = ImageIO.read(new File("dice 1.jpeg"));
受到影响,部分来自同一问题。 img
未定义。此外,ImageIO.read
会抛出您未处理的IOException
。有关详细信息,请参阅JavaDocs
接下来,您要在die
语句的本地上下文中声明if
if (rolledNumber == 1) {
JLabel die = new JLabel();
img = ImageIO.read(new File("dice 1.jpeg"));
} else if (rolledNumber == 2) {
JLabel die = new JLabel();
这意味着当您尝试使用return
die ,
die`时,未定义。确保在上下文级别声明要在...
int rolledNumber = random.nextInt(max - min + 1) + min;
String command = event.getActionCommand();
JLabel die = null;
if ("Roll!".equals(command)) {
if (rolledNumber == 1) {
die = new JLabel();
img = ImageIO.read(new File("dice 1.jpeg"));
} else if (rolledNumber == 2) {
请注意,我不认为你想创建一个JLabel
,而是改变一个已有的标签icon
属性......
最后,ActionListener#actionPerformed
没有定义返回类型(它被定义为void
),这意味着return die;
是非法声明......
您必须找出允许buttonRoll
类更新UI的方法
另外,为什么我要解析您的代码,您确实应该确保只在事件调度线程的上下文中创建和修改您的UI,请参阅Initial Threads更多细节
因为我知道它会来......
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Butttin {
public static void main(String[] args) {
new Butttin();
}
public Butttin() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel output;
private JButton roll;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
output = new JLabel("-");
add(output, gbc);
roll = new JButton("Roll");
add(roll, gbc);
roll.addActionListener(new RollActionListener(output));
}
}
public class RollActionListener implements ActionListener {
private JLabel label;
private Random random = new Random();
public RollActionListener(JLabel label) {
this.label = label;
}
@Override
public void actionPerformed(ActionEvent e) {
int max = 6;
int min = 1;
int rolledNumber = random.nextInt(max - min + 1) + min;
label.setText(Integer.toString(rolledNumber));
}
}
}