I am working on a project for my java class. I would like some suggestions leading me in the right direction to fixing my problems with the program below. Please do not come right out and tell me the answers, I want to figure it out but need someone to lead me in the right direction.
Here are my requirements, that will help explain what I am doing / trying to do.
1.
Perform at least one meaningful calculation based on values entered by the user.
2.
Use at least two different layout managers.
3.
Provide an attractive blend of colors and fonts.
4.
Use several (three or more) different types of Swing components.
5.
Have inner class listeners for events fired by input components.
6.
Use the Graphics class to draw on one or more components or containers.
7.
Have a pop-up "About" frame that shows your name. This frame must be launched from the main application frame.
Here is what I have. Somewhere something isn't right and I am not sure why or where.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
public class Project01b implements ActionListener {
JFrame frame;
JPanel panel;
JList choice;
JTextField quantity;
JCheckBox league;
JTextArea message;
JFrame frame2;
JButton about;
String[] items = {"Student Bowler", "Adult Bowler", "Senior Bowler"};
double[] prices = {1.50, 2.50, 2.00};
public static void main(String[] args) {
Project01b me = new Project01b();
me.go();
}
public void go() {
frame = new JFrame("Bowling Prices");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220, 220);
frame.setResizable(false);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel("Bowler Type"));
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
choice = new JList(items);
choice.setSelectedIndex(0);
choice.addListSelectionListener(new SelectionListener());
panel.add(choice, BorderLayout.WEST);
frame.getContentPane().add(choice);
frame.getContentPane().add(new JLabel("# of games"));
panel.add(choice, BorderLayout.CENTER);
quantity = new JTextField(4);
quantity.addActionListener(new QuantityListener());
panel.add(choice, BorderLayout.EAST);
frame.getContentPane().add(quantity);
league = new JCheckBox("League Bowler");
league.addItemListener(new LeagueListener());
frame.getContentPane().add(league);
message = new JTextArea(3, 15);
message.setBackground(Color.black);
message.setForeground(Color.yellow);
message.setEditable(false);
frame.getContentPane().add(message);
frame.setVisible(true);
frame2 = new JFrame("Launch another frame");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(300, 200);
about = new JButton("About");
about.addActionListener(new AboutListener());
frame2.getContentPane().add(about);
frame2.setVisible(true);
}
class SelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
if (quantity.getText().length() > 0) {
showOrderSummary();
}
else {
message.setText("");
message.append("\n" + " Please enter a quantity");
}
}
}
class QuantityListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
showOrderSummary();
}
}
class LeagueListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (quantity.getText().length() > 0) {
showOrderSummary();
}
else {
message.setText("");
message.append("\n" + " Please enter a quantity");
}
}
}
private void showOrderSummary() {
message.setText("");
try {
int qty = Integer.parseInt(quantity.getText().trim());
if (qty > 0) {
double price = prices[choice.getSelectedIndex()];
double amount = qty * price;
if (league.isSelected()) {
amount *= 0.90;
}
NumberFormat nf = NumberFormat.getCurrencyInstance();
String amountAsString = nf.format(amount);
message.append(qty + " " + (String)choice.getSelectedValue() + "(s)");
if (league.isSelected()) {
message.append("\n" + "With league bowler discount");
}
else {
message.append("\n" + "No discount");
}
message.append("\n" + "Total amount due: " + amountAsString);
}
else {
message.append("\n" + " Quantity must be positive");
}
}
catch (NumberFormatException err) {
message.append("\n" + " Quantity must be numeric");
}
public class AboutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame popUp = new JFrame("About frame");
popUp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popUp.setSize(250,200);
MyPanel panel = new MyPanel();
popUp.getContentPane().add(panel);
popUp.setVisible(true);
}
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
int width = this.getWidth();
int height = this.getHeight();
Image bowling = new ImageIcon("Bowling.jpg").getImage();
g.drawImage(bowling,0,0,this);
g.setFont(new Font("SansSerif", Font.BOLD, 12));
g.setColor(Color.green);
g.drawString("Go Bowling", 160, 165);g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.red);
g.drawString("Jeff Burel", width/4, height/2);
}
public void actionPerformed(ActionEvent e) {
((JButton)e.getSource()).setVisible(false);
}
}
}
}