This is a MigLayout-newbie question. I'd like to know how to draw a separator from the end of a label across the width of a panel.
Here's my example code:
package com.ndh.swingjunk;
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
class MyJFrame extends JFrame {
public MyJFrame() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("foo");
label.setFont(new Font("Tahoma", Font.BOLD, 11));
JSeparator separator = new JSeparator();
JLabel anotherLabel = new JLabel("some other label");
anotherLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
JButton button1 = new JButton("button 1");
JButton button2 = new JButton("button 2");
JPanel panel = new JPanel(new MigLayout());
panel.add(label);
panel.add(separator, "growx, wrap");
panel.add(anotherLabel);
panel.add(button1);
panel.add(button2);
getContentPane().add(panel);
pack();
}
}
public class SeparatorLayoutQuestion {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override public void run() {new MyJFrame().setVisible(true);}});}}
and here's the result:
which is terribly lame. I'd like the separator to stretch from the end of "Foo" to the end of the panel, all the way across button 2.
Here's what worked for me, I had to use "split" so the label and separator could share the same cell:
JPanel panel = new JPanel(new MigLayout());
panel.add(label, "split 2, span");
panel.add(separator, "growx, wrap");
panel.add(anotherLabel);
panel.add(button1);
panel.add(button2);
try adding
"span 3"
to the first label
Related
I'm working on simple Java GUI Frame creation with buttons, it should show the frame with two buttons "OK" and "Clear" respectively. And two lines for writing the First Name and Last Name. I'm using VS Code editor. Following code gives compilation error, any help would be appreciated:
import java.awt.*;
import javax.swing.*;
import javafx.event.ActionEvent;
public class MainFrame extends JFrame{
final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
JTextField tfFirstName, tfLastName;
JLabel lbWelcome;
public void initialize(){
/********** Form Panel ***************/
JLabel lbFirstName = new JLabel("First Name");
lbFirstName.setFont(mainFont);
tfFirstName = new JTextField();
tfFirstName.setFont(mainFont);
JLabel lbLastName = new JLabel("Last Name");
lbLastName.setFont(mainFont);
tfLastName = new JTextField();
tfLastName.setFont(mainFont);
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 1, 5, 5));
formPanel.add(lbFirstName);
formPanel.add(tfFirstName);
formPanel.add(lbLastName);
formPanel.add(tfLastName);
/********** Welcome Label ***************/
lbWelcome = new JLabel();
lbWelcome.setFont(mainFont);
/********** Button Panel ***************/
JButton btnOK = new JButton("OK");
btnOK.setFont(mainFont);
btnOK.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
String firstName = tfFirstName.getText();
String lastName = tfLastName.getText();
lbWelcome.setText("Hello " + firstName + " " + lastName);
}
});
JButton btnClear = new JButton("Clear");
btnClear.setFont(mainFont);
btnClear.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
tfFirstName.setText("");
tfLastName.setText("");
lbWelcome.setText("");
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
add(mainPanel);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
setTitle("Welcome");
setSize(500, 600);
setMinimumSize(new Dimension(300, 400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MainFrame myFrame = new MainFrame();
myFrame.initialize();
}
}
Compilation error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at JavaProjectTest.src.MainFrame.main(MainFrame.java:83)
Output result after fix:
There are two main problems:
(1) import javafx.event.ActionEvent; should be changed to import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
(2) The order of JPanel mainPanel = new JPanel(); should be before using mainPanel.setBackground(new Color(128, 128, 255));.
import javax.swing.*;
import java.awt.*;
//REMOVE THIS
//import javafx.event.ActionEvent;
//ADD THIS
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame {
final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
JTexThere are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import jThere are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.ava.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.tField tfFirstName, tfLastName;
JLabel lbWelcome;
public void initialize(){
/********** Form Panel ***************/
JLabel lbFirstName = new JLabel("First Name");
lbFirstName.setFont(mainFont);
There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.tfFirstName = new JTextField();
tfFirstName.setFont(mainFont);
JLabel lbLastName = new JLabel("Last Name");
lbLastName.setFont(mainFont);
tfLastName = new JTextField();
tfLastName.setFont(mainFont);
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 1, 5, 5));
formPanel.add(lbFirstName);
formPanel.add(tfFirstName);
formPanel.add(lbLastName);
formPanel.add(tfLastName);
/********** Welcome Label ***************/
lbWelcome = new JLabel();
lbWelcome.setFont(mainFont);
/********** Button Panel ***************/
JButton btnOK = new JButton("OK");
btnOK.setFont(mainFont);
btnOK.addActionListener(new ActionListener(){
#Overrideimport javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame {
final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
JTextField tfFirstName, tfLastName;
JLabel lbWelcome;
public void initialize(){
/********** Form Panel ***************/
JLabel lbFirstName = new JLabel("First Name");
lbFirstName.setFont(mainFont);
tfFirstName = new JTextField();
tfFirstName.setFont(mainFont);
JLabel lbLastName = new JLabel("Last Name");
lbLastName.setFont(mainFont);
tfLastName = new JTextField();
tfLastName.setFont(mainFont);
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 1, 5, 5));
formPanel.add(lbFirstName);
formPanel.add(tfFirstName);
formPanel.add(lbLastName);
formPanel.add(tfLastName);
/********** Welcome Label ***************/
lbWelcome = new JLabel();
lbWelcome.setFont(mainFont);
/********** Button Panel ***************/
JButton btnOK = new JButton("OK");
btnOK.setFont(mainFont);
btnOK.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
String firstName = tfFirstName.getText();
String lastName = tfLastName.getText();
lbWelcome.setText("Hello " + firstName + " " + lastName);
}
});
JButton btnClear = new JButton("Clear");
btnClear.setFont(mainFont);
btnClear.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
tfFirstName.setText("");
tfLastName.setText("");
lbWelcome.setText("");
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BorderLayout());
//MOVE SEQUENCE JPanel mainPanel = new JPanel();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
add(mainPanel);
setTitle("Welcome");
setSize(500, 600);
setMinimumSize(new Dimension(300, 400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MainFrame myFrame = new MainFrame();
myFrame.initialize();
}
}
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
String firstName = tfFirstName.getText();
String lastName = tfLastName.getText();
lbWelcome.setText("Hello " + firstName + " " + lastName);
}
});
JButton btnClear = new JButton("Clear");
btnClear.setFont(mainFont);
btnClear.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
tfFirstName.setText("");
tfLastName.setText("");
lbWelcome.setText("");
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BorderLayout());
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.setBackground(new Color(128, 128, 255));
mainPanel.add(formPanel, BorderLayout.NORTH);
mainPanel.add(lbWelcome, BorderLayout.CENTER);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
add(mainPanel);
setTitle("Welcome");
setSize(500, 600);
setMinimumSize(new Dimension(300, 400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MainFrame myFrame = new MainFrame();
myFrame.initialize();
}
}
I want to align Labels and a Panel (containing Buttons) to the left inside a vertical BoxLayout.
As long as I don't add the panel to the BoxLayout everything is aligned to the left perfectly, but adding it screws everything up.
import javax.swing.*;
import java.awt.*;
public class BoxLayoutDemo{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel five = new JPanel();
JButton plus = new JButton("+");
JButton minus = new JButton("-");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Font testFont = new Font("Arial", Font.BOLD, 20);
JLabel label1 = new JLabel("Label1");
label1.setFont(testFont);
JLabel label2 = new JLabel("Label2");
label2.setFont(testFont);
five.setLayout(new BoxLayout(five, BoxLayout.X_AXIS));
plus.setMinimumSize(new Dimension(30, 30));
plus.setMaximumSize(new Dimension(30, 30));
minus.setMinimumSize(new Dimension(30, 30));
minus.setMaximumSize(new Dimension(30, 30));
five.add(plus);
five.add(minus);
panel.add(label1);
panel.add(five);
panel.add(label2);
panel.setOpaque(true);
panel.setBackground(Color.red);
frame.setMinimumSize(new Dimension(200, 200));
frame.getContentPane().add(panel, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Your components need to have the same "x alignment":
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label2.setAlignmentX(Component.LEFT_ALIGNMENT);
five.setAlignmentX(Component.LEFT_ALIGNMENT);
Read the section from the Swing tutorial on Fixing Alignment Problems for more information.
You can use invisible components as fillers.
private static Box leftAlignedInHorizontalBox(Component component) {
Box box = Box.createHorizontalBox();
box.add(component);
box.add(Box.createHorizontalGlue());
return box;
}
and then:
panel.add(leftAlignedInHorizontalBox(label1));
I'm a newbie in Java programming and I got a question about BoxLayout. I can't get the JLabels centered in a panel with BoxLayout
What I want is to change from what I got now:
to this:
getting the labels being totally in the middle of the panel.
Here's my code:
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame{
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
public Test(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initWidgets();
setVisible(true);
}
private void initWidgets(){
setPreferredSize(new Dimension(300, 300));
label1 = new JLabel("Label 1");
label2 = new JLabel("Label 2");
label3 = new JLabel("Label 3");
label4 = new JLabel("Label 4");
label5 = new JLabel("Label 5");
JPanel jpanel = new JPanel();
label1.setAlignmentX(CENTER_ALIGNMENT);
label2.setAlignmentX(CENTER_ALIGNMENT);
label3.setAlignmentX(CENTER_ALIGNMENT);
label4.setAlignmentX(CENTER_ALIGNMENT);
label5.setAlignmentX(CENTER_ALIGNMENT);
jpanel.setLayout(new BoxLayout(jpanel, BoxLayout.PAGE_AXIS));
jpanel.add(label1);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label2);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label3);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label4);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label5);
add(jpanel);
pack();
}
public static void main(String[] args) {
new Test();
}
}
To vertically center the components you need to add "glue" at the start and end:
jpanel.add(Box.createVerticalGlue());
jpanel.add(label1);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label2);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label3);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label4);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label5);
jpanel.add(Box.createVerticalGlue());
Read the section from the Swing tutorial on How to Use BoxLayout for more information.
try adding this after setAlignmentX
label1.setHorizontalAlignment(SwingConstants.CENTER);
label2.setHorizontalAlignment(SwingConstants.CENTER);
label3.setHorizontalAlignment(SwingConstants.CENTER);
label4.setHorizontalAlignment(SwingConstants.CENTER);
label5.setHorizontalAlignment(SwingConstants.CENTER);
then add the labels at the panel like this:
jpanel.add(label1, BorderLayout.CENTER);
jpanel.add(label2, BorderLayout.CENTER);
jpanel.add(label3, BorderLayout.CENTER);
jpanel.add(label4, BorderLayout.CENTER);
jpanel.add(label5, BorderLayout.CENTER);
I would like to go to another frame of Connect Four by clicking the "PLAY ME" button, but I am very confused on how to do so. Here, I have the code for the opening page of connect four and labels and buttons set up on the page.
Here is my code:
import java.awt.*;
import javax.swing.event.*;
import java.awt.Color.*;
import javax.swing.*;
import java.util.*;
public class Game implements ActionListener{
public Game(){
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(1000,1000));
frame.setTitle("Connect Four");
frame.setLayout(new BorderLayout());
JButton play = new JButton();
play.setPreferredSize(new Dimension(75,150));
play.setBackground(Color.RED);
play.setFont(new Font("Arial", Font.PLAIN, 40));
play.setForeground(Color.BLACK);
play.setText("CLICK ME TO PLAY");
frame.add(play, BorderLayout.SOUTH);
JPanel north = new JPanel(new BorderLayout());
JLabel title = new JLabel("Connect Four");
north.setBackground(Color.WHITE);
title.setFont(new Font("Arial", Font.PLAIN, 100));
title.setForeground(Color.RED);
title.setHorizontalAlignment(0);
title.setVerticalAlignment(1);
frame.add(north, BorderLayout.NORTH);
north.add(title);
JPanel intro = new JPanel(new GridLayout(10,1));
intro.setBackground(Color.WHITE);
JLabel instructions = new JLabel("Instructions");
instructions.setFont(new Font("Ariel", Font.PLAIN, 70));
JLabel instructionsPart1 = new JLabel("Both players will be assigned a color, either red or black.");
JLabel instructionsPart2 = new JLabel("Players will take turns placing the colored discs on to the board.");
JLabel instructionsPart3 = new JLabel("The OBJECTIVE is to get four of one colored discs in a row.");
instructionsPart1.setFont(new Font("Ariel", Font.PLAIN, 35));
instructionsPart2.setFont(new Font("Ariel", Font.PLAIN, 35));
instructionsPart3.setFont(new Font("Ariel", Font.PLAIN, 35));
intro.add(instructions);
intro.add(new JLabel(""));
intro.add(instructionsPart1);
intro.add(new JLabel(""));
intro.add(instructionsPart2);
intro.add(new JLabel(""));
intro.add(instructionsPart3);
intro.add(new JLabel(""));
frame.add(intro, BorderLayout.CENTER);
frame.add(intro);
}
}
Hide Your First Frame And Set Visiblity of second frame to true
btnplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
second_add second = new second_add();
setVisible(false); // Hide current frame
second.setVisible(true);
}
});
Rather than changing the frame, change the panel. Instead of using JFrame's add() method, use setContentPane(Container container)
Instead of adding buttons and what not to the frame, create another wrapper panel and use the BorderLayout on that, and then add that panel to the frame:
Example:
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(play, BorderLayout.SOUTH);
//etc.
frame.setContentPane(wrapper);
Then, to handle the button click, implement the ActionListener interface. Don't do it with the main class - you'll need more than one. Use an anonymous inner class, or a lambda expression. Example:
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setContentPane(someOtherJPanel);
}
});
i am trying to set the text box and label in panel but the text box is getting inappropriate size and the layout is also not setting properly
public class FrameLayout1 implements ActionListener {
JTextField[] txtName;
JLabel[] lblname;
JCheckBox keyButton;
JCheckBox cascadeButton;
JComboBox SetList;
JComboBox ClassList;
JLabel[] lblType;
JTextField txtJoinColumn;
JLabel[] lblAssoType;
JTextField[] txtJoinColumnAssoc;
FrameLayout1(){
}
public void setDetailsPanel()
{
txtName = new JTextField[10];
txtJoinColumnAssoc = new JTextField[10];
lblname=new JLabel[10];
lblType=new JLabel[10];
lblAssoType=new JLabel[20];
JFrame ColumnFrame=new JFrame("Enter the Column Values");
int i=0;
JPanel panel = new JPanel(new GridLayout(0,1,5,10));
for (i=0;i<5;i++)
{
JPanel panelTxtLbl = new JPanel(new GridLayout(0,2));
lblname[i] = new JLabel("label"+i+":", JLabel.LEFT);
panelTxtLbl.add(lblname[i]);
txtName[i] = new JTextField(15);
panelTxtLbl.add(txtName[i]);
panel.add(panelTxtLbl);
lblType[i] = new JLabel("labeldata", JLabel.LEFT);
panel.add(lblType[i]);
String[] SetStrings = { "One to Many","Many to Many" };
SetList = new JComboBox(SetStrings);
SetList.setSelectedIndex(1);
panel.add(SetList);
cascadeButton = new JCheckBox("cascade");
cascadeButton.setSelected(true);
panel.add(cascadeButton);
JPanel panelSetClass = new JPanel(new GridLayout(0,2));
for(int j=0;j<3;j++)
{
lblAssoType[j]=new JLabel("Label Inner"+j+":", JLabel.LEFT);
panelSetClass.add(lblAssoType[j]);
txtJoinColumnAssoc[j] = new JTextField(15);
panelSetClass.add(txtJoinColumnAssoc[j]);
}
panel.add(panelSetClass);
panel.add(createHorizontalSeparator());
}
//detailsPanel.add(panel, BorderLayout.CENTER);
//detailsPanel.setAutoscrolls(true);
panel.setAutoscrolls(true);
JButton button = new JButton("Submit");
button.addActionListener(this);
//detailsPanel.add(button,BorderLayout.PAGE_END);
panel.add(button,BorderLayout.PAGE_END);
Color c=new Color(205, 222, 216);
ColumnFrame.setLayout(new BorderLayout());
ColumnFrame.add(panel,BorderLayout.CENTER);
//ColumnFrame.setSize(700,600);
ColumnFrame.setBackground(c);
ColumnFrame.pack();
ColumnFrame.setVisible(true);
ColumnFrame.setLocation(200, 200);
}
/*
* to add a horizontal line in the panel
*/
static JComponent createHorizontalSeparator() {
JSeparator x = new JSeparator(SwingConstants.HORIZONTAL);
x.setPreferredSize(new Dimension(3,2));
return x;
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
createHorizontalSeparator function helps you add a horizontal line in the panel, as i was unable to segregate the fields .
current output
desired output
Today, I answered a similar question where GridLayout caused much the same
confusion. Do yourself a service and use a flexible layout manager
(if it is possible) like MigLayout to create your layouts. These
simple built-in layout managers either have very limited application
(GridLayout, FlowLayout, BorderLayout) or it can become challenging
to create sofisticated layouts with them (BoxLayout).
GridLayout and BorderLayout stretch their components and do not
honour the size bounds of their children. This is the reason why you
have those unnecessary spaces and why the button is expanded horizontally.
The following is an example that mimics your layout. It is created with
the powerful MigLayout manager.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class DetailsPanel extends JFrame {
public DetailsPanel() {
initUI();
setTitle("Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("ins 10, wrap 2", "[][grow]"));
add(new JLabel("Label 1:"));
add(new JTextField(), "w 200, growx");
add(new JLabel("Label 2:"));
add(new JTextField(), "growx");
add(new JLabel("Label 3:"));
add(new JTextField(), "gaptop 30, growx");
add(new JLabel("Label 4:"));
add(new JTextField(), "growx");
add(new JLabel("Label 5:"));
add(new JTextField(), "gaptop 30, growx");
add(new JLabel("Label 6:"));
add(new JTextField(), "growx");
add(new JButton("Submit"), "gaptop 30, skip, right");
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
DetailsPanel ex = new DetailsPanel();
ex.setVisible(true);
}
});
}
}
You need to dedicate some time to learn a more complex layout manager, but
it pays off. Especially, if you often build layouts in Swing.