any one know how i can make this look better like the text fields up top and the add and subtract on the bottom side by side or something?
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import javax.swing.*;
publicclass TextFieldTest extends JFrame implements ActionListener
{
private JTextField number1Field; // used in both constructor and actionPerformed
private JTextField number2Field;
private JTextField sumField;
private JTextField subField;
public TextFieldTest(String title)
{
super(title);
// Set up text field for number 1
number1Field = new JTextField(4);
// Set up text field for number 2
number2Field = new JTextField(4);
// Set up text field for result
sumField = new JTextField(5);
sumField.setEditable(false);
subField = new JTextField(5);
subField.setEditable(false);
// set up a button that will add number 1 and number 2 and add a listener
JButton calcButton = new JButton("Add");
calcButton.addActionListener(this);
calcButton.setActionCommand("Add");
// set up a button that will add number 1 and number 2 and add a listener
JButton subButton = new JButton("Subtract");
subButton.addActionListener(this);
subButton.setActionCommand("Subtract");
// set up panel for input numbers with labels
JPanel inputNumberPanel = new JPanel();
inputNumberPanel.add(new JLabel(" Number1"));
inputNumberPanel.add(number1Field);
inputNumberPanel.add(new JLabel(" Number2"));
inputNumberPanel.add(number2Field);
// set up panel to display result
JPanel resultPanel = new JPanel();
resultPanel.add(new JLabel("Sum: "));
resultPanel.add(sumField);
// set up panel to display result
JPanel resultPanel2 = new JPanel();
resultPanel2.add(new JLabel("Subtract: "));
resultPanel2.add(subField);
// add components to content pane
getContentPane().add(inputNumberPanel, "Center");
getContentPane().add(resultPanel, "South");
getContentPane().add(resultPanel2, "East");
// set up a panel for the button and place the button in a panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(calcButton);
// set up a panel for the button and place the button in a panel
JPanel buttonPanel2 = new JPanel();
buttonPanel2.add(subButton);
// stack up these two panels in another panel
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(2, 1));
southPanel.add(resultPanel);
southPanel.add(buttonPanel);
// stack up these two panels in another panel
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(3, 3));
eastPanel.add(resultPanel2);
eastPanel.add(buttonPanel2);
getContentPane().add(southPanel, "South");
getContentPane().add(eastPanel, "East");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
show();
}
publicvoid actionPerformed(ActionEvent event)
{ String command = event.getActionCommand();
// get user input from input textfields
int num1 = Integer.parseInt(number1Field.getText().trim());
int num2 = Integer.parseInt(number2Field.getText().trim());
// compute average and display in result field
int sum;
int subtract;
if (command.equals("Add"))
{
sum = num1 + num2;
sumField.setText("" + sum);
}
elseif (command.equals("Subtract"))
{
subtract = num1 - num2;
subField.setText("" + subtract);
}
// set result in textfield resultField
}
publicstaticvoid main(String args[])
{
new TextFieldTest("Test of Text Fields");
}
}
// set up a button that will add number 1 and number 2 and add a listener JButton calcButton = new JButton("Add"); calcButton.addActionListener(this); calcButton.setActionCommand("Add");
// set up a button that will add number 1 and number 2 and add a listener JButton subButton = new JButton("Subtract"); subButton.addActionListener(this); subButton.setActionCommand("Subtract"); JPanel buttonPanel = new JPanel(); buttonPanel.add(calcButton); buttonPanel.add(subButton);
JPanel resultPanel = new JPanel(); resultLabel = new JLabel(""); resultPanel.add(new JLabel("Result: ")); resultPanel.add(resultLabel);
the problem i was having wasnt the way it worked it was the way the layout looked because it did not look to be user friendly it was kind of cluttered and i wanted to get the subtract to the bottom so its side by side with the add but i dont know how to do it
// set up a button that will add number 1 and number 2 and add a listener JButton calcButton = new JButton("Add"); calcButton.addActionListener(this); calcButton.setActionCommand("Add"); JPanel addPanel = new JPanel(); addPanel.add(calcButton);
// set up a button that will add number 1 and number 2 and add a listener JButton subButton = new JButton("Subtract"); subButton.addActionListener(this); subButton.setActionCommand("Subtract"); JPanel subtractPanel = new JPanel(); subtractPanel.add(subButton);
JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BorderLayout()); buttonPanel.add(subtractPanel,"West"); buttonPanel.add(addPanel,"East");
JPanel resultPanel = new JPanel(); resultLabel = new JLabel(""); resultPanel.add(new JLabel("Result: ")); resultPanel.add(resultLabel);
I created an addPanel and a subtractPanel and added them to a new panel with its own layout so the addPanel and subtractPanel are side by side and at the bottom just as you asked.
If this is still not it, perhaps you could be more specific?
Anyway, if you want to solve your problem, I suggest you create JPanel components to hold the desired sub components with the type layout you need, and then add the JPanels to parent panels or the getContentPane() panel in your JFrame.
You can do it!! Don't give up. There are lots of ways to do this kind of thing.
i took your code and ran it and it was the same layout as that i had orginally with the two number fields up top the subtract on top to the right and the add on the bottom in the center not subtract and add side by side on the bottom