This is my first GUI application and I'm having trouble making it look neat. I have tried several layouts and tinkered with them e.g flow, grid, border. When I run the program everything is just jumbled together.
I would like it to look like:
Unloaded Measurement |textfield| |textfield|
Loaded Rider Measurement |textField| |textfield|
Loaded Bike Measurement |textField| |Textfield|
|Button|
_______________________________________________________________________________________
Race Sag: |TextField|
Free Sag: |TextField|
Race Sag Notes: | TextField |
Free Sag Notes: | TextField |
Here is a screenshot to help understand what my issue is:
The top is for user input and the bottom is calculated output. I hope that I have given enough details for some help with this. I really appreciate anyone that helps out!
Here is my code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JLabel messageLabel;
private JLabel messageLabel1;
private JLabel messageLabel2;
private JLabel raceSagLabel;
private JLabel freeSagLabel;
private JTextField wholeTextField;
private JTextField wholeTextField1;
private JTextField wholeTextField2;
private JTextField fracTextField;
private JTextField fracTextField1;
private JTextField fracTextField2;
private JTextField raceSagText;
private JTextField freeSagText;
private JTextField noteText;
private JTextField noteText1;
private JButton calcButton;
private final int WINDOW_WIDTH = 575;
private final int WINDOW_HEIGHT = 400;
/*===============================================================================
Project : test.java - SagCalculator
Author : Brian Green
Date : Jan 10, 2013
Abstract:
===============================================================================*/
public static void main(String[] args) {
#SuppressWarnings("unused")
Main sc = new Main();
}
public Main(){
setTitle("Rider Sag Calculator");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setLayout();
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel(){
messageLabel = new JLabel("Enter Unloaded Stand Measurement");
messageLabel1 = new JLabel("Enter Loaded with Rider Measurement");
messageLabel2 = new JLabel("Enter Loaed without Rider Measurement");
wholeTextField = new JTextField(3);
fracTextField = new JTextField(3);
wholeTextField1 = new JTextField(3);
fracTextField1 = new JTextField(3);
wholeTextField2 = new JTextField(3);
fracTextField2 = new JTextField(3);
calcButton = new JButton("Calculate");
raceSagLabel = new JLabel("Race Sag: ");
raceSagText = new JTextField(5);
freeSagLabel = new JLabel("Free Sag: ");
freeSagText = new JTextField(5);
noteText = new JTextField(30);
noteText1 = new JTextField(30);
calcButton.addActionListener(new CalcButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(wholeTextField);
panel.add(fracTextField);
panel.add(messageLabel1);
panel.add(wholeTextField1);
panel.add(fracTextField1);
panel.add(messageLabel2);
panel.add(wholeTextField2);
panel.add(fracTextField2);
panel.add(calcButton);
panel.add(raceSagLabel);
panel.add(raceSagText);
panel.add(freeSagLabel);
panel.add(freeSagText);
panel.add(noteText);
panel.add(noteText1);
}
If for some reason you need to see more of the code, just let me know. I will be happy to provide it. Thanks for the help!
I got this all worked out! I would like to say thanks to #trashgod for his suggestion:
Try calling pack() on your JFrame, the layout doesn't get applied immediately when you set it. Alternatively, you can use validate(), which should lay out the component as well. Btw. in your sample code you don't set a layout manager.
It's possible to achieve the layout that you want with some of the other suggestions here (including GridBagLayout), but the easiest solution by far is to use the JGoodies FormLayout,
since it's explicitly designed for creating layouts where you need to line up fields and labels.
GroupLayout, see here, does a nice job on labeled forms, and it is manageable as a subpanel.
Related
I am stumped and desperate for help. Been able to figure everything else out until I got into GUI's. What I am trying to do is go from the LogInPane (Log In Page) to JobSelectionGUI (Job Selection Page);
When I compile it runs how I want it to, but I can't figure out how to get my JFrame from LogInPaneGUI to close when it opens JobSelectionGUI, Tons of reading and videos and picking GUI's/Applying them is rough! I started with a GridLayout then switched to GridBagLAyout, then tried CardLayout and now back to GBL.
I don't use an IDE; I use SublimeText, so if something looks extremely elongated in my code, its because I had to write it out long for Sublime (or because I'm bad). All my code is separated into different classes so it stays neat and easy to debug. Every class has its own package, and every package has no more than 2 Methods. I work my butt off to keep my main completely empty!
Taking all criticism and advice!
MAIN:
package com.hallquist.kurtis.leigh.srcmain;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.text.*;
// Class imports;
import JobSelection.*;
import LogInPane.*;
// My Main function. Used to pull packages and methods and compile them here;
public class SrcMainUserInformation{
public static void main(String[] args){
LogInPaneGUI logInGUI = new LogInPaneGUI();
logInGUI.logInPaneMainGUI();
}
}
First Class:
package LogInPane; // package name;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import JobSelection.*; //import for next GUI when LogInButton is clicked;
public class LogInPaneGUI{
private static final JFrame frame = new JFrame();
private static final int COLS = 10; // Max columns;
private static final JPanel panelForm = new JPanel(new GridBagLayout()); // layout
private static final JTextField fieldLogInName = new JTextField(COLS); //login
private static final JPasswordField logInPassword = new JPasswordField(COLS); //pw
private static final JButton logInButton = new JButton("Log In"); //login button
private static final JButton exitButton = new JButton("EXIT"); //system.exit button
private static final JButton newUser = new JButton("New User? Click here to sign up!"); // new user button
// Wigits on login page;
public LogInPaneGUI(){
GridBagConstraints c = new GridBagConstraints();
// Creates the panel that goes ontop of the JFrame;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.LINE_END;
panelForm.add(new JLabel("Account Name: "), c);
c.gridy ++;
panelForm.add(new JLabel("Password: "), c);
c.gridy ++;
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.LINE_START;
panelForm.add(fieldLogInName, c);
c.gridy++;
panelForm.add(logInPassword, c);
c.gridy++;
panelForm.add(logInButton, c);
c.gridy++;
panelForm.add(newUser, c);
c.gridy++;
panelForm.add(exitButton, c);
// Goes to fourm/website on newUser click;
newUser.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
System.out.println("Fourm/Website to sign up for game");
}
});
// Exits program on exitButton click;
exitButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
// Goes to JobSelectionGUI on logInButton Click;
logInButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
JobSelectionGUI jobSelecting = new JobSelectionGUI();
jobSelecting.jobSelectionJFrameGUI();
// frame.dispose();
// System.out.println("Will log you in when I set it up");
}
});
}
// Actual JFrame that everything goes ontop of;
public static void logInPaneMainGUI(){
JFrame frame = new JFrame("FirstProject");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(1080, 720);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(panelForm);
}
}
Second Class:
package JobSelection;
import java.awt.*;
import java.awt.event.ActionEvent.*;
import javax.swing.*;
import javax.swing.text.*;
// Mass import from JobSelection package; All base job information;
import JobSelection.JobInformationIndex.JobAmazonData.*;
import JobSelection.JobInformationIndex.JobBanditData.*;
import JobSelection.JobInformationIndex.JobLancerData.*;
import JobSelection.JobInformationIndex.JobSorcererData.*;
import JobSelection.JobInformationIndex.JobWitchData.*;
import LogInPane.*; // to return to login screen;
import JobSelection.*; // dont know if needed;
public class JobSelectionGUI{
private static JFrame frame = new JFrame();
private static JSplitPane jSplitPane = new JSplitPane();
private static JPanel leftPane = new JPanel();
private static JLabel logInCharacterName = new JLabel();
private static JTextField userCharacterName = new JTextField();
private static JButton logInAccept = new JButton("Accept");
private static JButton logInBack = new JButton("Back");
private static JTextArea firstGameIntroduction = new JTextArea();
private static JTextArea descriptionIntroduction = new JTextArea();
private static JTextArea jobSelectedInformation = new JTextArea();
private static JPanel allWidgits = new JPanel();
public JobSelectionGUI(){
JTextArea firstGameIntroduction = new JTextArea("\ Text.");
firstGameIntroduction.setLineWrap(true);
firstGameIntroduction.setWrapStyleWord(true);
JScrollPane areaScrollPane = new JScrollPane(firstGameIntroduction);
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 250));
areaScrollPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Introduction"),
BorderFactory.createEmptyBorder(5,5,5,5)),
areaScrollPane.getBorder()));
leftPane.add(areaScrollPane);
JTextArea descriptionIntroduction = new JTextArea(" Text.\n");
descriptionIntroduction.setLineWrap(true);
descriptionIntroduction.setWrapStyleWord(true);
JScrollPane areaScrollPaneTwo = new JScrollPane(descriptionIntroduction);
areaScrollPaneTwo.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPaneTwo.setPreferredSize(new Dimension(250, 250));
areaScrollPaneTwo.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("What to expect"),
BorderFactory.createEmptyBorder(5,5,5,5)),
areaScrollPaneTwo.getBorder()));
leftPane.add(areaScrollPaneTwo);
}
public static void jobSelectionJFrameGUI(){
JFrame frame = new JFrame("FirstProject");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1080, 720);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(leftPane);
}
}
You grossly overuse the static modifier and most of your fields should not in fact be static. I would venture to state that most if not all of your class's fields should be private instance fields.
The log-in window should not be a JFrame but rather a blocking or "modal" dialog, and for Swing that means using a modal JDialog or JOptionPane (which creates a modal JDialog behind the scenes)
A modal dialog will block the calling code when it is displayed
And if the dialog is modal, then you know when it is no longer visible since the calling code is unblocked. This is when you would query the state of the dialog's fields (using public getter methods, not calling static fields directly), and decide if the login was successful or not. If so, show your main GUI window or JFrame.
Another option is to yes, use CardLayout, but for this to work, all your major GUI classes should be geared towards creating JPanels, not JFrames. This way you can insert the panels where and when needed, including within top-level windows such as JFrames or JDialogs, within or JPanels, or swapped using a CardLayout.
Note that frame.dispose() isn't working for you because you shadow the frame field by re-declaring it logInPaneMainGUI() method.
public static void logInPaneMainGUI() {
// ***** this creates a new local JFrame variable called frame
JFrame frame = new JFrame("FirstProject");
// so calling frame.dispose() elsewhere will have no effect on this window
Don't do this, and the .dispose() method call will close the first window.
public static void logInPaneMainGUI() {
frame = new JFrame("FirstProject"); // this initializes the frame field
// so calling frame.dispose() elsewhere will have no effect on this window
Of course frame would have to be non-final or not-initialized previously. I still would recommend using a JDialog however, as well as moving out of the static world and into the instance world
Unrelated criticism:
Less chatty text in your question, text that is completely unrelated to your actual problem at hand, and more text that tells us useful information that helps us to understand your problem and your code, and thereby helping us solve the problem.
Hi though i did complete the basics of java which im fairly new of, i keep getting errors when i try to add buttons on a new Frame/Panel. Care to educate me on what the problem might be?
import javax.swing.*;
import java.awt.*;
class MainClass {
String cont_orders;
private JFrame frame1;
private JPanel mainpanel;
JButton bt1, bt2, bt3, bt4, bt5;
private JButton btotal = new JButton("Order");
private JButton clearOr = new JButton("Clear");
private JTextField pricetotal = new JTextField();
private JTextField list_of_orders = new JTextField();
public MainClass(){
gui();
}
private void gui(){
frame1 = new JFrame("Order");
frame1.setSize(500,430);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setResizable(false);
mainpanel = new JPanel();
mainpanel.setBackground(Color.BLUE);
mainpanel.add(bt1);
bt1 = new JButton("M-Item 1 [Soda]");
frame1.add(mainpanel,BorderLayout.CENTER);
}
public static void main (String[]args){
new MainClass();
}
}
im trying to practice on coding normally instead of relying on that automatic one in NetBeans [JFrame Form/JPanel Form]
care to help?
Now this cannot be done in java
mainpanel.add(bt1);
bt1 = new JButton("M-Item 1 [Soda]");
Turn it around.
Explanation: the field bt1 is at that time a variable holding the null object.
That object (value) is added, not some variable address as in other languages.
Reverse it bt1 = new JButton("M-Item 1 [Soda]"
mainpanel.add(bt1);
Because if not the value of bt1 would be null so you must fill it first then use it .
Or
mainpanel.add(new JButton("..."));
I'm having a very hard time with borders, I've looked through tutorials and examples and each seems to use a different style, I'm just trying to organise the content into separated borders. The content for the bottom panal isn't finished yet but I'm just trying to get it to work as is before adding more.
The compiler says there are problems on two lines java.lang.NullPointerException:
package question2;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
/**
* #author Matt Headley
This frame shows a data set and its statistics.
*/
public class ComputerStoreGUI extends JFrame
{
private JCheckBox item1;
private JCheckBox item2;
private JCheckBox item3;
private JCheckBox item4;
private JCheckBox item5;
private TitledBorder border;
private TitledBorder border2;
private static JPanel content;
private static JPanel top;
private static JPanel bottom;
private JTextField parts;
public ComputerStoreGUI()
{
JCheckBox item1 = new JCheckBox("Install Hard Drive - $25.00");
JCheckBox item2 = new JCheckBox("Install RAM - $15.00");
JCheckBox item3 = new JCheckBox("Remove Virus - $50.00");
JCheckBox item4 = new JCheckBox("Format Hard Drive - $80.00");
JCheckBox item5 = new JCheckBox("Quote Hourly Labour - $10.00");
item1.setHorizontalAlignment(JCheckBox.LEFT);
item2.setHorizontalAlignment(JCheckBox.LEFT);
item3.setHorizontalAlignment(JCheckBox.LEFT);
item4.setHorizontalAlignment(JCheckBox.LEFT);
item5.setHorizontalAlignment(JCheckBox.LEFT);
JTextField cost = new JTextField(10);
top = new JPanel(new FlowLayout(FlowLayout.LEFT));
top.add(item1);
top.add(item2);
top.add(item3);
top.add(item4);
top.add(item5);
bottom.add(cost); //here ????????
cost.setHorizontalAlignment(JTextField.CENTER);
}
public static void main(String[] args)
{
JFrame frame = new ComputerStoreGUI(); // and here ???????
content = new JPanel();
content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
top = new JPanel();
top.setBorder(BorderFactory.createTitledBorder("Standard Services"));
bottom = new JPanel();
bottom.setBorder(BorderFactory.createTitledBorder("Hourly Service"));
frame.setSize(250, 400);
frame.setTitle("LU Computer Store");
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.add(top);
content.add(bottom);
frame.setVisible(true);
}
}
the end goal is to look something like this:
First, I dont see a reson to extend JFrame in your example. So without inheritance you can use composition like:
JFrmae frame = new JFrame();
Read more: Why do we need to extend JFrame in a swing application?
The compiler says there are problems on two lines
java.lang.NullPointerException:
Reason is, you have declared the JPanel bottom but without initializing you are trying to use it, from this line: bottom.add(cost);
Do something like:
bottom = new JPanel(new FlowLayout(FlowLayout.LEFT));
bottom.add(cost);
EDIT:
comment: It runs now, but the panels are tiny
Because you need to set layout for the parent JPanel, you can use BoxLayout, to add child panels one below another, like below:
content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
In case if you want to add space between those two child panels, use as below:
content.add(top);
content.add(Box.createRigidArea(new Dimension(0,10)));
content.add(bottom);
Im rather new at Java, I am have created a home page and a few buttons, When i click one of the buttons it sets the homepage panel visibility to false, opens a new class and sets that classes Jpanel to visible.
homePanel.setVisible(false);
Goodsin Barcode = new Goodsin();
Goodsin.setVisible(true);
However once it opens the new class "Goodsin" it wont show any of the Buttons or TextFileds. I know it is opening the new class as a System.out.println prints to the console but nothing displays in the JFrame and i do not know why.
Here is my code of the new class
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Goodsin {
public JPanel Goodsin;
public JTextField item1;
public String code;
public JButton btn1;
public Goodsin() {
System.out.println("TEST");
Goodsin = new JPanel();
item1 = new JTextField(10);
btn1 = new JButton("Look up Barcode");
Goodsin.setVisible(true);
Goodsin.add(item1);
item1.setSize(80, 30);
Goodsin.add(btn1);
btn1.setSize(80, 30);
}
public void getString(String code) {
System.out.println(code);
}
}
Im sure i am not doing something correct with the Jpanel or adding the textfields or button but all the answers i have seen so far havnt worked.
I suggest you add your panels to a JFrame. You can either do this by extending JFrame from the class or simply instantiating one in your constructor. Then you can simply add and remove (or set visible/invisible) as you wish. Be sure to validate your JFrame/JPanel after changing visibility though.
Try to do something like this:
Goodsin = new JPanel();
item1 = new JTextField(10);
btn1 = new JButton("Look up Barcode");
item1.setSize(80, 30);
Goodsin.add(item1);
btn1.setSize(80, 30);
Goodsin.add(btn1);
JFrame frame = new JFrame("JFrame Example");
Goodsin.setLayout(new FlowLayout());
frame.add(Goodsin);
I suggest you try the following code :
public class Goodsin extends JFrame{
public static void main(String[] args) {
Goodsin ui = new Goodsin();
JTextField item1 = new JTextField(10);
JButton btn1 = new JButton("Look up Barcode");
JPanel centralPanel = new JPanel(new FlowLayout());
centralPanel.add(item1);
centralPanel.add(btn1);
item1.setSize(80, 30);
btn1.setSize(80, 30);
ui.add(centralPanel);
ui.pack();
ui.setVisible(true);
}
}
Everything is done in the main method in my example, however you still can improve this code and refactor it in a better way.
just you have to set Bounds of Goodsin panel or set Size and add in home page frame
I am creating a basic user interface in Swing and was hoping for some help. Below is a screenshot of what I am trying to achieve:
My code currently is as follows:
package testui;
import java.awt.Container;
import javax.swing.*;
public class TestUI{
private JTextField outputArea = new JTextField();
private JTextField errorReportArea = new JTextField();
private JPanel inputPanel = new JPanel();
private JLabel nameLabel = new JLabel("Item Name");
private JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
private JLabel priceLabel = new JLabel("Price per unit (Or L) in pence");
private JTextField nameField = new JTextField(10);
private JTextField numberField = new JTextField(10);
private JTextField priceField = new JTextField(10);
private JButton addVolumeButton = new JButton("Add by Volume");
private JButton addNumberButton = new JButton("Add by number of units");
public TestUI() {
JFrame frame = new JFrame("Fuel Station");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputArea.setEditable(false);
errorReportArea.setEditable(false);
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(numberLabel);
inputPanel.add(numberField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
inputPanel.add(addVolumeButton);
inputPanel.add(addNumberButton);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(outputArea);
contentPane.add(errorReportArea);
contentPane.add(inputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
TestUI test1 = new TestUI();
}
}
Which looks like this:
So what I would like to do is set a specific size for the top two JTextFields, as the top one will contain multiple lines of text, and the one below will contain just one line of text. I am unsure how to do this without using setSize, as I have been told it is bad coding practice to use this.
I would also like to add some padding between the JLabels, JTextFields and JButtons in the bottom JPanel.
If anyone could give me some suggestions on resizing these components I would be most grateful
Since you want your textfields to be multilined, use JTextAreas. JTextFields are single lined only.
Your components are right next to each other which isn't the same look as your intended outcome. There may be some method that gives your components some breathing room before you would call frame.pack()
Look for any method that can make a component fill the total amount of room it's given; especially when you want something to fill a large chunk of space.
You can set the number of columns instead of using setSize() for your JTextFields/JTextAreas. Just saying.
Reviewing all of Java's Layout Managers would help you get a grasp of the capabilities and use cases for each layout manager
There are a few layout managers that are flexible enough to perform this, such as Mig, Gridbag, and SpringLayout.
In your case, you'd have the following constraints:
outputarea - south border constrained to be ###px from the north border of the contentPane
errorReportArea - north border constrained to be 0px from outputarea's south, and south border constrained to be 0px from inputPanel's north.
inputPanel - north border constrained to be ##px from the south border of the contentPane.
GUI builders such as WindowBuilder will allow you to do this pretty quickly. You just drop in the layout onto the contentPane and then set the constraints.
If you have to use a box layout look at the glue and rigidArea methods in Box. If you can use other layouts, go with those suggested by the other answers.
I have created a solution with the MigLayout manager.
Here are some recommendations:
Put application code outside the constructor; in the solution, the code
is placed in the initUI() method.
The application should be started on EDT by calling the
EventQueue.invokeLater(). (See the main() method of the provided solution.)
Use a modern, flexible layout manager: MigLayout, GroupLayout, or FormLayout.
Take some time to study them to fully understand the layout management process. It
is important have a good understanding of this topic.
Shorten the labels; use more descriptive tooltips
instead.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutSolution extends JFrame {
private JTextArea outputArea;
private JTextField errorReportField;
private JLabel nameLabel;
private JLabel numberLabel;
private JLabel priceLabel;
private JTextField nameField;
private JTextField numberField;
private JTextField priceField;
private JButton addVolumeButton;
private JButton addNumberButton;
public MigLayoutSolution() {
initUI();
}
private void initUI() {
setLayout(new MigLayout());
outputArea = new JTextArea(10, 20);
errorReportField = new JTextField(15);
nameLabel = new JLabel("Item name");
numberLabel = new JLabel("# of units");
numberLabel.setToolTipText("Number of units (or Volume in L)");
priceLabel = new JLabel("Price per unit");
priceLabel.setToolTipText("Price per unit (Or L) in pence");
nameField = new JTextField(10);
numberField = new JTextField(10);
priceField = new JTextField(10);
addVolumeButton = new JButton("AddVol");
addVolumeButton.setToolTipText("Add by Volume");
addNumberButton = new JButton("AddNum");
addNumberButton.setToolTipText("Add by number of units");
add(new JScrollPane(outputArea), "grow, push, wrap");
add(errorReportField, "growx, wrap");
add(nameLabel, "split");
add(nameField);
add(numberLabel);
add(numberField);
add(priceLabel);
add(priceField);
add(addVolumeButton);
add(addNumberButton);
pack();
setTitle("Fuel station");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutSolution ex = new MigLayoutSolution();
ex.setVisible(true);
}
});
}
}