Design and implement a JPanel that displays as shown. When the GUI first display
ID: 653193 • Letter: D
Question
Design and implement a JPanel that displays as shown.
When the GUI first displays, the ?Pounds to Kilograms? a JRadioButton is clicked and the GUI accepts a weight in pounds and converts it to a weight in kilograms, displaying the answer in the JTextArea as shown.
To enter the kilograms for conversion, the user must click the ?Kilograms to Pounds? JRadioButton . The conversion is done when the user presses the convert button.
The ?C? JButton clears the JTextArea between calculations. Note to convert pounds to kilograms multiply by 2.24 and to convert kilograms to pounds divide by 2.24.
Use a JFrame class to display your JPanel class.
Explanation / Answer
package com.online.gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import com.online.shopping.CheckOut;
import com.online.shopping.ReadingMatter;
import com.online.shopping.ShoppingCart;
public class GUIFrame extends JFrame implements ActionListener {
JComboBox comboSubType;
ShoppingCart cart = new ShoppingCart();
GUIFrame() throws FileNotFoundException {
super("Online Shopping");
GridLayout layout = new GridLayout(4, 2);
setLayout(layout);
List<String> list = ShoppingCart.types;
String[] types = ShoppingCart.types.toArray(new String[list.size()]);
JLabel label1 = new JLabel("Select the type:");
JComboBox comboTypes = new JComboBox(types);
add(label1);
add(comboTypes);
comboSubType = new JComboBox();
JLabel label2 = new JLabel("Select the name from list:");
add(label2);
add(comboSubType);
comboTypes.addActionListener(this);
JButton addCart = new JButton("Add to Cart");
addCart.setActionCommand("Add to cart");
addCart.addActionListener(this);
JButton exitCart = new JButton("Exit");
exitCart.setActionCommand("Exit");
exitCart.addActionListener(this);
add(addCart);
add(exitCart);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Add to cart")) {
if (comboSubType.getSelectedItem() == null)
return;
System.out.println(comboSubType.getSelectedItem());
String option = (String) comboSubType.getSelectedItem();
int id = ShoppingCart.getId(option);
cart.addToCart(id + 1);
JOptionPane.showMessageDialog(null, "Added to cart successfully");
} else if (e.getActionCommand().equals("Exit")) {
JOptionPane.showMessageDialog(null, cart.toString() + " Pay rs:"
+ new DecimalFormat("#0.00").format(cart.getTotalPrice()));
try {
CheckOut.updateSales(cart);
} catch (IOException e1) {
e1.printStackTrace();
}
setVisible(false);
} else {
JComboBox cb = (JComboBox) e.getSource();
String option = (String) cb.getSelectedItem();
comboSubType.removeAllItems();
List<ReadingMatter> list = ShoppingCart.getTypes(option);
for (ReadingMatter rd : list)
comboSubType.addItem(rd.getTitle());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.