package ratsac2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class RatSAC2 extends JFrame {
// Create text fields for ItemList, Item Name, Quantity and Item description
// private JComboBox jtfItemList = new JComboBox();
private JTextField jtfItemName = new JTextField();
private JTextField jtfQuantity = new JTextField();
private JTextField jtfDescription = new JTextField();
JComboBox ItemList; // Adds the combo box
String choice;
// Set the fonts to be used.
Font font1 = new Font("DialogInput", Font.BOLD + Font.ITALIC, 15);
Font font2 = new Font("Monospaced",Font.ITALIC,15);
Color newCol = new Color(120,100,100);
//Create the buttons.
private JButton jbtCancel = new JButton("Cancel");
private JButton jbtViewCart = new JButton ("View Cart");
private JButton jbtAddToCart = new JButton("Add to Cart");
public RatSAC2 () {
// Panel p1 to hold labels and text fields.
JPanel p1 = new JPanel();
p1.setLayout(null);
p1.add(new JLabel("ItemList"));
// Set up the combo box:
String [] Item = {"Bath Mat","Mirror","Olive Scented Candle","Toilet Paper","Pet Rats","Vase","Potato Sack","Inflatable Beer Pong","LED Fairy Net","Toothbrush"}; // Entries must be of type String! (These are the options)
ItemList = new JComboBox(Item);
ItemList.setSelectedIndex(0);
ItemList.setBackground(Color.white); // Sets background and foreground colors
ItemList.setForeground(Color.black);
// p1.add(jtfItemList);
// **** Add the combo box to panel p1, then add a listener to get the selected item.
p1.add(ItemList);
ItemList.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
String str = (String)ItemList.getSelectedItem();
if ("Mirror".equals(str)){
jtfDescription.setText("uuu");
}
else if ("Vase".equals(str)){
jtfDescription.setText("kkk");
}
else {
jtfDescription.setText("ooo");
}
}
// choice = Integer.parseInt(str);
});
// Set absolute coordinates for fields and labels; X,Y,W,H
jtfItemName.setBounds(20,20,50,50);
jtfQuantity.setBounds(40,40,50,50);
jtfDescription.setBounds(50,50,50,50);
// add the labels and fields to panel 1
p1.add(jtfItemName);
p1.add(new JLabel("Item Name"));
p1.add(jtfQuantity );
p1.add(new JLabel("Quantity"));
p1.add(new JLabel("Description"));
p1.add(jtfDescription);
// *********** Set up the second frame ************
final JFrame f2 = new JFrame(); //See note below re local variable f2
f2.getContentPane().setBackground(Color.white);
f2.setSize(500,800);
f2.setLocation(200,150);
f2.setVisible(false);
// Check when NewWindow button is clicked.
jbtCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Local variable is accessed from within inner class. Needs to be declared final (as above).
f2.setVisible(true);
}
});
// *************************************************
// Panel p2 to hold the buttons.
JPanel p2 = new JPanel();
p2.setLayout(null);
p2.add(jbtCancel);
p2.add(jbtViewCart);
p2.add(jbtAddToCart);
// Add the panels to the frame.
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);
}
public static void main(String[] args) {
RatSAC2 f1 = new RatSAC2();
f1.pack();
f1.setSize(800,500);
f1.setTitle("Shopping Cart");
f1.setLocationRelativeTo(null); // Center the frame.
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
f1.setResizable(false);
}}