Simple Swing GUI

Assignment

Assignment Part A

 GENERAL INSTRUCTIONS

Write a simple swing form that has the following features and looks (more or less) like the following:

It has three buttons, each of which does one thing; Add, Subtract, Concatenate.

Here are examples of each function

Add

Subtract

Concatenate (example 1)

Concatenate (example 2)

Below are some useful code fragments:

// *******************************************************

getContentPane().setLayout(null);

setTitle(“Assignment PartA”);

// *******************************************************

String text = text1.getText();

try

{

val1 = Double.parseDouble(text);

}

catch (NumberFormatException e)

{

val1=0;

rc=false;

JOptionPane.showMessageDialog(null, “Enter a valid double precision number please”,

“<< First Number Error >>”,

JOptionPane.ERROR_MESSAGE);

}

// *******************************************************

public class bAddAction implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

///// —————–

////// Lines deleted here that parsed the JTextFields into val1 and val2

///// —————–

if (rc)

{displayText4.setText(String.format(“%12.4f”,(val1 + val2)));}

else

{displayText4.setText(“0”);}

}

}

// *******************************************************

intposX =260;

JButtonbuttonAdd = new JButton(“Add”);

buttonAdd.setLocation(posX,30);

buttonAdd.setSize(110,20);

buttonAdd.addActionListener(new bAddAction());

getContentPane().add(buttonAdd);

// *******************************************************

Solution

importjavax.swing.*;

importjava.awt.GridLayout;

importjava.awt.event.*;

public class PartA {

// This class includes the initialization of GUI components

// (labels, text fields, buttons).

public static class MainPane extends JPanel {

private final JLabel label1;

private final JLabel label2;

private final JLabel label3;

private final JTextField field1;

private final JTextField field2;

private final JLabelresultLabel;  // the label to display result

private final JButtonaddButton;

private final JButtonsubButton;

private final JButtonconButton;

publicMainPane() {

// create the components

this.label1 = new JLabel(“First Number: “);

this.label2 = new JLabel(“Second Number: “);

this.label3 = new JLabel(“==============”);

this.field1 = new JTextField(“0”);

this.field2 = new JTextField(“0”);

this.resultLabel = new JLabel(“0”);

this.addButton = new JButton(“Add”);

this.subButton = new JButton(“Subtract”);

this.conButton = new JButton(“Concatenate”);

// create the grid layout to organize the components

this.setLayout(new GridLayout(4, 3)); // 4 rows 3 columns

this.add(this.label1);

this.add(this.field1);

this.add(this.addButton);

this.add(this.label2);

this.add(this.field2);

this.add(this.subButton);

this.add(new JLabel());

this.add(this.label3);

this.add(this.conButton);

this.add(new JLabel());

this.add(this.resultLabel);

this.add(new JLabel());

// add action listener to the buttons

finalButtonListener listener = new ButtonListener(this.field1, this.field2, this.resultLabel);

this.addButton.addActionListener(listener);

this.subButton.addActionListener(listener);

this.conButton.addActionListener(listener);

}

}

public static class ButtonListener implements ActionListener {

private final JTextField field1;

private final JTextField field2;

private final JLabelresultLabel; // the label to display result

publicButtonListener(final JTextField field1, final JTextField field2, final JLabelresultLabel) {

this.field1 = field1;

this.field2 = field2;

this.resultLabel = resultLabel;

}

@Override

public void actionPerformed(final ActionEventae) {

finalJButton button = JButton.class.cast(ae.getSource());

final String command = button.getText();

// get the values from input fields

final String text1 = this.field1.getText().trim();

final String text2 = this.field2.getText().trim();

double value1, value2, result = 0;

try {

value1 = Double.parseDouble(text1);

value2 = Double.parseDouble(text2);

} catch (final NumberFormatExceptionnfe) {

// the input field does not have numbers

JOptionPane.showMessageDialog(null, “Enter valid double precision numbers please”);

return;

}

// execute the command from the button

if (“Add”.equals(command)) {

result = value1 + value2;

this.resultLabel.setText(“” + result);

return;

}

if (“Subtract”.equals(command)) {

result = value1 – value2;

this.resultLabel.setText(“” + result);

return;

}

if (“Concatenate”.equals(command)) {

this.resultLabel.setText(text1 + text2);

return;

}

}

}

public static void main(String[] args) {

// create the JFrame for the panel

finalJFrame frame = new JFrame(“Assignment PartA”);

frame.setContentPane(new MainPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

}