I want to align all the JLabels to the left side of the panel. Following is my code, but it doesnt work properly, I dont know why.
JFrame frame1 = new JFrame("Register a passenger");
frame1.setVisible(true);
frame1.setSize(550, 200);
JPanel panel = new JPanel();
frame1.add(panel);
JLabel label1 = new JLabel("Name",SwingConstants.LEFT);
JLabel label2 = new JLabel("Activities",SwingConstants.LEFT);
JButton jbtReg = new JButton("Register");
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(jbtReg);
Based on your example, you could use
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
But this will align all your components to the left.
You could also consider using a different layout manager or combination of layout managers?
Take a look at A Visual Guide to Layout Managers for more ideas
Updated
FlowLayout (which is the default layout manager for JPanel) doesn't give you a lot of options, instead consider trying to use a different layout manager or combination of layout managers, for example...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LayoutExample {
public static void main(String[] args) {
new LayoutExample();
}
public LayoutExample() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(new JLabel("Name:"), gbc);
gbc.gridy++;
add(new JLabel("Activity:"), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(20), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 2;
add(new JButton("Register"), gbc);
}
}
}
Related
I am trying to move a table to the left side of a JPanel which is also inside a Window
What I get is this:
What I want is this:
I tried using .setBounds method but it does not work. Does anyone know what to do?
This is my code:
package Modulos;
import paneles.*;
import javax.swing.*;
public class ModuloAdmin extends JFrame {
public ModuloAdmin(){
//Crear tabla
String[][] datosprueba = {
{"001","Carlitos", "Casa","51202011"},
{"001","Carlitos", "Casa","51202011"}
};
String[] TituloColumna = {"Código", "Nombre", "Dirección", "Teléfono"};
JTable TablaSucursales = new JTable(datosprueba,TituloColumna);
//Contenido de las pestañas
JPanel PanelSucursales = new JPanel();
PanelSucursales.add(TablaSucursales);
PanelSucursales.add(new JScrollPane(TablaSucursales));
JPanel PanelProductos = new JPanel();
PanelProductos.add(new JLabel("Panel productos"));
JPanel PanelClientes = new JPanel();
PanelClientes.add(new JLabel("Panel Clientes"));
JPanel PanelVendedores = new JPanel();
PanelVendedores.add(new JLabel("Panel Vendedores"));
//CREACION DE PESTAÑAS
JTabbedPane Pestanias = new JTabbedPane();
Pestanias.setBounds(20,80,700,700);
Pestanias.add("Sucursales",PanelSucursales);
Pestanias.add("Productos",PanelProductos);
Pestanias.add("Clientes", PanelClientes);
Pestanias.add("Vendedores",PanelVendedores);
//vENTANA MODULO DE ADMIN
JFrame VentanaModuloAdmin = new JFrame();
VentanaModuloAdmin.add(Pestanias);
VentanaModuloAdmin.setLayout(null);
VentanaModuloAdmin.setSize(800,850);
VentanaModuloAdmin.setLocationRelativeTo(null);
VentanaModuloAdmin.setTitle("Módulo de administrador");
VentanaModuloAdmin.setVisible(true);
VentanaModuloAdmin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Use appropriate layout mangers and container management
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable table;
public TestPane() {
setBorder(new EmptyBorder(16, 16, 16, 16));
setLayout(new GridLayout(0, 2));
add(new JScrollPane(table));
add(createButtonPane());
}
protected JPanel createButtonPane() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 16;
gbc.ipady = 16;
gbc.fill = gbc.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Clear"), gbc);
gbc.gridx = 1;
panel.add(new JButton("Carga Masiva"), gbc);
gbc.gridy++;
gbc.gridx = 0;
panel.add(new JButton("Actualizar"), gbc);
gbc.gridx = 1;
panel.add(new JButton("Eliminar"), gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
panel.add(new JButton("Exportar Listado a PDF"), gbc);
return panel;
}
}
}
Seriously, layout managers solve one of the most difficult issues facing UI developers - the variable nature of the output (screen resolutions, font metrics, accessibility modifiers, a whole bunch of different stuff which changes how layouts need to be calculated).
They might seem difficult to start with, but once you get use to using them and learn how to make use of "compound layouts", which is demonstrated above, it will help you make better UIs more quickly
i want to make a bigger resolution/fullscreen but i dont know what layout should i use, can u guys help me to choose a better layout
this is the first code for first pic
tp = new JTabbedPane();
tp.setBounds(0,0,250,400);
panel = new JPanel();
panel.setPreferredSize(new Dimension(250, 480));
tp.addTab("Mahasiswa",panel);
panel.setLayout(new FlowLayout());
label_insert = new JLabel("NIM : ");
label_insert2 = new JLabel("ID Jurusan : ");
tf_insert1 = new JTextField(15);
tf_insert2 = new JTextField(15);
Insert = new JButton(" Insert ");
//panel add
this one i want to make it on middle of frame and all of them already on panel, how can i move it to the middle ?
this is code for the second picture
public void mainform(){
main = new JPanel();
main.setLayout(new FlowLayout(FlowLayout.CENTER));
label_main = new JLabel("ID");
label_main2 = new JLabel("Password");
tf_main = new JTextField(15);
tf_main2 = new JPasswordField(15);
login = new JButton("LOGIN");
login.addActionListener(this);
main.add(label_main);
main.add(tf_main);
main.add(label_main2);
Following this guide here I would suggest the following:
yourFrame = new JFrame();
yourFrame.setLayout(new BorderLayout());
tp = new JTabbedPane();
yourFrame.add(tp, BorderLayout.CENTER);
Then fiddle with GridLayout, GridBagLayout, or GroupLayout for the tabs in your tabbedpane.
I would consider using a GridBagLayout which will give you greater flexibility in how the components are laid out
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JLabel("ID"), gbc);
gbc.gridx++;
add(new JTextField(20), gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JLabel("Password"), gbc);
gbc.gridx++;
add(new JTextField(20), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
add(new JButton("Login"), gbc);
}
}
}
See Laying Out Components Within a Container and How to Use GridBagLayout for more details
I'm coding a GUI by hand and I've run into difficulty positioning a JLabel on a JPanel. I'm trying to put it in the top left hand side above the JTextField but it's defaulting to the middle even though I'm settings the bounds:
Relevant code:
JPanel mainPanel = new JPanel();
JLabel myFleetLabel = new JLabel("My Fleet");
myFleetLabel.setBounds(1,1, 10, 10);
mainPanel.add(myFleetLabel);
add(mainPanel);
Here's what it looks like:
There are a few ways you might be able achieve this, one might be to use a GridBagLayout as the primary layout manager, for example
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHWEST;
JLabel label = new JLabel("My Fleet: ");
add(label, gbc);
JTextArea ta = new JTextArea(10, 20);
gbc.gridx++;
add(new JScrollPane(ta), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JScrollPane(new JTextArea(5, 10)), gbc);
JPanel actions = new JPanel();
actions.add(new JButton("Create Ship"));
actions.add(new JButton("Flip Coins"));
gbc.gridy++;
add(actions, gbc);
}
}
}
See Laying Out Components Within a Container and How to Use GridBagLayout for more details.
Remember, it's unlikely that a single layout manager will solve all your problems and some times you will need to use two or more to accomplish the overall effect
Typically you need to use layouts to place objects in a containter. You should get acquainted with layouts to really code properly in swing.
The being said, the reason your code isn't working as is, is because containers have a layout by default. You CAN remove the layout as follows
mainPanel.setLayout(null);
but this is very bad practice and should be avoided always.
I'm tired with Java GridBagLayout. I want to create a panel that looks like the one shown in the picture below, but I couldn't get it. I'm unable to position the left panel, and I failed to make panels long. The width does not increase when I set gridWidth. I can't use any GUI builders for that. I want to get a layout like the picture below.
This is the unsuccessful code:
public class gui3 extends Frame{
Panel p1,p2,p3,p4,p5,p6,p7,pmain;
gui3(){
setVisible(true);
setSize(500,500);
setTitle(" Calculator ");
GridBagLayout gb1=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
setLayout(gb1);
p1=new Panel();
p1.setBackground(Color.BLACK);
gbc.gridx=5;
gbc.gridy=0;
gbc.gridwidth=3;
//gbc.weightx =0.5;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p1,gbc);
p2=new Panel();
p2.setBackground(Color.BLUE);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p2,gbc);
p3=new Panel();
p3.setBackground(Color.GREEN);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p3,gbc);
p4=new Panel();
p4.setBackground(Color.cyan);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p4,gbc);
p5=new Panel();
p5.setBackground(Color.RED);
gbc.gridx=0;
gbc.gridy=4;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p5,gbc);
p6=new Panel();
p6.setBackground(Color.pink);
gbc.gridx=0;
gbc.gridy=5;
gbc.gridwidth=2;
// gbc.weightx = 1;
// gbc.fill=GridBagConstraints.HORIZONTAL;
add(p6,gbc);
p7=new Panel();
p7.setBackground(Color.yellow);
gbc.gridx=6;
gbc.gridy=0;
gbc.gridheight=6;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p7,gbc);
}
}
You mean something like...
So basically, you can use GridBagConstraints#gridheight (or gridwidth) to set the number of grid cells that a component will span across
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLayout {
public static void main(String[] args) {
new TestLayout();
}
public TestLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.weightx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(createPane(Color.RED), gbc);
gbc.gridy++;
frame.add(createPane(Color.GREEN), gbc);
gbc.gridy++;
frame.add(createPane(Color.BLUE), gbc);
gbc.gridy++;
frame.add(createPane(Color.CYAN), gbc);
gbc.gridy++;
frame.add(createPane(Color.MAGENTA), gbc);
gbc.gridy++;
frame.add(createPane(Color.ORANGE), gbc);
gbc.gridy++;
frame.add(createPane(Color.PINK), gbc);
gbc.gridx++;
gbc.weightx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.VERTICAL;
frame.add(createPane(Color.YELLOW), gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public JPanel createPane(Color color) {
JPanel pane = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
pane.setBackground(color);
return pane;
}
}
Have a look at How to Use GridBagLayout for more details
The abrupt changes in size from the MadProgrammer's solution can be
easily fixed in two ways:
public JPanel createPane(Color color) {
JPanel pane = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(50, 50);
}
};
pane.setBackground(color);
return pane;
}
We also provide a mimimum size which is equal to the preferred size.
This way the panels are not shrinking but they are cut from the window
when the container is too small to display them.
The optimal solution is probably to set the weighty to 1. The panels will
gradually being shrinking.
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 0;
Finally, I also created a solution with MigLayout. If possible, try
to create your layouts with MigLayout rather than with GridBagLayout.
package com.zetcode;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutPanels extends JFrame {
public MigLayoutPanels() {
initUI();
setTitle("Panels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("wrap"));
add(createPanel(), "w 200, push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "cell 1 0 1 6, growy");
pack();
}
public JPanel createPanel() {
JPanel pnl = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
pnl.setBorder(BorderFactory.createEtchedBorder());
return pnl;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutPanels ex = new MigLayoutPanels();
ex.setVisible(true);
}
});
}
}
GridBagLayout is a powerful manager which can be used to create most layouts.
Many programmers find it difficult to use. MigLayout is easier to understand,
more powerful, and much less verbose.
This is what i want to achieve
I used grid layout and this is what have
and my code goes (not full code can provide one if needed).
components.setLayout(new GridLayout(4,0));
components.setBorder(BorderFactory.createTitledBorder("Personal Data"));
//Lable to display
name.setText("Resident Name");
roomNo.setText("Room Number");
age.setText("Age");
gender.setText("Gender");
careLvl.setText("Care Level");
components.add(name);
components.add(textFieldForName);
components.add(roomNo);
components.add(textFieldForAge);
components.add(age);
components.add(coForAge);
components.add(gender);
components.add(coForGender);
components.add(careLvl);
components.add(coForCareLvl);
any heads up would be greatly appreciated.
GridLayout does just that, it layouts components out in a grid, where each cell is percentage of the available space based on the requirements (ie width / columns and height / rows).
Take a look at A Visual Guide to Layout Managers for examples of the basic layout managers and what they do.
I would recommend that you take a look at GridBagLayout instead. It is the most flexible (and most complex) layout manager available in the default libraries.
For Example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLayout31 {
public static void main(String[] args) {
new TestLayout31();
}
public TestLayout31() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JLabel lblRes = new JLabel("Resident Name");
JLabel lblRoomNo = new JLabel("RoomNo");
JLabel lblAge = new JLabel("Age");
JLabel lblGender = new JLabel("Gender");
JLabel lblCare = new JLabel("Care level");
JTextField fldRes = new JTextField("john smith", 20);
JTextField fldRoomNo = new JTextField(10);
JComboBox cmbAge = new JComboBox(new Object[]{51});
JComboBox cmbGener = new JComboBox(new Object[]{"M", "F"});
JComboBox cmbCare = new JComboBox(new Object[]{"Low"});
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(1, 1, 1, 1);
add(lblRes, gbc);
gbc.gridx++;
gbc.gridwidth = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(fldRes, gbc);
gbc.gridx = 7;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
add(lblRoomNo, gbc);
gbc.gridx++;
add(fldRoomNo, gbc);
gbc.gridy++;
gbc.gridx = 1;
add(lblAge, gbc);
gbc.gridx++;
add(cmbAge, gbc);
gbc.gridx++;
add(lblGender, gbc);
gbc.gridx++;
add(cmbGener, gbc);
gbc.gridx++;
gbc.gridwidth = 2;
add(lblCare, gbc);
gbc.gridx += 2;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(cmbCare, gbc);
}
}
}
Compound Layout Example
Another option would be to use a compound layout. This is, you separate each section of your UI into separate containers, concentrating on their individual layout requirements.
For example, you have two rows of fields, each which don't really relate to each other, so rather than trying to figure out how to make the fields line up, you can focus on each row separately...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLayout31 {
public static void main(String[] args) {
new TestLayout31();
}
public TestLayout31() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JPanel topPane = new JPanel(new GridBagLayout());
JLabel lblRes = new JLabel("Resident Name");
JLabel lblRoomNo = new JLabel("RoomNo");
JLabel lblAge = new JLabel("Age");
JLabel lblGender = new JLabel("Gender");
JLabel lblCare = new JLabel("Care level");
JTextField fldRes = new JTextField("john smith", 20);
JTextField fldRoomNo = new JTextField(10);
JComboBox cmbAge = new JComboBox(new Object[]{51});
JComboBox cmbGener = new JComboBox(new Object[]{"M", "F"});
JComboBox cmbCare = new JComboBox(new Object[]{"Low"});
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(1, 1, 1, 1);
topPane.add(lblRes, gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPane.add(fldRes, gbc);
gbc.gridx++;
topPane.add(lblRoomNo, gbc);
gbc.gridx++;
topPane.add(fldRoomNo, gbc);
JPanel bottomPane = new JPanel(new GridBagLayout());
gbc.gridx = 0;
bottomPane.add(lblAge, gbc);
gbc.gridx++;
bottomPane.add(cmbAge, gbc);
gbc.gridx++;
bottomPane.add(lblGender, gbc);
gbc.gridx++;
bottomPane.add(cmbGener, gbc);
gbc.gridx++;
bottomPane.add(lblCare, gbc);
gbc.gridx++;
bottomPane.add(cmbCare, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(topPane, gbc);
gbc.gridy++;
add(bottomPane, gbc);
}
}
}
This will make it easier to modify the UI later should you have to...
JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
JPanel northPane = new JPanel();
JPanel centerPane = new JPanel();
JPanel southPane = new JPanel();
contentPane.setLayout(new BorderLayout());
northPane.setLayout( new GridLayout(1, 6));
southPane.setLayout( new GridLayout(1, 7));
contentPane.add(northPane, BorderLayout.NORTH );
contentPane.add(centerPane, BorderLayout.CENTER);
contentPane.add(southPane, BorderLayout.SOUTH );
frame.setContentPane(contentPane);
JLabel residentNameLabel = new JLabel("Resident name ");
JTextField residentNameText = new JTextField();
JLabel roomNoLabel = new JLabel("RoomNo ");
JTextField roomNoText = new JTextField();
JLabel emptyLabel0 = new JLabel(" ");
JLabel emptyLabel1 = new JLabel(" ");
JLabel emptyLabel2 = new JLabel(" ");
JLabel emptyLabel3 = new JLabel(" ");
JLabel ageLabel = new JLabel("Age ");
JComboBox<String> ageComboBox = new JComboBox<String>();
ageComboBox.addItem("50");
ageComboBox.addItem("51");
ageComboBox.addItem("52");
ageComboBox.addItem("53");
ageComboBox.addItem("54");
ageComboBox.addItem("55");
JLabel genderLabel = new JLabel("Gender ");
JComboBox<String> genderComboBox = new JComboBox<String>();
genderComboBox.addItem("M");
genderComboBox.addItem("F");
JLabel careLevelLabel = new JLabel("Care Level ");
JComboBox<String> careLevelComboBox = new JComboBox<String>();
genderComboBox.addItem("low");;
genderComboBox.addItem("medium");
genderComboBox.addItem("high");
residentNameLabel.setHorizontalAlignment(JLabel.RIGHT);
roomNoLabel.setHorizontalAlignment(JLabel.RIGHT);
ageLabel.setHorizontalAlignment(JLabel.RIGHT);
genderLabel.setHorizontalAlignment(JLabel.RIGHT);
careLevelLabel.setHorizontalAlignment(JLabel.RIGHT);
northPane.add(emptyLabel0 );
northPane.add(residentNameLabel);
northPane.add(residentNameText );
northPane.add(roomNoLabel );
northPane.add(roomNoText );
northPane.add(emptyLabel1 );
centerPane.add(emptyLabel2 );
southPane.add(ageLabel );
southPane.add(ageComboBox );
southPane.add(genderLabel );
southPane.add(genderComboBox );
southPane.add(careLevelLabel );
southPane.add(careLevelComboBox);
southPane.add(emptyLabel3 );
contentPane.setBorder(BorderFactory.createTitledBorder("Personal Data"));
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
frame.setVisible(true);
frame.pack();