I am in the beginning process of coding a matrix project for my coding class. I am currently just trying to get the GUI looking the way I want it. Here is a quick sketch of how I want it to look:
https://drive.google.com/file/d/1Hmt-9DNH6CGtV1eBJYSOu72Iu2cAqPe8/view?usp=sharing
(The pic is too big to upload so linking the Drive was the next best option.)
Basically, The user would enter in the rows and columns of their 2 matrices, and press the button to calculate. The program would ensure the 2 matrices were compatible, prompt for specific numbers, and then the final calculations would appear in the labels.
The issue is I'm still pretty inexperienced in Java, and this is my first time working with more than 1 JPanel. Here is the code I've written for my Gui so far:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class GuiWindow {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame myGUI = new JFrame();
myGUI.setTitle("Matrix Project");
myGUI.setSize(600,600);
myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border border = BorderFactory.createLineBorder(Color.BLACK, 1); //borders
JLabel AddLabel = new JLabel(); //creates JLabels
AddLabel.setBorder(border);
JLabel SubLabel = new JLabel();
SubLabel.setBorder(border);
JLabel MultLabel = new JLabel();
MultLabel.setBorder(border);
JPanel main = new JPanel(); //Adds JLabel panel
myGUI.add(main);
main.setLayout(new GridLayout(1, 3));
main.setAlignmentY(Component.TOP_ALIGNMENT);
main.add(MultLabel);
main.add(SubLabel);
main.add(AddLabel);
JTextField row1txt = new JTextField(); //textfields to input rows and columns
row1txt.setText("Matrix 1 rows...");
JTextField column1txt = new JTextField();
column1txt.setText("Matrix 1 columns...");
JTextField row2txt = new JTextField();
row2txt.setText("Matrix 2 rows...");
JTextField column2txt = new JTextField();
column2txt.setText("Matrix 2 columns...");
JPanel input = new JPanel(); //adds textfield panel
myGUI.add(input);
input.setLayout(new GridLayout (2, 2, 3, 3));
input.add(row1txt);
input.add(column1txt);
input.add(row2txt);
input.add(column2txt);
JButton Calc = new JButton(); //button to eventually calculate
Calc.setText("Calculate");
JPanel CalcButton = new JPanel(); //adds button panel
myGUI.add(CalcButton);
CalcButton.add(Calc);
myGUI.setVisible(true); //instantiates the gui
}
}
When I load the code, All I see is the button. This makes sense. I have nothing splitting up the 3 JPanels into different sections, so Java is just sort of laying them on top of each other. So here, of course, is the question: How would you split these Jpanels up into separate sections of the JFrame so it could look at least sort of like my sketch?
Any help is appreciated. Thanks!
The first thing you want to do is have a read through Laying Out Components Within a Container.
You could probably do this using compounding GridLayouts, but to my money, GridBagLayout offers more flexibility.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.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 {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.weighty = 0.5;
add(new TopPane(), gbc);
gbc.weightx = 0.5;
gbc.gridy++;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 1;
add(new CheckerPane(), gbc);
gbc.gridx++;
add(new JButton("Button"), gbc);
}
}
public class TopPane extends JPanel {
public TopPane() {
setLayout(new GridLayout(0, 3));
add(new JLabel("label1", JLabel.CENTER));
add(new JLabel("label2", JLabel.CENTER));
add(new JLabel("label3", JLabel.CENTER));
}
}
public class CheckerPane extends JPanel {
public CheckerPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(new JCheckBox("Tick box 1"), gbc);
gbc.gridx++;
add(new JCheckBox("Tick box 2"), gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JCheckBox("Tick box 3"), gbc);
gbc.gridx++;
add(new JCheckBox("Tick box 4"), gbc);
}
}
}
Related
So I have a main JPanel that I would want to use as a container for more panels. I would like the child panels to have the same width as its parent and I tried using GridBagLayout to achieve it:
private JPanel createPanels() {
JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JPanel p1 = new JPanel();
p1.add(new JLabel("Test"));
p1.add(new JButton("+"));
content.add(p1, gbc);
return content;
}
With this piece of code, I get the following result:
As you can see, the panel that just got created doesn't fit the width of its container (green panel). I thought gbc.anchor = GridBagConstraints.LINE_START; and gbc.fill = GridBagConstraints.HORIZONTAL; lines would be in charge of doing that, but apparently they did not.
How can I achieve this?
I made a few changes to your code and created the following GUI.
In your code, you created a JPanel with a FlowLayout and placed it inside a JPanel with a GridBagLayout. The FlowLayout JPanel is considered one Swing component, so it won't adjust to the container size.
I added a main method and a JFrame to your code to show the difference.
Here's the complete runnable code that I think you want.
import java.awt.BorderLayout;
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.SwingUtilities;
public class GridBagLayoutTest2 implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutTest2());
}
#Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createPanels(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createPanels() {
JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.0;
content.add(new JLabel("Test"), gbc);
gbc.gridx++;
gbc.weightx = 1.0;
content.add(new JButton("+"), gbc);
return content;
}
}
I want to make the copyright in the image below in the center of the frame.
I tried to add SwingConstants.CENTER and Component.CENTER_ALIGNMENT but neither worked.
Can someone please help me with this one? Thanks in advance.
enter image description here
This is my code:
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JTextField text1;
JTextField text2;
JLabel label1;
JLabel label2;
JButton encrypt;
JButton decrypt;
JLabel copyright;
//constructor
public MyFrame(){
super("Encryption and Decryption");
setLayout(new FlowLayout()); //add first text field
label1 = new JLabel("enter a text: ");
add(label1);
text1= new JTextField(10);
add(text1);
//add second text field
label2 = new JLabel("result: ");
add(label2);
text2= new JTextField(10);
add(text2);
encrypt = new JButton("encrypt!");
add(encrypt);
decrypt = new JButton("decrypt!");
add(decrypt);
copyright = new JLabel(" © Project realized by Ayoub Touti ");
add(copyright);
//organizing elements in a panel
JPanel panel = new JPanel(new GridLayout(4,2,12,6));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(encrypt);
panel.add(decrypt);
panel.add(copyright);
add(panel);
}
}
//////////////////////////////
import javax.swing.*;
public class main {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (500, 200);
frame.setResizable(true);
frame.setVisible (true);
frame.setLocationRelativeTo(null); // to open the window in the main screen
}
}
Use a GridBagLayout (or a combination of layouts through compound components, slightly demonstrated below)
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.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
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 {
public TestPane() {
setBorder(new EmptyBorder(32, 32, 32, 32));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = gbc.LINE_END;
gbc.insets = new Insets(0, 8, 0, 8);
add(new JLabel("enter a text:"), gbc);
gbc.gridy++;
add(new JLabel("result:"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = gbc.LINE_START;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
JPanel actionPane = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = gbc.HORIZONTAL;
gbc.insets = new Insets(0, 0, 0, 0);
actionPane.add(new JButton("encyprt!"), gbc);
gbc.gridx++;
actionPane.add(new JButton("decyprt!"), gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
gbc.gridy = 2;
add(actionPane, gbc);
gbc.gridy++;
add(new JLabel("© Project realized by Ayoub Touti"), gbc);
}
}
}
See How to Use GridBagLayout for more details
I am new to swing and trying to make a simple quiz game just for practice reasons. What I want to make is like this:
This was my first implementation but there I extends JFrame and make all of this in one frame then i realize i need to use cardlayout to change from the main menu to the playing scene so i split it to 1 class main frame and other classes for the mainmenu, play scene, game over scene, etc. So my MainMenu now extends JPanel and when I add buttons to the button panel and then add buttonpanel to the mainpanel i got this:
Its like the panel containing label of the main menu and the button panel are next to each other but i need them like in the first picture. Here is my MainMenu class:
public class MainMenu extends JPanel{
private JLabel menuTitle;
private JPanel menuTitlePanel;
private JPanel buttonPanel;
public MainMenu(){
this.setBackground(new Color(0, 128, 43));
//ADDING THE TITLE
menuTitle = new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>");
menuTitle.setForeground(Color.BLACK);
menuTitlePanel = new JPanel(new GridBagLayout());
menuTitlePanel.setBackground(new Color(0, 128, 43));
GridBagConstraints gbcTitle = new GridBagConstraints();
gbcTitle.weightx = 0;
gbcTitle.weighty = 0;
gbcTitle.gridx = 0;
gbcTitle.gridy = 0;
gbcTitle.gridwidth = 3;
gbcTitle.insets = new Insets(70, 0, 0, 0);
menuTitlePanel.add(menuTitle, gbcTitle);
this.add(menuTitlePanel, BorderLayout.NORTH);
buttonPanel = new JPanel(new GridBagLayout());
buttonPanel.setBackground(new Color(0, 128, 43));
JButton startButton = new JButton("Start");
GridBagConstraints gbcStart = new GridBagConstraints();
gbcStart.gridx = 1;
gbcStart.gridy = 1;
gbcStart.ipadx = 50;
gbcStart.ipady = 10;
gbcStart.gridwidth = 3;
gbcStart.insets = new Insets(10, 0, 0, 0);
buttonPanel.add(startButton, gbcStart);
this.add(buttonPanel, BorderLayout.CENTER);
}
}
Any help will be appreciated.
So, you basically have two basic groups, the title and the buttons, these two need to be managed individually, as they have different layout requirements (primarily, the buttons been laid out in the middle).
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.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class TestMenu {
public static void main(String[] args) {
new TestMenu();
}
public TestMenu() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.add(new MenuPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MenuPane extends JPanel {
public MenuPane() {
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTH;
add(new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>"), gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
JPanel buttons = new JPanel(new GridBagLayout());
buttons.add(new JButton("Start"), gbc);
buttons.add(new JButton("Show scores"), gbc);
buttons.add(new JButton("Help"), gbc);
buttons.add(new JButton("Exit"), gbc);
gbc.weighty = 1;
add(buttons, gbc);
}
}
}
That should help with the current question, but you'll probably also want to have a look at How to use CardLayout for details about how you could switch between different views
I see you haven't set a layout for MainMenu, perhaps try Boxlayout and see if it offers the result you are after:
public MainMenu(){
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
...
this.add(menuTitlePanel, Component.CENTER_ALIGNMENT);
...
this.add(buttonPanel, Component.CENTER_ALIGNMENT);
...
}
I am having to migrate from Python TKinter to Java Swing due to a change of jobs.
The problem I have is:
How do I position a panel containing objects such as labels and text fields in the top left hand corner of JTabbedFrame.
If the frame itself is bigger than the panel , the JPanel is centering by default on the page.
Here is my code so far:
public class GUI {
//Constructor
GUI(){
//Craete a new frame
//borderlayout
JFrame jfrm = new JFrame("Tabbed Demo");
//Intialize size
jfrm.setSize(500, 250);
//Terminate program
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create Tabbed Frame
JTabbedPane jtp = new JTabbedPane(JTabbedPane.TOP);
//Create a panel to place on tabbed page
JPanel jpnl = new JPanel();
jpnl.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5,5,5,5);
jpnl.setOpaque(true);
jpnl.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
//create a 3 labels horizontally
gbc.gridx=1;
gbc.gridy=0;
JLabel title= new JLabel(" Tracking System ");
title.setFont(new Font("Serif", Font.PLAIN, 40));
jpnl.add(title,gbc);
gbc.gridx=0;
gbc.gridy=1;
JLabel subtitle= new JLabel("First");
subtitle.setFont(new Font("Serif", Font.PLAIN, 18));
jpnl.add(subtitle,gbc);
gbc.gridx=1;
gbc.gridy=1;
JTextArea firstText = new JTextArea("Type Here!");
firstText.setFont(new Font("Serif", Font.PLAIN, 18));
jpnl.add(firstText,gbc);
gbc.gridx=0;
gbc.gridy=2;
JLabel subtitle2= new JLabel("Last");
jpnl.add(subtitle2,gbc);
subtitle2.setFont(new Font("Serif", Font.PLAIN, 18));
//add to Tab
jtp.addTab("Demo", jpnl);
//make visible
jfrm.getContentPane().add(jtp);
jfrm.setVisible(true);
}
public static void main(String[] args) {
// launch app
//create the frame on the event dispaytching thread
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new GUI();
}
});
}
}
The result is shown below
What I want to do is move the location of the JPanel to the top left no matter how big the frame is, eg location 0,0 of the tabbed page.Override the centring of the JPanel.
I am using the GridBaglayout manager.
Not to discount the power of something like MigLayout, but if you're stuck on a project which limits third party libraries, then knowing how to manipulate the default layout managers is one of the most important skills you can develop
You could simply add a "filler" component into you layout, setting it's weightx and weighty properties to fill the available space
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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 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.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.gridwidth = 2; //GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 4, 4, 4);
JLabel title = new JLabel("Tracking System");
title.setFont(title.getFont().deriveFont(40f));
add(title, gbc);
gbc.gridy++;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First: "), gbc);
gbc.gridy++;
add(new JLabel("First: "), gbc);
gbc.gridx++;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridx++;
gbc.gridy++;
gbc.weightx = 1;
gbc.weighty = 1;
// Blank/filler component
add(new JLabel(), gbc);
}
}
}
By changing
gbc.gridwidth = 2; //GridBagConstraints.REMAINDER;
to
gbc.gridwidth = GridBagConstraints.REMAINDER;
I can effect the position of the title as well
But you can also effect it by using the anchor property. Which you use will come down to your needs
Have a look at How to Use GridBagLayout for more details
I'm trying to figure out the best way to align 2 sets of items in the center of a panel in a Java Swing application. The panel is in the North position of a BorderLayout whose width is determined by the JTextField in the Center position of the layout. The problem I'm having is, I have a set of labels and smaller text fields that I want to center so that the end of the label and the start of the first text field meet at the center of the panel.
I've tried GroupLayout, but ended up with the following result:
Note: The 2 text fields separated by a + are in a sub-panel.
What I'm trying to achieve is the following:
Apparently I'm either missing something, or this is far more complicated than necessary to do. I actually run into this issue a LOT! I'm surprised there isn't a special grid layout specifically for this.
Trying to do this with a GridLayout resulted in this:
So... what IS the easiest way to get the layout I'm looking for (second image)?
GroupLayout example code below:
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
frame.setContentPane(panel);
JPanel longText = new JPanel();
JPanel shortText = new JPanel();
JPanel mediumText = new JPanel();
longText.add(new TextField(5));
longText.add(new JLabel("+"));
longText.add(new TextField(5));
shortText.add(new TextField(5));
shortText.add(new JLabel("+"));
shortText.add(new TextField(5));
mediumText.add(new TextField(5));
mediumText.add(new JLabel("+"));
mediumText.add(new TextField(5));
JLabel lExample = new JLabel("Long text example:");
JLabel sExample = new JLabel("Short:");
JLabel mExample = new JLabel("Medium Example:");
JPanel subPanel = new JPanel();
GroupLayout layout = new GroupLayout(subPanel);
subPanel.setLayout(layout);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.CENTER)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.TRAILING)
.addComponent(lExample)
.addComponent(sExample)
.addComponent(mExample))
.addGroup(layout.createParallelGroup(Alignment.TRAILING)
.addComponent(longText)
.addComponent(shortText)
.addComponent(mediumText))))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(lExample)
.addComponent(longText))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(sExample)
.addComponent(shortText))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(mExample).addComponent(mediumText))
);
JTextArea textArea = new JTextArea() {
#Override
public Dimension getPreferredSize() {
return new Dimension(600,300);
}
};
textArea.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setAutoscrolls(true);
panel.add(subPanel,BorderLayout.NORTH);
panel.add(textArea,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
Consider using a GridBagLayout, as it gives you a lot more control over the placement of individual components and respects the preferred size of the components where it can (unless you override them through the use of the GridBagConstraints)
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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() {
JLabel longText = new JLabel("Long Text Example");
JLabel shortText = new JLabel("Short Example");
JLabel medText = new JLabel("Medium Example");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(longText, gbc);
addFields(gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.EAST;
add(shortText, gbc);
addFields(gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.EAST;
add(medText, gbc);
addFields(gbc);
}
protected void addFields(GridBagConstraints gbc) {
JTextField field1 = new JTextField("0", 5);
field1.setEnabled(false);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx++;
add(field1, gbc);
gbc.gridx++;
gbc.insets = new Insets(0, 4, 0, 4);
add(new JLabel("+"), gbc);
JTextField field2 = new JTextField(5);
gbc.gridx++;
add(field2, gbc);
}
}
}