Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Swing

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Where
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Swing

    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.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Swing

    a) No need to get the screen center point, just call setLocationRelativeTo(null) on the JFrame instance (typically do this after calling pack so the JFrame can use the layouts to size its components up)
    b) I do not see where you have added anything to the JFrame content pane.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Rootntootn (April 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Where
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing

    Thank you for the quick response.

    a) great information, works like a charm.
    b) whoops ;p Only gui I've ever created was in C# with the VB IDE so not use to having to add all these panels and then listeners and actions and so forth...


    Again thank you for your help.

Similar Threads

  1. How to display a Java method using Java Swing
    By IHeartProgramming in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 18th, 2011, 01:08 PM
  2. hi - Java swing errors
    By genlastudio in forum AWT / Java Swing
    Replies: 1
    Last Post: March 22nd, 2011, 05:49 PM
  3. Java swing app
    By luke88 in forum Java IDEs
    Replies: 1
    Last Post: March 19th, 2011, 09:49 PM
  4. [SOLVED] Java Swing problem?
    By nasser in forum AWT / Java Swing
    Replies: 2
    Last Post: July 3rd, 2010, 12:34 PM
  5. java swing help
    By JM_4ever in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 06:42 AM