Java Swing - JSlider 和 JCombobox 导致运行时错误
好的,我将发布这三个类的代码,因为它不太长。
package guiDemonstration;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TabbedGUI {
//create fields
private JFrame frame;
private JTabbedPane tabbedPane;
//make the following three labels public for access in the GUIEngine class
private JLabel comboLabel;
private JLabel sliderLabel;
private JLabel radioLabel;
private JSlider slider;
private JComboBox combo;
private JPanel comboPanel, sliderPanel, radioPanel;
private String []comboArray;
private JRadioButton radio, radio1, radio2;
private ButtonGroup buttonGroup;
private String comboText = "Please Make a Choice";
private String sliderText = "Move the Slider";
private String radioText = "Choose a Radio Button";
//Create field to hold GUIEngine Class
GUIEngine engine;
// empty constructor
public TabbedGUI(){
}
//method used to construct the gui
private void makeFrame(){
//create the Frame
frame = new JFrame("Example of a Tabbed GUI");
//set the initial size of the frame in pixels
frame.setSize(500, 200);
//add the frame to the contentPane
Container contentPane = frame.getContentPane();
//create an instance of the GUIEngine class
engine = new GUIEngine();
//create an array of size 3 to be used as dropdown values in the combobox
comboArray = new String[3];
//initialise the array
comboArray[0] = "First Choice";
comboArray[1] = "Second Choice";
comboArray[2] = "Third Choice";
//create instance of JComboBox and add array
combo = new JComboBox(comboArray);
//create instance of JSlider
slider = new JSlider();
//creat instance of Button Group
// this ButtonGroup will hold the individual radio buttons
buttonGroup = new ButtonGroup();
radio = new JRadioButton();
radio1 = new JRadioButton();
radio2 = new JRadioButton();
//create instances of JPanel
comboPanel = new JPanel();
sliderPanel= new JPanel();
radioPanel = new JPanel();
//create flowlayout for comboPanel
comboPanel.setLayout(new FlowLayout());
//create instances of labels
comboLabel = new JLabel(comboText);
sliderLabel=new JLabel(sliderText);
radioLabel = new JLabel(radioText);
//add radio buttons to the group
buttonGroup.add(radio);
buttonGroup.add(radio1);
buttonGroup.add(radio2);
//add a border to the button group
//begin creation of the tabbed GUI and add to contentPane
tabbedPane = new JTabbedPane();
contentPane.add(tabbedPane);
frame.add(tabbedPane);
//add instances of JPanel to each tab
tabbedPane.addTab("Combo Box", comboPanel);
tabbedPane.addTab("Slider", sliderPanel);
tabbedPane.addTab("Radio", radioPanel);
//add components to each JPanel of each tab
comboPanel.add(combo);
comboPanel.add(comboLabel);
sliderPanel.add(sliderLabel);
radioPanel.add(radioLabel);
sliderPanel.add(slider);
radioPanel.add(radio);
radioPanel.add(radio1);
radioPanel.add(radio2);
//set a border around the Slider
slider.setBorder(
BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLUE));
// call the method to add the listeners to each component
addListeners();
//by default the frame is set to invisible. Set the frame to visible
frame.setVisible(true);
}
/**
* This method adds listeners to each component
*/
public void addListeners(){
//add actionListeners to each component
combo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
engine.useCombo();
}
});
slider.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
engine.useSlider();
}
});
radio.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
engine.useRadioButtons();
}
});
radio1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
engine.useRadioButtons();
}
});
radio2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
engine.useRadioButtons();
}
});
}
/*
* The following three methods set the text for each
* label on the three individual tabs.
* These methods are called from the GUIEngine Class.
*/
//set the text on the comboLabel
public void setComboLabel(){
String updatedComboText = (String)combo.getSelectedItem();
comboLabel.setText(updatedComboText);
//System.out.println("You selected" + comboText);
}
//set the text on the sliderLabel
public void setSliderLabel(){
sliderLabel.setText("You've moved the slider!");
}
//set the text on the radioLabel
public void setRadioLabel(){
System.out.println("You've selected a radio button!");
}
/**
* This method is used to begin execution of the program
*/
public void runProgram(){
makeFrame();
}
}
类 2:
package guiDemonstration;
public class GUIEngine {
TabbedGUI tg;
//constructor
public GUIEngine(){
tg = new TabbedGUI();
}
public void useCombo(){
//System.out.println("You Used the Combo Box");
tg.setComboLabel();
}
public void useSlider(){
tg.setSliderLabel();
}
public void useRadioButtons(){
//System.out.println("You clicked a radio button");
tg.setRadioLabel();
}
}
只有 main 方法:
package guiDemonstration;
public class Controller {
/**
* #This is the main method where program execution begins
* @param args
*/
public static void main(String[] args) {
TabbedGUI tg = new TabbedGUI();
tg.runProgram();
}
}
JCombobox 和 JSlider 会导致运行时错误。代码编译正常,但是当我移动 JSlider 或选择 JComboBox 上的项目时,程序崩溃。
有什么想法吗?
GF
Ok, I will post the code for the three classes as it's not too long.
package guiDemonstration;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TabbedGUI {
//create fields
private JFrame frame;
private JTabbedPane tabbedPane;
//make the following three labels public for access in the GUIEngine class
private JLabel comboLabel;
private JLabel sliderLabel;
private JLabel radioLabel;
private JSlider slider;
private JComboBox combo;
private JPanel comboPanel, sliderPanel, radioPanel;
private String []comboArray;
private JRadioButton radio, radio1, radio2;
private ButtonGroup buttonGroup;
private String comboText = "Please Make a Choice";
private String sliderText = "Move the Slider";
private String radioText = "Choose a Radio Button";
//Create field to hold GUIEngine Class
GUIEngine engine;
// empty constructor
public TabbedGUI(){
}
//method used to construct the gui
private void makeFrame(){
//create the Frame
frame = new JFrame("Example of a Tabbed GUI");
//set the initial size of the frame in pixels
frame.setSize(500, 200);
//add the frame to the contentPane
Container contentPane = frame.getContentPane();
//create an instance of the GUIEngine class
engine = new GUIEngine();
//create an array of size 3 to be used as dropdown values in the combobox
comboArray = new String[3];
//initialise the array
comboArray[0] = "First Choice";
comboArray[1] = "Second Choice";
comboArray[2] = "Third Choice";
//create instance of JComboBox and add array
combo = new JComboBox(comboArray);
//create instance of JSlider
slider = new JSlider();
//creat instance of Button Group
// this ButtonGroup will hold the individual radio buttons
buttonGroup = new ButtonGroup();
radio = new JRadioButton();
radio1 = new JRadioButton();
radio2 = new JRadioButton();
//create instances of JPanel
comboPanel = new JPanel();
sliderPanel= new JPanel();
radioPanel = new JPanel();
//create flowlayout for comboPanel
comboPanel.setLayout(new FlowLayout());
//create instances of labels
comboLabel = new JLabel(comboText);
sliderLabel=new JLabel(sliderText);
radioLabel = new JLabel(radioText);
//add radio buttons to the group
buttonGroup.add(radio);
buttonGroup.add(radio1);
buttonGroup.add(radio2);
//add a border to the button group
//begin creation of the tabbed GUI and add to contentPane
tabbedPane = new JTabbedPane();
contentPane.add(tabbedPane);
frame.add(tabbedPane);
//add instances of JPanel to each tab
tabbedPane.addTab("Combo Box", comboPanel);
tabbedPane.addTab("Slider", sliderPanel);
tabbedPane.addTab("Radio", radioPanel);
//add components to each JPanel of each tab
comboPanel.add(combo);
comboPanel.add(comboLabel);
sliderPanel.add(sliderLabel);
radioPanel.add(radioLabel);
sliderPanel.add(slider);
radioPanel.add(radio);
radioPanel.add(radio1);
radioPanel.add(radio2);
//set a border around the Slider
slider.setBorder(
BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLUE));
// call the method to add the listeners to each component
addListeners();
//by default the frame is set to invisible. Set the frame to visible
frame.setVisible(true);
}
/**
* This method adds listeners to each component
*/
public void addListeners(){
//add actionListeners to each component
combo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
engine.useCombo();
}
});
slider.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
engine.useSlider();
}
});
radio.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
engine.useRadioButtons();
}
});
radio1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
engine.useRadioButtons();
}
});
radio2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
engine.useRadioButtons();
}
});
}
/*
* The following three methods set the text for each
* label on the three individual tabs.
* These methods are called from the GUIEngine Class.
*/
//set the text on the comboLabel
public void setComboLabel(){
String updatedComboText = (String)combo.getSelectedItem();
comboLabel.setText(updatedComboText);
//System.out.println("You selected" + comboText);
}
//set the text on the sliderLabel
public void setSliderLabel(){
sliderLabel.setText("You've moved the slider!");
}
//set the text on the radioLabel
public void setRadioLabel(){
System.out.println("You've selected a radio button!");
}
/**
* This method is used to begin execution of the program
*/
public void runProgram(){
makeFrame();
}
}
Class 2:
package guiDemonstration;
public class GUIEngine {
TabbedGUI tg;
//constructor
public GUIEngine(){
tg = new TabbedGUI();
}
public void useCombo(){
//System.out.println("You Used the Combo Box");
tg.setComboLabel();
}
public void useSlider(){
tg.setSliderLabel();
}
public void useRadioButtons(){
//System.out.println("You clicked a radio button");
tg.setRadioLabel();
}
}
And just the main method:
package guiDemonstration;
public class Controller {
/**
* #This is the main method where program execution begins
* @param args
*/
public static void main(String[] args) {
TabbedGUI tg = new TabbedGUI();
tg.runProgram();
}
}
The JCombobox and JSlider cause runtime errors. The code compiles ok, but when I move the JSlider or select an item on the JComboBox, the program crashes.
Any ideas?
GF
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 GUIEngine 正在创建自己的 TabbedGUI 实例,而不是使用您在 main 方法中创建的实例。
如果您需要保留 GUIEngine 类,我建议在 makeFrame 中执行此操作
然后更改 GUIEngine 构造函数以接受 TabbedGUI 作为参数。不妨也将实例变量设为最终变量。
Your GUIEngine is creating its own instance of the TabbedGUI rather than using the instance you create in your main method.
If you need to keep the GUIEngine class around, I would suggest doing this in makeFrame
Then change the GUIEngine constructor to take in a TabbedGUI as a parameter. Might as well make the instance variable final too.
这会导致 NullPointerException,这意味着 sliderLabel 尚未创建。确保执行以下操作:
在调用 setSliderLabel 之前
That is causing a NullPointerException which means that sliderLabel has not been created yet. Make sure that you do:
before the call to setSliderLabel