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();
}
}
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();
}
}
그리고 단지 주요 방법 :
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
해결책
GuiEngine은 기본 메소드에서 작성한 인스턴스를 사용하지 않고 TabbedGui의 자체 인스턴스를 작성합니다.
Guiengine 클래스를 유지해야한다면 MakeFrame 에서이 작업을 수행하는 것이 좋습니다.
엔진 = 새로운 guiengine (this);
그런 다음 guiengine 생성자를 변경하여 TabbedGui를 매개 변수로 가져갑니다. 인스턴스 변수를 최종적으로 만들 수도 있습니다.
다른 팁
sliderLabel.setText("You've moved the slider!");
이는 NullPointerException을 유발하여 SliderLabel이 아직 생성되지 않았 음을 의미합니다. 당신이하는지 확인하십시오 :
sliderLabel = new JLabel()
SetSliderLabel로 호출하기 전에
제휴하지 않습니다 StackOverflow