Reputation: 15
I am having trouble getting my GUI to display, and I am certain I am missing something very simple. This is for homework. The class that contains the GUI is here :
public class SorcGUI extends JFrame{
public void SorcGUI(){
JTextArea jta = new JTextArea();
JPanel jp1 = new JPanel();
setTitle ("Sorcerers Cave");
setSize (600, 600);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JButton jbsearch = new JButton("Search");
JTree jTree = null;
JScrollPane treeView = new JScrollPane(jTree);
JButton jbshow = new JButton("Show Contents");
JButton jbread = new JButton("Import File");
JScrollPane jsoutput = new JScrollPane(jta);
JTextField jtfsearch = new JTextField(" enter search term ");
JLabel jlsearch = new JLabel("Search Type : ");
JComboBox jcbsearch = new JComboBox <> ();
jcbsearch.addItem ("index");
jcbsearch.addItem ("Type");
jcbsearch.addItem ("weight");
//create a panel to hold the buttons and text fields
jp1.add(jbread);
jp1.add(jbshow);
jp1.add(jtfsearch);
jp1.add(jlsearch);
jp1.add(jcbsearch);
jp1.add(jbsearch);
jp1.add(treeView);
//add the components to the GUI
add(jp1, BorderLayout.PAGE_START);
add(jsoutput, BorderLayout.CENTER);
setVisible(true);
jbread.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SorcCave sc = new SorcCave();
}
});
jbshow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("show");
}
});
jbsearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("search");
}
});
And the way I am creating the GUI is simply adding the following to my main method:
SorcGUI gui = new SorcGUI();
The program simply goes form run to build successful in three seconds, never showing the GUI. I am trying to clean up my projects by getting the GUI stuff into it's own class file. Thank you for any help.
Upvotes: 0
Views: 151
Reputation: 159844
Remove the void
keyword from the constructor of SorcGUI
so the components can be added to the parent container
public SorcGUI() {
Read: Providing Constructors for Your Classes
Upvotes: 3
Reputation: 26
I copy-pasted your code and commented the jbread action method and it works in a new Java Project in Eclipse IDE.
I only removed the 'void' from the constructor method.
Upvotes: 1