Results 1 to 4 of 4
  1. #1
    jackwong is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default jbuttons and jcombobox didn't show up please help...

    Hey guys, there are two separated questions in this thread
    1) The jbuttons or jcombobox didn't show up in my GUI even though I added both of them in my frame. There's no error for this code, just that it doesn't show up in GUI.
    2) Do you guys have any open-source for AI-chess? I need it for my current project I tried all the best I could to find an open-source that contains easy,medium,hard but couldn't find any that works with java...
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    import java.util.Calendar;
    
    public class chess extends JFrame{
    	int x = 0;
    	//texts or labels or panels
    	private JLabel label1,label2,label3,label4,difficulties,statusbar;
    	private JTextField timeF,timeT;
    	private JPanel panel,panel1,panel2,panel3,panel4;
    	//buttons
    	private JButton start,stop,settings, aminutes, bminutes, cminutes, dcustom;
    	//combo boxes
    	private JComboBox difficulty, color;
    	//j frame
    	private JFrame frame;
    	//lsits for combobox
    	private static String[] difflist = {"Easy", "Medium", "Hard"};
    	private static String[] colorlist = {"White", "Black"};
    	//for timer
    public chess (){
    	//house keeping stuffs
    	JFrame frame = new JFrame("Chess");
        frame.setSize(900,900);
        frame.setVisible(true);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
    	JPanel panel1 = new JPanel(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();
    	frame.add(panel1, BorderLayout.CENTER);
    
    
    	Font f = new Font("sansserif", Font.BOLD, 15);
    	
    	label1 = new JLabel("Difficulties : ");
    	label1.setFont(f);
    	label2 = new JLabel("Colour : ");
    	label2.setFont(f);
    	label3 = new JLabel("Black User's Timer : ");
    	label3.setFont(f);
    	label4 = new JLabel("White User's Timer : "); 
    	label4.setFont(f);
    	
    	c.insets = new Insets(100,100,800,800);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label1,c);
    	
    	c.insets = new Insets(100,72,700,800);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label2, c);
    	
    	c.anchor = GridBagConstraints.PAGE_END;
    	c.insets = new Insets(0,0,400,0);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label3, c);
    	
    	c.anchor = GridBagConstraints.PAGE_END;
    	c.insets = new Insets(0,0,200,0);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label4, c);
    	
    	JComboBox diff = new JComboBox(difflist);
    	JComboBox color = new JComboBox(colorlist);	
    	c.insets = new Insets(100,100,800,600);
    	c.gridx = 30;
    	c.gridy = 30;
    
    	panel1.add(diff, c);
    	frame.add(panel1);
    	
    	c.insets = new Insets(10,0,0,0);
    	c.gridx = 1;
    	c.gridy = 2;
    	panel1.add(color, c);
    	
    	statusbar = new JLabel("Status Bar Current State: Default");
    	
    	JLabel statusbar = new JLabel();
    	statusbar.setFont(new Font("Arial",Font.BOLD,25));
    	
    	JButton start = new JButton("Start");
    	JButton stop = new JButton("Stop");
    	JButton settings = new JButton("Settings");
    	
    	c.insets = new Insets(100,100,400,800);
    	c.gridx = 10;
    	c.gridy = 10;
    	panel1.add(start, c);
    	
    	c.insets = new Insets(10,0,0,0);
    	c.gridx = 1;
    	c.gridy = 2;
    	panel1.add(color, c);
    	
    
    }
    
    
    
    }
    Thanks alot :).

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: jbuttons and jcombobox didn't show up please help...

    Read the API for Container: Container (Java Platform SE 6)
    Unless you have a specific reason, add the components to a container prior to setting it to visible. In your case, there is no reason to immediately set the JFrame to visible until all components have been added.

  3. #3
    jackwong is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: jbuttons and jcombobox didn't show up please help...

    sorry.. i didn't really understand what you meant but yeah i only set the visible at the end after I have added all my components still the combobox/button doesn't show up...
    can
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    import java.util.Calendar;
    
    public class chess extends JFrame{
    	int x = 0;
    	//texts or labels or panels
    	private JLabel label1,label2,label3,label4,difficulties,statusbar;
    	private JTextField timeF,timeT;
    	private JPanel panel,panel1,panel2,panel3,panel4;
    	//buttons
    	private JButton start,stop,settings, aminutes, bminutes, cminutes, dcustom;
    	//combo boxes
    	private JComboBox difficulty, color;
    	//j frame
    	private JFrame frame;
    	//lsits for combobox
    	private static String[] difflist = {"Easy", "Medium", "Hard"};
    	private static String[] colorlist = {"White", "Black"};
    	//for timer
    public chess (){
    	//house keeping stuffs
    	JFrame frame = new JFrame("Chess");
        frame.setSize(900,900);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
    	JPanel panel1 = new JPanel(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();
    	frame.add(panel1, BorderLayout.CENTER);
    
    	statusbar = new JLabel("Status Bar Current State: Default");
    	c.insets = new Insets(100,72,700,800);
    	c.gridx = 0;
    	c.gridy = 0;
    	
    	Font f = new Font("sansserif", Font.BOLD, 15);
    	label1 = new JLabel("Difficulties : ");
    	label1.setFont(f);
    	label2 = new JLabel("Colour : ");
    	label2.setFont(f);
    	label3 = new JLabel("Black User's Timer : ");
    	label3.setFont(f);
    	label4 = new JLabel("White User's Timer : "); 
    	label4.setFont(f);
    	
    	c.anchor = GridBagConstraints.LINE_START;
    	c.insets = new Insets(100,100,800,800);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label1,c);
    	
    	c.anchor = GridBagConstraints.LINE_START;
    	c.insets = new Insets(100,72,700,800);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label2, c);
    	
    	c.anchor = GridBagConstraints.PAGE_END;
    	c.insets = new Insets(0,0,400,0);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label3, c);
    	
    	c.anchor = GridBagConstraints.PAGE_END;
    	c.insets = new Insets(0,0,200,0);
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label4, c);
    	
    	JComboBox diff = new JComboBox(difflist);
    	JComboBox color = new JComboBox(colorlist);	
    	c.anchor = GridBagConstraints.LINE_START;
    	c.insets = new Insets(100,100,800,600);
    	c.gridx = 30;
    	c.gridy = 30;
    
    	panel1.add(diff, c);
    	frame.add(panel1);
    	c.anchor = GridBagConstraints.LINE_START;
    	c.insets = new Insets(10,0,0,0);
    	c.gridx = 1;
    	c.gridy = 2;
    	panel1.add(color, c);
    	
    	statusbar = new JLabel("Status Bar Current State: Default");
    	
    	JLabel statusbar = new JLabel();
    	statusbar.setFont(new Font("Arial",Font.BOLD,25));
    	
    	JButton start = new JButton("Start");
    	JButton stop = new JButton("Stop");
    	JButton settings = new JButton("Settings");
    	
    	c.insets = new Insets(100,100,400,800);
    	c.gridx = 10;
    	c.gridy = 10;
    	panel1.add(start, c);
    	
    	c.insets = new Insets(10,0,0,0);
    	c.gridx = 1;
    	c.gridy = 2;
    	panel1.add(color, c);
    	
    	//timer
    	timeF = new JTextField(10);
    	timeF.setEditable(false);
    	timeF.setFont(new Font("Arial",Font.PLAIN,48));
    	
    	Timer t = new Timer(1000, new Listener());
    	t.start();	 
    	
    	c.gridx = 0;
    	c.gridy = 0;
    	panel2.add(label3, c);
        frame.setResizable(true);
    }
    
    	class Listener implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			Font font = new Font("SansSerif",Font.BOLD,20);
    			timeF.setText(x+"");
    			timeF.setBackground(Color.BLACK);
    			timeF.setForeground(Color.GREEN);
    			timeF.setFont(font);
    			x++;
    	
    		}
    	}
    	
    }
    /*
    frame.setVisible(true);
    frame.setResizable(true);
    frame.setLocationRelativeTo(null);*/

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: jbuttons and jcombobox didn't show up please help...

    No you don't.
    You set the frame visible in the third line of the 'chess' constructor...then proceed to add stuff to it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. The code didn't delete the text file
    By amyng in forum New To Java
    Replies: 3
    Last Post: 05-01-2012, 06:37 AM
  2. Replies: 0
    Last Post: 02-27-2012, 10:29 PM
  3. NetBeans Didn't show the Design mode
    By ryor310575 in forum NetBeans
    Replies: 2
    Last Post: 05-31-2011, 06:48 AM
  4. my JButtons won't show up
    By gib65 in forum AWT / Swing
    Replies: 4
    Last Post: 09-25-2010, 04:09 AM
  5. My own ClassLoader didn't work.
    By snooze-g in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2007, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •