我想更改JLabel在运行时期间正在查看的图像,但是当我按下应该执行操作的魔术按钮时,我会继续获取NullPointerExceptions或没有任何反应。它甚至可以在Java中使用吗?
以下是我的完整代码:
import java.text.*;
import javax.swing.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Shell implements ActionListener, MenuKeyListener
{
JFrame frame;
JWindow window;
JButton PSubmit;
JPanel pane1, pane2;
JRadioButton R1, R2, R3;
ButtonGroup PGroup;
JTabbedPane layout;
String result;
String border = "Border.png";
String DF = "Frame.png";
String list [];
Driver driver;
public Shell()
{
driver = new Driver();
list = new String [6];
}
public void setFrame()
{
frame = new JFrame("Pokemon Program 3 by Systems Ready");
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.getContentPane().setLayout(new BorderLayout());
}
public void frameLayout()
{
layout = new JTabbedPane();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
JLabel label = new JLabel("Please choose the restrictions:");
JLabel imgLabel1 = new JLabel(new ImageIcon(border));
JLabel notiLabel1 = new JLabel("The Pokemon chosen with these restrictions are: ");
JLabel notiLabel2 = new JLabel("'No Restrictions': No restrictions for the kind of Pokemon chosen based on species or items.");
JLabel notiLabel3 = new JLabel("'Battle Revolution': All Pokemon must have unique items.");
JLabel notiLabel4 = new JLabel("'Battle Tower': All Pokemon must have unique items, Uber and Event Legendaries banned.");
JLabel label2 = new JLabel("Please choose possible Pokemon:");
pane1.add(label);
pearlButtons();
pane1.add(R1);
pane1.add(R2);
pane1.add(R3);
pane1.add(PSubmit);
pane1.add(notiLabel2);
pane1.add(notiLabel3);
pane1.add(notiLabel4);
pane1.add(imgLabel1);
pane1.add(notiLabel1);
JLabel pokeLabel1 = new JLabel(new ImageIcon(DF));
JLabel pokeLabel2 = new JLabel(new ImageIcon(DF));
JLabel pokeLabel3 = new JLabel(new ImageIcon(DF));
JLabel pokeLabel4 = new JLabel(new ImageIcon(DF));
JLabel pokeLabel5 = new JLabel(new ImageIcon(DF));
JLabel pokeLabel6 = new JLabel(new ImageIcon(DF));
pane1.add(pokeLabel1);
pane1.add(pokeLabel2);
pane1.add(pokeLabel3);
pane1.add(pokeLabel4);
pane1.add(pokeLabel5);
pane1.add(pokeLabel6);
pane2.add(label2);
layout.add("Pearl Version", pane1);
layout.add("SoulSilver Version", pane2);
frame.add(layout);
}
public void pearlButtons()
{
PGroup = new ButtonGroup();
R1 = new JRadioButton("No Restrictions", true);
R1.setActionCommand("N");
R1.setVisible(true);
R2 = new JRadioButton("Battle Revolution");
R2.setActionCommand("BR");
R2.setVisible(true);
R3 = new JRadioButton("Battle Tower");
R3.setActionCommand("B");
R3.setVisible(true);
PGroup.add(R1);
PGroup.add(R2);
PGroup.add(R3);
PSubmit = new JButton("Submit");
PSubmit.setActionCommand("pstart");
PSubmit.setVisible(true);
PSubmit.addActionListener(this);
}
public void pearlProcessing()
{
//The "list" array has a bunch of string names that get .png affixed to them (and I named the image files as such when I name them)
String file1 = list[0] + ".png";
String file2 = list[1] + ".png";
String file3 = list[2] + ".png";
String file4 = list[3] + ".png";
String file5 = list[4] + ".png";
String file6 = list[5] + ".png";
/*-------------------------------------------------------------------------------//
This is where the method's supposed to go to change the image...
I've tried pokeLabel = new JLabel(new ImageIcon(file1));, but that yields a NullPointerException.
//-----------------------------------------------------------------------------------*/
}
public static void main(String[] args)
{
Shell test = new Shell();
test.setFrame();
test.frameLayout();
test.frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if ("pstart".equals(e.getActionCommand()))
{
result = PGroup.getSelection().getActionCommand();
if (result.equals("N"))
{
list = driver.Prandom();
pearlProcessing();
}
else
System.out.println("Not done yet! ;)");
}
}
public void menuKeyPressed(MenuKeyEvent e)
{
System.out.println("pressed");
}
public void menuKeyReleased(MenuKeyEvent e)
{
System.out.println("menuKeyReleased");
}
public void menuKeyTyped(MenuKeyEvent e)
{
System.out.println("menuKeyTyped");
}
}
答案 0 :(得分:2)
如果不知道这是否是原因,我会改变
result = PGroup.getSelection().getActionCommand();
if (result.equals("N")) {
到
ButtonModel selection = PGroup.getSelection();
result = (selection==null ? null : selection.getActionCommand());
if ("N".equals(result)) {
// etc...
这可以防止可能的空指针。
答案 1 :(得分:0)
我强烈建议每当你得到任何异常总是查看堆栈跟踪并查看异常发生的实际行在哪里。
但如果我推测,请在PGroup.getSelection()
方法中致电actionPerformed
。如果您的单选按钮组中未选择任何按钮,则此方法可能返回null
。我还注意到你没有设置最初选择的单选按钮(在单选按钮上调用setSelected(true)
)。因此,如果所选单选按钮为null
,则调用getActionCommand
将导致NullPointerException
。