我只想将组合框中的选定选项打印到文本字段。请解释什么是错的,因为我必须完成它&在课堂上解释。任何帮助将不胜感激。提前谢谢。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class App3 extends JFrame implements ActionListener
{
private JPanel boxPanel,textPanel;
private JLabel selectName,selectedName;
private JComboBox nameCombo;
private JTextField valueOfSelectedName;
private Container c;
public App3()
{
super("Combo example");
setup();
setSize(200,200);
setLocation(50,50);
show();
}
public void setup()
{
c = getContentPane();
boxPanel = new JPanel();
c.add(boxPanel,BorderLayout.NORTH);
selectName = new JLabel("Select Name : ");
selectedName = new JLabel("The selected Name : ");
String[] names = {"Ramila","Hashan","Shaad","Gus","Mahasen","Hasaru","Shabba"};
nameCombo = new JComboBox(names);
nameCombo.addActionListener(this);
valueOfSelectedName = new JTextField(10);
boxPanel.add(selectName);
boxPanel.add(nameCombo);
c.add(textPanel,BorderLayout.CENTER);
textPanel.add(selectedName);
textPanel.add(valueOfSelectedName);
}
public void actionPerformed(ActionEvent e)
{
JComboBox nameCombo = (JComboBox)e.getSource();
String newSelection = (String)nameCombo.getSelectedItem();
valueOfSelectedName.setText(newSelection);
}
public static void main(String args[])
{
App3 a = new App3();
}
}
我没有得到任何编译时错误,我在运行时遇到这些错误。
Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1041) at java.awt.Container.add(Container.java:927) at App3.setup(App3.java:42) at App3.(App3.java:16) at App3.main(App3.java:58) Process completed.
答案 0 :(得分:1)
private JPanel boxPanel,textPanel;
...
textPanel = new JPanel();
您还没有创建JPanel对象,因此textPanel指向null,这就是抛出异常的原因。创建对象,一切都应该正常工作
答案 1 :(得分:0)
当您尝试将其添加到内容窗格时,textPanel为null:
c.add(textPanel, BorderLayout.CENTER);
答案 2 :(得分:0)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class App3 extends JFrame implements ActionListener
{
private JPanel boxPanel,textPanel;
private JLabel selectName,selectedName;
private JComboBox nameCombo;
private JTextField valueOfSelectedName;
private Container c;
public App3()
{
super("Combo example");
setup();
setSize(200,200);
setLocation(50,50);
show();
}
public void setup()
{
c = getContentPane();
boxPanel = new JPanel();
c.add(boxPanel,BorderLayout.NORTH);
selectName = new JLabel("Select Name : ");
selectedName = new JLabel("The selected Name : ");
String[] names = {"Ramila","Hashan","Shaad","Gus","Mahasen","Hasaru","Shabba"};
nameCombo = new JComboBox(names);
nameCombo.addActionListener(this);
valueOfSelectedName = new JTextField(10);
boxPanel.add(selectName);
boxPanel.add(nameCombo);
textPanel = new JPanel(); // panel is not init that why its give error
c.add(textPanel,BorderLayout.CENTER);
textPanel.add(selectedName);
textPanel.add(valueOfSelectedName);
}
public void actionPerformed(ActionEvent e)
{
JComboBox nameCombo = (JComboBox)e.getSource();
String newSelection = (String)nameCombo.getSelectedItem();
valueOfSelectedName.setText(newSelection);
}
public static void main(String args[])
{
App3 a = new App3();
}
}