Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: How do i position my components in GUI?

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

    Default How do i position my components in GUI?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    
    import java.util.Calendar;
    public class buttons extends JFrame{
    	//texts or labels or panels
    	private JLabel level;
    	private JLabel difficulties;
    	private JLabel colour;
    	private JTextField timeF;
    	private JPanel panel;
    	//buttons
    	private JButton start;
    	private JButton stop;
    	private JButton settings;
    	//combo boxes
    	private JComboBox difficulty;
    	private JComboBox color;
    	//j frame
    	private JFrame frame;
    	//lsits for combobox
    	private static String[] difflist = {"Easy", "Medium", "Hard"};
    	private static String[] colorlist = {"White", "Black"};
    	//for timer
    	private static final long serialVersionUID = 1L; 
    	
    	public buttons (){
    		super("Chess");
    		panel = new JPanel();
    		panel.setBackground(Color.BLACK);
    		//combo
    		JComboBox color = new JComboBox(difflist);
    		JComboBox diff = new JComboBox(colorlist);
    		/////////
    		add(color);
    		add(diff);
    		//buttons
    		JButton start = new JButton("Start");
    		JButton stop = new JButton("Stop");
    		JButton settings = new JButton("Settings");
    		/////////
    		/////////
    		add(start);
    		add(stop);
    		add(settings);
    		///////////////////////////////// TIMER ///////////////////////////////////		
    		timeF = new JTextField(10);
    		timeF.setEditable(false);
    		timeF.setFont(new Font("Arial",Font.PLAIN,48));	
    		panel.add(timeF);
    		add(panel);
    		
    		
    		Timer t = new Timer(1000, new Listener());
    		t.start();
    		
    	}
    	class Listener implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			Font font = new Font("SansSerif",Font.BOLD,20);
    			Calendar rightNow = Calendar.getInstance();
    			int hour = rightNow.get(Calendar.HOUR_OF_DAY);
    			int min = rightNow.get(Calendar.MINUTE);
    			int sec = rightNow.get(Calendar.SECOND);
    			timeF.setText(hour+":"+min+":"+sec);
    			timeF.setBackground(Color.BLACK);
    			timeF.setForeground(Color.GREEN);
    			timeF.setFont(font);
    			
    		}
    }
    }
    Java Code:
    import java.awt.FlowLayout;
    import java.awt.Color;
    import javax.swing.JList;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.event.ListSelectionEvent;
    
    class Main extends JFrame {
      
       public static void main(String args[ ]) {
          Interface buttons = new Interface();
          buttons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          buttons.setSize(900, 400);
          buttons.setVisible(true);
    	  buttons.setResizable(true);
    	  buttons.setLocationRelativeTo(null);
       
       }
    }
    Hey guys.. I've been searching for hours already and still can't find the answer.. I wanted to position my components all of them just stack up to one another if i use this code..

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: How do i position my components in GUI?

    You will have to go through this tutorial on layout managers.
    Until you do you will have set yourself up for a ton of frustration.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do i position my components in GUI?

    This is not an advanced question; it is about Swing; I'm moving this thread.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Position of image
    By The original stinger in forum AWT / Swing
    Replies: 5
    Last Post: 05-10-2012, 09:57 PM
  2. How to position text?
    By Ike in forum New To Java
    Replies: 2
    Last Post: 10-08-2011, 06:05 PM
  3. Repositioning components based on mouse position
    By robbie.26 in forum AWT / Swing
    Replies: 3
    Last Post: 06-21-2011, 06:55 AM
  4. Position of PdfPTable
    By Takashy in forum New To Java
    Replies: 1
    Last Post: 02-09-2011, 04:45 PM
  5. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 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
  •