Can someone help me with this Java program? I have created the buttons but am ha
ID: 639254 • Letter: C
Question
Can someone help me with this Java program? I have created the buttons but am having a hard time then creating actions for them. Thank you for your help.
// Declare and create a JPanel named panelSysButton. Use the default FlowLayout layout
// manager. Call addButton() to add buttons labeled "Clear", "About", and "Exit".
JPanel panelSysButton = new JPanel();
addButton(panelSysButton, "Clear");
addButton(panelSysButton, "About");
addButton(panelSysButton, "Exit");
// Write code that determines if the Exit button is the source of the event and if so,
// exit the application by calling System.exit().
???
// Write code that determines if the About button is the source of the event and if so,
// display the about dialog using JOptionPane.showMessageDialog().
???
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// Implements the GUI for a calculator.
public class ViewTest implements ActionListener {
public static void main(String[] args) {
new ViewTest().run();
}
// Default ctor. Does nothing.
public void run() {
// Create border panel
JPanel panelMain = new JPanel();
panelMain.setLayout(new BorderLayout());
//create top panel
JPanel panelTop = new JPanel();
panelTop.setLayout(new FlowLayout());
// create bottom panel
JPanel panelBot = new JPanel();
panelBot.setLayout(new BoxLayout(panelBot, BoxLayout.X_AXIS));
//panel for functions
JPanel panelFunctButton = new JPanel();
panelFunctButton.setLayout(new GridLayout(2, 2));
addButton(panelFunctButton, "x^y");
addButton(panelFunctButton, "log 10");
addButton(panelFunctButton, "log e");
addButton(panelFunctButton, "sqrt");
//panel for label
JPanel panelLabel = new JPanel();
JLabel label = new JLabel("Kalkutron-9000");
panelLabel.add(label);
panelTop.add(panelLabel);
//panel for text
JPanel panelTextField = new JPanel();
panelTextField.setLayout(new FlowLayout());
JTextField mText = new JTextField();
mText.setColumns(30);
panelTextField.add(mText);
//panel for keypad
JPanel panelKeypad = new JPanel();
panelKeypad.setLayout(new GridLayout(4, 4));
addButton(panelKeypad, "7");
addButton(panelKeypad, "8");
addButton(panelKeypad, "9");
addButton(panelKeypad, "/");
addButton(panelKeypad, "4");
addButton(panelKeypad, "5");
addButton(panelKeypad, "6");
addButton(panelKeypad, "*");
addButton(panelKeypad, "3");
addButton(panelKeypad, "2");
addButton(panelKeypad, "1");
addButton(panelKeypad, "-");
addButton(panelKeypad, "0");
addButton(panelKeypad, ".");
addButton(panelKeypad, "+/-");
addButton(panelKeypad, "+");
// panel for exit buttons
JPanel panelSysButton = new JPanel();
panelSysButton.setLayout(new FlowLayout());
addButton(panelSysButton, "Clear");
addButton(panelSysButton, "About");
addButton(panelSysButton, "Exit");
//combine buttons sys and funct
JPanel panelFunctSys = new JPanel();
panelFunctSys.setLayout(new BorderLayout());
panelFunctSys.add(panelFunctButton, BorderLayout.CENTER);
panelFunctSys.add(panelSysButton, BorderLayout.SOUTH);
//combine text and label into one panel
JPanel panelLabelAndText = new JPanel();
panelLabelAndText.setLayout(new BorderLayout());
panelLabelAndText.add(panelLabel, BorderLayout.NORTH);
panelLabelAndText.add(panelTextField, BorderLayout.CENTER);
// Add the horizontal BoxLayout panel to the BorderLayout panel NORTH region.
panelTop.add(panelLabelAndText, BorderLayout.NORTH);
// Add the vertical BoxLayout panel to the BorderLayout panel SOUTH region.
panelBot.add(panelKeypad, BorderLayout.WEST);
panelBot.add(Box.createRigidArea(new Dimension(10, 0)));
panelBot.add(panelFunctSys, BorderLayout.EAST);
panelMain.add(panelTop, BorderLayout.NORTH);
panelMain.add(Box.createVerticalGlue());
panelMain.add(panelBot, BorderLayout.SOUTH);
panelMain.add(Box.createVerticalGlue());
// Create the JFrame and add the BorderLayout panel to the frame.
JFrame frame = new JFrame("Kalkutron-9000");
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setSize(570, 225);
frame.add(panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void addButton(JPanel pPanel, String pText) {
JButton button = new JButton(pText);
button.addActionListener(this);
pPanel.add(button);
}
@Override
public void actionPerformed(ActionEvent pEvent) {
if (pEvent.getActionCommand().contains("Exit")) {
System.exit(0);
}
if (pEvent.getActionCommand().contains("About")) {
String about = "This is my calculator - Ben Spencer";
JOptionPane.showMessageDialog(new JFrame(), about);
}
}
}