Reputation: 61
I know this is a lot of code. I'm trying to get it so the layout is nicer, and I can't get anything to work.I want to get it so the HelpButton is bottom right, HelpField is bottom center, and the Clear button is bottom left.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
public class GUI extends JFrame { //open class
private final JTextField HelpField, tField, uField, xField, yField, tPlusuField, tMinusuField, uDividedtField, uTimestField, xPlusyField, xMinusyField, yDividedxField, yTimesxField;
private final JLabel tLabel, uLabel, xLabel, yLabel, tPlusuLabel, tMinusuLabel, uDividedtLabel, uTimestLabel, xPlusyLabel, xMinusyLabel, yDividedxLabel, yTimesxLabel;
private final JButton doSimpleMath, Reset, HelpButton;
public GUI() { //open
super("GUI");
setLayout(new FlowLayout());
JPanel panLeft = new JPanel(new BorderLayout());
tLabel = new JLabel("Enter value you for t:");
tField = new JTextField(2);
add(tLabel);
add(tField);
uLabel = new JLabel("Enter value you for u:");
uField = new JTextField(2);
add(uLabel);
add(uField);
xLabel = new JLabel("Enter value you for x:");
xField = new JTextField(2);
add(xLabel);
add(xField);
yLabel = new JLabel("Enter value you for y:");
yField = new JTextField(2);
add(yLabel);
add(yField);
doSimpleMath = new JButton("Do Simple Math");
add(doSimpleMath);
Reset = new JButton("Reset");
add(Reset);
HelpButton = new JButton("Help");
add(HelpButton);
ButtonHandler handler = new ButtonHandler();
doSimpleMath.addActionListener(handler);
Reset.addActionListener(handler);
HelpButton.addActionListener(handler);
HelpField = new JTextField(40);
add(HelpField);
HelpField.setEditable(false);
tPlusuLabel = new JLabel("t + u =");
tPlusuField = new JTextField(5);
tPlusuField.setEditable(false);
add(tPlusuLabel);
add(tPlusuField);
tMinusuLabel = new JLabel("t - u =");
tMinusuField = new JTextField(5);
tMinusuField.setEditable(false);
add(tMinusuLabel);
add(tMinusuField);
uDividedtLabel = new JLabel("u / t =");
uDividedtField = new JTextField(5);
uDividedtField.setEditable(false);
add(uDividedtLabel);
add(uDividedtField);
uTimestLabel = new JLabel("u * t =");
uTimestField = new JTextField(5);
uTimestField.setEditable(false);
add(uTimestLabel);
add(uTimestField);
xPlusyLabel = new JLabel("x + y =");
xPlusyField = new JTextField(5);
xPlusyField.setEditable(false);
add(xPlusyLabel);
add(xPlusyField);
xMinusyLabel = new JLabel("x - y =");
xMinusyField = new JTextField(5);
xMinusyField.setEditable(false);
add(xMinusyLabel);
add(xMinusyField);
yDividedxLabel = new JLabel("y / x =");
yDividedxField = new JTextField(5);
yDividedxField.setEditable(false);
add(yDividedxLabel);
add(yDividedxField);
yTimesxLabel = new JLabel("y * x =");
yTimesxField = new JTextField(5);
yTimesxField.setEditable(false);
add(yTimesxLabel);
add(yTimesxField);
} // close
private class ButtonHandler implements ActionListener { //open ButtonHandler
@Override
public void actionPerformed(ActionEvent event) { //open actionPerformed
if(event.getSource() == doSimpleMath){ //close if
int t = Integer.parseInt(tField.getText()); //convert String to int
int u = Integer.parseInt(uField.getText());
int x = Integer.parseInt(xField.getText());
int y = Integer.parseInt(yField.getText());
int tSumu = t + u; //define values
int tMinusu = t - u;
int uDividedt = t / u;
int uTimest = u * t;
int xSumy = x + y;
int xMinusy = x - y;
int yDividedx = y / x;
int yTimesx = y * x;
tPlusuField.setText(tSumu + " "); //disply output
tMinusuField.setText(tMinusu + " ");
uDividedtField.setText(uDividedt + " ");
uTimestField.setText(uTimest + " ");
xPlusyField.setText(xSumy + " ");
xMinusyField.setText(xMinusy + " ");
yDividedxField.setText(yDividedx + " ");
yTimesxField.setText(yTimesx + " ");
} //close if
if (event.getSource() == Reset){ //open if
tPlusuField.setText(""); //disply output
tMinusuField.setText("");
uDividedtField.setText("");
uTimestField.setText("");
xPlusyField.setText("");
xMinusyField.setText("");
yDividedxField.setText("");
yTimesxField.setText("");
tField.setText("");
uField.setText("");
xField.setText("");
yField.setText("");
HelpField.setText("");
} //close if
if (event.getSource() == HelpButton) { //open if
HelpField.setText("You Need Help.");
} //close if
} // close actionPerformed
}//close ButtonHandler
} // close class
Upvotes: 1
Views: 169
Reputation: 347314
Remember, you can use any number of containers and layouts which you can combine together to generate complex layouts, for example:
public GUI() { //open
super("GUI");
JPanel panLeft = new JPanel(new BorderLayout());
JPanel fields = new JPanel();
JPanel actions = new JPanel();
tLabel = new JLabel("Enter value you for t:");
tField = new JTextField(2);
fields.add(tLabel);
fields.add(tField);
uLabel = new JLabel("Enter value you for u:");
uField = new JTextField(2);
fields.add(uLabel);
fields.add(uField);
xLabel = new JLabel("Enter value you for x:");
xField = new JTextField(2);
fields.add(xLabel);
fields.add(xField);
yLabel = new JLabel("Enter value you for y:");
yField = new JTextField(2);
fields.add(yLabel);
fields.add(yField);
doSimpleMath = new JButton("Do Simple Math");
fields.add(doSimpleMath);
HelpField = new JTextField(40);
add(HelpField);
HelpField.setEditable(false);
Reset = new JButton("Reset");
add(Reset);
HelpButton = new JButton("Help");
add(HelpButton);
actions.add(Reset);
actions.add(HelpField);
actions.add(HelpButton);
tPlusuLabel = new JLabel("t + u =");
tPlusuField = new JTextField(5);
tPlusuField.setEditable(false);
fields.add(tPlusuLabel);
fields.add(tPlusuField);
tMinusuLabel = new JLabel("t - u =");
tMinusuField = new JTextField(5);
tMinusuField.setEditable(false);
fields.add(tMinusuLabel);
fields.add(tMinusuField);
uDividedtLabel = new JLabel("u / t =");
uDividedtField = new JTextField(5);
uDividedtField.setEditable(false);
fields.add(uDividedtLabel);
fields.add(uDividedtField);
uTimestLabel = new JLabel("u * t =");
uTimestField = new JTextField(5);
uTimestField.setEditable(false);
fields.add(uTimestLabel);
fields.add(uTimestField);
xPlusyLabel = new JLabel("x + y =");
xPlusyField = new JTextField(5);
xPlusyField.setEditable(false);
fields.add(xPlusyLabel);
fields.add(xPlusyField);
xMinusyLabel = new JLabel("x - y =");
xMinusyField = new JTextField(5);
xMinusyField.setEditable(false);
fields.add(xMinusyLabel);
fields.add(xMinusyField);
yDividedxLabel = new JLabel("y / x =");
yDividedxField = new JTextField(5);
yDividedxField.setEditable(false);
fields.add(yDividedxLabel);
fields.add(yDividedxField);
yTimesxLabel = new JLabel("y * x =");
yTimesxField = new JTextField(5);
yTimesxField.setEditable(false);
fields.add(yTimesxLabel);
fields.add(yTimesxField);
add(fields);
add(actions, BorderLayout.SOUTH);
} // close
Take a look at A Visual Guide to Layout Managers and Laying Out Components Within a Container for more details
Upvotes: 2
Reputation: 24630
In your JFrame put a BorderLayout. In the SOUTH
of this put another BorderLayout. In this last BorderLayout put your components in WEST
, CENTER
and EAST
.
Upvotes: 0
Reputation: 439
try using (new AbsoluteLayout()) in setLayout and assign x and y co-ordinate for every component. it'll be helpful..you can place your components wherever you want.
Upvotes: -1