Aranging objects on a Gui
Basically im trying to get stuff aligned on a JPanel. I want everything to be on its own devidual line
problem is idk. I was messing around with setBounds but that did seem to work so how would i go about doing this?
heres my class:
Code :
package main;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Gui implements ActionListener {
private JFrame f = new JFrame("Simple Games");
private JPanel jp = new JPanel();
private JLabel title = new JLabel("Guess a Number between 1 and 10!");
private JTextField type = new JTextField("Type your number here!");
private JButton submit = new JButton("Submit");
private JMenuBar mb = new JMenuBar();
private JMenu muFile = new JMenu("File");
public Gui() {
f.setJMenuBar(mb);
f.add(jp);
jp.add(title);
jp.add(type);
jp.add(submit);
mb.add(muFile);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
public void launchFrame() {
Container pane = f.getContentPane();
pane.setLayout(new GridLayout (0,1));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.setSize(420, 200);
}
/**
* @param args
*/
public static void main(String[] args) {
Gui g = new Gui();
g.launchFrame();
}
}
Re: Aranging objects on a Gui
Use a layout manager. See
A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
And also note that layouts can be nested, for example one panel can have a certain layout while the panels that it contains another.
Re: Aranging objects on a Gui