How do i position my components in GUI?
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);
}
}
}
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..
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.
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