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 7 of 7

Thread: Java GUI

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java GUI

    Hello, im working on a battleship game in java and i'm currently working with the graphical part of the program. Im trying to make a JPanel that i will fill with JComboBox. The JComboBox will have some preset values 0-7 that allows me to select different positions in an 2d array that i will assign with boats. However i cant get it to show. Keep in mind that my code isn't completely done and this is only a window to set player information.

    My code:
    import java.awt.Container;
    import java.awt.GridLayout;
     
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
     
    public class WelcomeWindow extends JFrame {
     
    	private Container contentPanel;
    	private JButton okButton;
     
    	public WelcomeWindow(){
    		configureFrame();
    		initiateInstanceVariables();
    		addComponentsToFrame();
    	}
     
    	private void configureFrame(){
    		this.setSize(400, 600);
    		this.setLocationRelativeTo(null);
    		this.setResizable(false);
    		this.setTitle("Add Player");
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		setVisible(true);
    		}
     
    	private void addComponentsToFrame(){
    		JPanel dropDownPanel = new JPanel();
    		dropDownPanel.setLayout(null);
    		dropDownPanel.setBorder(BorderFactory.createTitledBorder("Pos for ships"));
     
    		this.contentPanel.add(this.okButton);
    		JPanel aPanel= buildComboBoxes();
    		dropDownPanel.add(aPanel);
     
    		this.contentPanel.add(dropDownPanel);
    	}
     
    	private void initiateInstanceVariables(){
    	this.okButton = new JButton("Ok");
    	this.okButton.setBounds(150, 500, 100, 40);
     
    	this.contentPanel = this.getContentPane();
    	this.contentPanel.setLayout(null);
    	}
     
    	private JPanel buildComboBoxes(){
    		JPanel comboPanel = new JPanel();
    		comboPanel.setLayout(new GridLayout(4,2,00,10));
    		comboPanel.setBounds(0,100,400,200);
     
    		String[] avaliblePos = {"0","1","2","3","4","5","6","7"};
    		JComboBox xyPos = null;
    		for(int i=0;i<avaliblePos.length;i++){
    			xyPos = new JComboBox(avaliblePos);
    		}
    		comboPanel.add(xyPos);
     
     
    		return comboPanel;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Java GUI

    my code isn't completely done and this is only a window to set player information.
    Does the posted code: compile, execute and show the problem?

    What layout manager does the code use for components that are added to a container? There can be several different ones involved and each one needs special usage.
    If you don't understand my answer, don't ignore it, ask a question.

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

    sebbe605 (March 20th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI

    Yes the code compiles and the window pops up just as its "suppose" to.
    JAVA.jpg
    However i don't think that the gridlayout is working properly. This is my first project in java with GUI so im having some problems to get it working the way i want to, i hope that is understandable .

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Java GUI

    The code doesn't execute. It needs a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI

    I do,

    	public static void main(String arg[]){
    		GUI gui= new GUI();
    		WelcomeWindow guiW = new WelcomeWindow();
    		guiW.setVisible(true);		
    	}
    GUI gui= new GUI(); My gamewindow that holds a board.

    WelcomeWindow guiW = new WelcomeWindow(); The window i can't get to work properly, its a separate class that will launch before the gamewindow lunches (will implement how its shown later on, im only trying to get it work for now so i can start implementing the game code).

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Java GUI

    The code has no comments so I'm having a hard time trying to figure out what it is trying to do. The methods do this and do that without order or sense.

    For example how is it doing the layout of the components?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI

    I cant understand half of the stuff im doing either so its understandable that you don't get it, hehe.
    kljn.jpg
    However i got a little bit further. Ill probably come back with more questions after i've slept, thanks for the help.

Similar Threads

  1. GUI Java
    By nick777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 28th, 2013, 04:31 PM
  2. [SOLVED] GUI Java Help Please!
    By Techstudent in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 16th, 2013, 12:34 PM
  3. Replies: 2
    Last Post: January 18th, 2013, 10:12 AM