So I just began learning java last week and am starting to learn swing and GUI creation today when I ran into a problem. The below code worked for a while and then suddenly the panels stopped displaying and I can't seem to figure out why. Currently when you run the script it displays an empty box in the center of the screen. Was hoping some of you experts could help a beginner out as it seems like such a simple task...
GUI.java:
package Armory; import java.awt.GridLayout; import java.awt.Dimension; import java.awt.GraphicsEnvironment; import java.awt.Point; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class GUI extends JFrame { public static void informationGUI() { // Set up frames/panels and center frame. JFrame frame = new JFrame("Character Information"); JPanel container = new JPanel(); JPanel cPanel = new JPanel(); JPanel sPanel = new JPanel(); JPanel bPanel= new JPanel(); Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); int width = 400; int height = 150; frame.setBounds(center.x - width / 2, center.y - height / 2, width, height); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); // Set up all labels/buttons/text fields. JLabel cName = new JLabel("Character Name:", SwingConstants.RIGHT); JLabel cServer = new JLabel("Character Server:", SwingConstants.RIGHT); cName.setPreferredSize(new Dimension(115,20)); cServer.setPreferredSize(new Dimension(115, 20)); JTextField cNameTF = new JTextField(15); JTextField cServerTF = new JTextField(15); JButton retrieve = new JButton("Retrieve"); JButton cancel = new JButton("Cancel"); // Add labels/Text Fields to cPanel cPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); cPanel.add(cName); cPanel.add(cNameTF); // Add labels/Text Fields to sPanel sPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); sPanel.add(cServer); sPanel.add(cServerTF); // Add buttons to bPanel bPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); bPanel.add(retrieve); bPanel.add(cancel); // Add all panels to main container panel and display frame. container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); container.add(cPanel); container.add(sPanel); container.add(bPanel); frame.setVisible(true); } }
Entry.java
package Armory; public class Entry { public static void main(String args[]) { new GUI(); GUI.informationGUI(); } }
Thanks for taking the time to look at this for me.