我修复了代码并进行了更新,但现在我不知道在哪里放置构造函数:
private JPanel panel;
public RollButton(JPanel panel){
this.panel = panel;
}
我查看了一些放置构造函数的示例,看起来它放在我的RollButton
类附近,但我试图将它放在多个地方并且在多个地方,但我似乎无法得到它没错。
代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
public class DiceRollGUI {
public static JLabel label;
public static JFrame frame;
public static JPanel panel;
private static JButton button;
private static JButton buttonRollDie;
private static JLabel diceRoll;
public static void main (String[] args) {
JFrame frame = new JFrame("Dice Roll GUI");
JPanel contentPanel = new JPanel(new GridLayout(0,2,5,10));
button = new JButton("Roll");;
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(750, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setActionCommand("Roll");
button.addActionListener(new RollButton());
panel.setPreferredSize(new Dimension(750, 500));
frame.setContentPane(panel);
frame.pack();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(button);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
}
private static class RollButton implements ActionListener {
public void actionPerformed(ActionEvent event){
int roll = (int) (Math.round((Math.random() * 5) + 1));
ImageIcon dice = null;
if(roll == 1){
dice = new ImageIcon("DiceRollGUI Pictures/dice_face_1.png");
}
else if(roll == 2){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_2.png");
}
else if(roll == 3){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_3.png");
}
else if(roll == 4){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_4.png");
}
else if(roll == 5){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_5.png");
}
else if(roll == 6){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_6.png");;
}
JLabel diceRoll = new JLabel("",dice, JLabel.CENTER);
panel.add(diceRoll);
panel.revalidate();
}
}
}
答案 0 :(得分:7)
编辑:这是一个NullPointerException,或者像我们中的一些人所说的那样,NPE。
这里的关键是学习如何修复大多数NPE:
at DiceRollGUI$RollButton.actionPerformed(DiceRollGUI.java:40)
。在您的情况下,您的JLabel标签为null,因为您永远不会初始化它。在尝试使用它之前,您需要为其分配一个新的JLabel对象。
另外,正如我在评论中提到的,将来,如果您询问有关异常或错误的问题,请始终发布完整的异常或错误消息,并指出哪条行导致了问题。错误消息通常会告诉您确切的行,但您必须将行号转换为程序中的实际行。
答案 1 :(得分:6)
这是一个简单的NullPointerException
,应该通过检查源代码中异常原因的最近行和/或调试程序来修复
当我运行该程序时,我得到以下异常......
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at dicerollgui.DiceRollGUI$RollButton.actionPerformed(DiceRollGUI.java:55)
在label.setText(" ");
方法中我指向了actionPerformed
...
if(rolldie.equals("Roll")) {
label.setText(" ");
这自动告诉我label
尚未初始化(因此null
)。
<强>更新... 强>
此...
JFrame frame = new JFrame();
frame.add(dice6);
对我来说似乎很奇怪。基本上在每个骰子上,你创建了一个新的JLabel
和一个新的JFrame
,但是你没有显示结果框...
相反,您应该只有一个JLabel
来维护最后一个卷的值,只需使用setText
或setIcon
根据您的要求更改其状态。
更新了示例
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DiceRoll {
public static void main(String[] args) {
new DiceRoll();
}
public DiceRoll() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new DicePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DicePane extends JPanel {
private JLabel diceRoll;
private JButton rollDice;
public DicePane() {
setLayout(new BorderLayout());
diceRoll = new JLabel();
Font font = diceRoll.getFont();
diceRoll.setFont(font.deriveFont(128f));
diceRoll.setHorizontalAlignment(JLabel.CENTER);
add(diceRoll);
rollDice = new JButton("Papper needs a new pair of shoes");
rollDice.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int roll = (int) (Math.round((Math.random() * 5) + 1));
diceRoll.setText(Integer.toString(roll));
}
});
add(rollDice, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}