Reputation: 169
I am trying to build a simple GUI to process customer orders [of course this is just for practice]. However, I cannot format my components using GridLayout. Could someone suggest a better method and make that change in the code, so I can learn the implementation?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class RectangleProgram extends JFrame
{
private JLabel Title, LastName, FirstName, LawnSize, TotalCost, RunningTotal;
private JTextField Field1, Field2, Field3, Field4, Field5;
private JButton Next, Quit;
private re x= new re(); private me xx=new me();
public RectangleProgram()
{
setSize(500,500);
setTitle("Sample1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane=getContentPane();
pane.setLayout(new GridLayout(5,2));
Title=new JLabel("Green and Grow Mowing Company");
LastName=new JLabel("Last Name: ");
FirstName=new JLabel("First Name: ");
LawnSize=new JLabel("Lawn Size: ");
TotalCost=new JLabel("Total Cost: ");
RunningTotal=new JLabel ("Running Total: ");
Field1= new JTextField (10); Field2= new JTextField(10); Field3= new JTextField(10); Field4= new JTextField(10); Field5= new JTextField(10);
Next= new JButton("Next"); Quit= new JButton("Quit");
Next.addActionListener(x); Quit.addActionListener(xx);
pane.add(Title); pane.add(LastName); pane.add(Field1); pane.add(FirstName);
pane.add(Field2); pane.add(LawnSize); pane.add(Field3); pane.add(TotalCost); pane.add(Field4);
pane.add(RunningTotal); pane.add(Field5);
pane.add(Next); pane.add(Quit);
setVisible(true);
}
public class re implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String firstname, lastname; double ans;
firstname=Field2.getText();
lastname=Field1.getText(); String fullname=firstname+" "+lastname;
fullname="Hello "+fullname+"!";
JOptionPane.showMessageDialog(null, fullname);
ans=Double.parseDouble(Field3.getText());
String toput="Your total is "+ans;
JOptionPane.showMessageDialog(null,ans);
Field4.setText("$"+ans);
Field5.setText("$"+ans);
}
}
public class me implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
RectangleProgram x= new RectangleProgram();
}
}
Upvotes: 0
Views: 309
Reputation: 11637
GridLayout
is a very simple layout manager that cannot do much.
You can happily forget about it. I recommend you to study MigLayout
and GroupLayout
managers and choose from them. These two managers
are flexible and create layouts that are independent from font size
and screen resolution. GroupLayout
is a built-in manager, MigLayout
is a
third-party manager.
Layout management is a complex thing and there are no shortcuts.
MigLayout solution
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutSolution extends JFrame {
public MigLayoutSolution() {
initUI();
setTitle("MigLayout solution");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout("wrap 2, ins dialog", "[r]"));
pnl.add(new JLabel("Green and Grow Moving Company "), "span 2, center, wrap");
pnl.add(new JLabel("Last name:"), "gaptop u");
pnl.add(new JTextField(15), "pushx, growx");
pnl.add(new JLabel("First name:"));
pnl.add(new JTextField(15), "growx");
pnl.add(new JLabel("Lawn size:"));
pnl.add(new JTextField(15), "growx");
pnl.add(new JLabel("Total cost:"));
pnl.add(new JTextField(15), "growx");
pnl.add(new JLabel("Running total:"));
pnl.add(new JTextField(15), "growx");
pnl.add(new JButton("Next"), "gaptop u, sgx, split 2, span 2, right");
pnl.add(new JButton("Quit"), "sgx");
add(pnl);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutSolution ex = new MigLayoutSolution();
ex.setVisible(true);
}
});
}
}
GroupLayout solution
package com.zetcode;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.CENTER;
import static javax.swing.GroupLayout.Alignment.TRAILING;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import static javax.swing.LayoutStyle.ComponentPlacement.UNRELATED;
public class GroupLayoutSolution extends JFrame {
public GroupLayoutSolution() {
initUI();
}
private void initUI() {
Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
JLabel titleLbl = new JLabel("Green and Grow Moving Company");
JLabel fnLbl = new JLabel("First name:");
JLabel lnLbl = new JLabel("Last name:");
JLabel lsLbl = new JLabel("Lawn size:");
JLabel tsLbl = new JLabel("Total cost:");
JLabel rtLbl = new JLabel("Running total:");
JTextField field1 = new JTextField(15);
JTextField field2 = new JTextField(15);
JTextField field3 = new JTextField(15);
JTextField field4 = new JTextField(15);
JTextField field5 = new JTextField(15);
JButton nextBtn = new JButton("Next");
JButton quitBtn = new JButton("Quit");
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup(CENTER)
.addComponent(titleLbl)
.addGroup(gl.createParallelGroup(TRAILING)
.addGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup(TRAILING)
.addComponent(fnLbl)
.addComponent(lnLbl)
.addComponent(lsLbl)
.addComponent(tsLbl)
.addComponent(rtLbl))
.addGroup(gl.createParallelGroup()
.addComponent(field1)
.addComponent(field2)
.addComponent(field3)
.addComponent(field4)
.addComponent(field5)))
.addGroup(gl.createSequentialGroup()
.addComponent(nextBtn)
.addComponent(quitBtn)))
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(titleLbl)
.addPreferredGap(UNRELATED)
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(fnLbl)
.addComponent(field1))
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(lnLbl)
.addComponent(field2))
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(lsLbl)
.addComponent(field3))
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(tsLbl)
.addComponent(field4))
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(rtLbl)
.addComponent(field5))
.addPreferredGap(UNRELATED)
.addGroup(gl.createParallelGroup()
.addComponent(nextBtn)
.addComponent(quitBtn))
);
pack();
setTitle("GroupLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GroupLayoutSolution ex = new GroupLayoutSolution();
ex.setVisible(true);
}
});
}
}
Upvotes: 1
Reputation: 465
It smells like homework but since there is a chance that it isn't.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class RectangleProgram extends JFrame
{
private JLabel Title, LastName, FirstName, LawnSize, TotalCost, RunningTotal;
private JTextField Field1, Field2, Field3, Field4, Field5;
private JButton Next, Quit;
public RectangleProgram()
{
re r=new re(); // the actionlistner
setTitle("Sample1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
Title=new JLabel("Green and Grow Mowing Company");
LastName=new JLabel("Last Name: ");
FirstName=new JLabel("First Name: ");
LawnSize=new JLabel("Lawn Size: ");
TotalCost=new JLabel("Total Cost: ");
RunningTotal=new JLabel ("Running Total: ");
Field1= new JTextField (10);
Field2= new JTextField(10);
Field3= new JTextField(10);
Field4= new JTextField(10);
Field5= new JTextField(10);
Next= new JButton("Next");
Quit= new JButton("Quit");
Next.addActionListener(r);
Quit.addActionListener(r);
// notice that you have to define the constraints before placing each component
c.gridwidth=2; // allows the title to span 2 columns
c.gridx=0;
c.gridy=0;
add(Title, c);
c.gridwidth=1; // sets the column span back to 1 column per component
c.gridx=0;
c.gridy=1;
c.anchor=GridBagConstraints.EAST; // alignment set to the east
add(LastName, c);
c.gridx=1;
c.gridy=1;
c.anchor=GridBagConstraints.WEST; // alignment set to the west
add(Field1, c);
c.gridx=0;
c.gridy=2;
c.anchor=GridBagConstraints.EAST;
add(FirstName, c);
c.gridx=1;
c.gridy=2;
c.anchor=GridBagConstraints.WEST;
add(Field2, c);
c.gridx=0;
c.gridy=3;
c.anchor=GridBagConstraints.EAST;
add(LawnSize, c);
c.gridx=1;
c.gridy=3;
c.anchor=GridBagConstraints.WEST;
add(Field3, c);
c.gridx=0;
c.gridy=4;
c.anchor=GridBagConstraints.EAST;
add(TotalCost, c);
c.gridx=1;
c.gridy=4;
c.anchor=GridBagConstraints.WEST;
add(Field4, c);
c.gridx=0;
c.gridy=5;
c.anchor=GridBagConstraints.EAST;
add(RunningTotal, c);
c.gridx=1;
c.gridy=5;
c.anchor=GridBagConstraints.WEST;
add(Field5, c);
c.gridx=0;
c.gridy=6;
c.anchor=GridBagConstraints.CENTER;
add(Next, c);
c.gridx=1;
c.gridy=6;
add(Quit, c);
pack();
setVisible(true);
}
public class re implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if(e.getSource()==Next){
String firstname, lastname;
double ans;
firstname=Field2.getText();
lastname=Field1.getText();
String fullname=firstname+" "+lastname;
fullname="Hello "+fullname+"!";
JOptionPane.showMessageDialog(null, fullname);
ans=Double.parseDouble(Field3.getText());
String toput="Your total is "+ans;
JOptionPane.showMessageDialog(null,ans);
Field4.setText("$"+ans);
Field5.setText("$"+ans);
}else if(e.getSource().equals(Quit))
{
System.exit(0);
}
}
}
public static void main(String[] args)
{
RectangleProgram x= new RectangleProgram();
}
}
You should consider using only one actionlistener for both buttons. You can alternatively use a lamda expression for the exit button since it only contains one line of code.
Upvotes: 0