Witam. Mam kolejny problem. Krótko:
Chce zrobić GUI i stworzyłem sobie taki testowy model. Mam błędy dotyczące
Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:621)
at javax.swing.JFrame.setContentPane(JFrame.java:698)
at com.company.GUI.main(GUI.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Chce zrobić aby w GUI były wczytane modele konsol z klasy Konsola i aby byly wyświelone w comboboxie. W czym jest problem? Stworzyłem konstruktor aby probować włączyć GUI jednakże napotykam wieczne błędy. Proszę o pomoc.
package com.company;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created by Przemek on 31.12.2016.
*/
public class GUI {
private JPanel JPanel;
private JComboBox comboBox1;
public GUI(Konsola kon){
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox comboBox1 = new JComboBox(petStrings);
comboBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
comboBox1.setSelectedIndex(4);
}
});
}
public static void main(String[] args) {
Konsola kon = new Konsola("nic", 1 ,1 ,"100");
JFrame frame = new JFrame("GUI");
frame.setContentPane(new GUI(kon).JPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
package com.company;
public class Konsola extends Komponent {
public static void main(String[] args) {
Konsola[] konsole = new Konsola[6];
konsole[0]= new Konsola ("Play Station 1",100,200,"Sony Computer Entertainment");
konsole[1]= new Konsola ("Play Station 2",120,450,"Sony Computer Entertainment");
konsole[2]= new Konsola ("Play Station 3",125,700,"Sony Computer Entertainment");
konsole[3]= new Konsola ("Play Station 4",115,2000,"Sony Computer Entertainment");
konsole[3]= new Konsola ("Play Station 4",115,2000,"Sony Computer Entertainment");
konsole[3]= new Konsola ("Play Station 4",115,2000,"Sony Computer Entertainment");
konsole[3]= new Konsola ("Play Station 4",115,2000,"Sony Computer Entertainment");
for(int i=0; i<konsole.length ; i++)
System.out.println("Nazwa: "+ konsole[i].nazwa + " Waga: " + konsole[i].waga);
}
public Konsola(String nazwa, int waga, int cena, String producent) {
super(nazwa, waga, cena, producent);
}
}