Well my English is not that good but i will try to clarify myself. I am trying to build Spaza shop system and it cant display the image on the button menu1, menu2, and menu3 as well the buttons for proceed and cancel. Also, i want the button for start menu when it is clicked to display list of other items to be sold in the spaza shop. I am new in java, so some of mistakes i cant be able to correct them. Here is mycode:
package wing_cafe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;
import java.lang.reflect.InvocationTargetException;
public class wing_cafe extends JFrame implements ActionListener
{
private GridBagLayout layout;
private GridBagConstraints cons;
private Container container;
Thread th=new Thread();
public static JList foodList;
public static DefaultListModel appendList =new DefaultListModel();
public static JButton other;
public static double change=0.0;
JButton panel1= new JButton();
JPanel panel2= new JPanel();
JPanel menu= new JPanel();
JPanel payment= new JPanel();
JButton done= new JButton("DONE");
JButton exit= new JButton("EXIT");
JLabel order= new JLabel("Ordered");
public static JTextArea orderText= new JTextArea(2,8);
JLabel amount= new JLabel("Enter amount to pay");
public static JTextField amountText= new JTextField(8);
JLabel orderTotal= new JLabel("Ordered Total");
public static JTextField orderTotalText= new JTextField(8);
JLabel changeL= new JLabel("Change");
public static JTextField changeText= new JTextField(8);
JButton ok= new JButton("proceed");
JButton cancel= new JButton("Cancel");
public static JTextField field1,timeField,timer,calc;
JLabel label=new JLabel();
JPanel a1= new JPanel();
JPanel b1= new JPanel();
JPanel c1= new JPanel();
JPanel d1= new JPanel();
JPanel e1= new JPanel();
JMenuBar mb=new JMenuBar();
JMenu print=new JMenu("Reciept");
JMenu help=new JMenu("Help!");
JMenu manage=new JMenu("Database management");
JMenuItem receipt=new JMenuItem("print reciept");
JMenuItem aboutItem=new JMenuItem("About the program");
JMenuItem usage=new JMenuItem("Help Message");
JMenuItem mng=new JMenuItem("Management");
CafeCalculation calculate=new CafeCalculation();
selectMenu fmenu=new selectMenu();
Progressbar bar=new Progressbar();
public wing_cafe()
{
setTitle("Wings Cafe System");
cons= new GridBagConstraints();
container = getContentPane();
layout = new GridBagLayout();
container.setLayout( layout );
container.setBackground(Color.BLACK);
setJMenuBar(mb);
manage.setIcon(new ImageIcon("images/loc.png"));
help.setIcon(new ImageIcon("images/help.png"));
print.setIcon(new ImageIcon("images/printer.png"));
aboutItem.setIcon(new ImageIcon("images/helpA.png"));
usage.setIcon(new ImageIcon("images/helpC.png"));
mb.add(help);
mb.add(print);
mb.add(manage);
help.add(aboutItem);
help.add(usage);
manage.add(mng);
print.add(receipt);
receipt.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
try{
if(changeText.getText().equals("")||amountText.getText().equals("")||orderTotalText.getText().equals("")){
throw new Throwable();
}
Progressbar.answer( );
}
catch(Throwable b){
JOptionPane.showMessageDialog(null,"Please fill up all transactions to complete","Notice!",JOptionPane.ERROR_MESSAGE);
}
}
}
);
manage.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
password();
}
}
);
aboutItem.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog( wing_cafe.this,
"This is a complete Project developed by FELAPHE SEQHIBI, LEFU NTOA and TSOEU REMAKETSE ",
"About", JOptionPane.PLAIN_MESSAGE );
}
}
);
usage.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
hlpmsg();
}
}
);
cons.weightx=1;
cons.weighty=1;
cons.fill=GridBagConstraints.BOTH;
panel1.setBackground(Color.black);
panel1.setIcon(new ImageIcon("images/menu.png"));
addComponent(panel1,0,0 ,1,1);
cons.weightx=1;
cons.weighty=1;
cons.fill=GridBagConstraints.BOTH;
panel2.setBackground(Color.green);
addComponent(panel2,0,1,1,1);
cons.weightx=2;
cons.weighty=2;
cons.fill=GridBagConstraints.BOTH;
JButton startMenu= new JButton("startMenu");
JButton menu1= new JButton("Drink= M8.00",new ImageIcon("images/drink.jpg"));
JButton menu2= new JButton("Pizza= M20.00",new ImageIcon("images/pizza.jpg"));
JButton menu3= new JButton("Burger= M16.00",new ImageIcon("images/burger.jpg"));
menu1.setBackground(Color.black);
menu1.setForeground(Color.white);
menu2.setBackground(Color.black);
menu2.setForeground(Color.white);
menu3.setBackground(Color.black);
menu3.setForeground(Color.white);
startMenu.setBackground(Color.black);
startMenu.setForeground(Color.white);
startMenu.addActionListener(this);
menu1.addActionListener(this);
menu2.addActionListener(this);
menu3.addActionListener(this);
menu.setLayout(new GridLayout(2,2));
menu.add(startMenu);
menu.add(menu1);
menu.add(menu2);
menu.add(menu3);
addComponent(menu,1,0 ,1,1);
cons.weightx=2;
cons.weighty=2;
cons.fill=GridBagConstraints.BOTH;
payment.setLayout(new GridLayout(5,1));
orderText.setEditable(false);
orderTotalText.setEditable(false);
changeText.setEditable(false);
order.setForeground(Color.white);
orderTotal.setForeground(Color.white);
amount.setForeground(Color.white);
changeL.setForeground(Color.white);
c1.setLayout(new FlowLayout());
c1.add(order);
c1.add(orderText);
c1.setBackground(Color.black);
a1.setLayout(new FlowLayout());
a1.add(orderTotal);
a1.add(orderTotalText);
a1.setBackground(Color.black);
b1.setLayout(new FlowLayout());
b1.add(amount);
b1.add(amountText);
b1.setBackground(Color.black);
d1.setLayout(new FlowLayout());
d1.add(changeL);
d1.add(changeText);
d1.setBackground(Color.black);
e1.setLayout(new GridLayout(1,2));
d1.setBackground(Color.black);
ok.setBackground(Color.WHITE);
cancel.setBackground(Color.WHITE);
ok.addActionListener(this);
cancel.addActionListener(this);
e1.add(ok);
e1.add(cancel);
payment.add(c1);
payment.add(a1);
payment.add(b1);
payment.add(d1);
payment.add(e1);
addComponent(payment,1,1 ,1,1);
cons.weightx=0.1;
cons.weighty=0.1;
cons.fill=GridBagConstraints.CENTER;
done.setBackground(Color.WHITE);
addComponent(done,2,0 ,1,1);
done.addActionListener(this);
cons.weightx=0.1;
cons.weighty=0.1;
cons.fill=GridBagConstraints.CENTER;
exit.setBackground(Color.WHITE);
addComponent(exit,2,1 ,1,1);
exit.addActionListener(this);
}
public void SwingAnimator(){
try{
for(int i = 1; i <= 5; i++){
ImageIcon images = new ImageIcon("images/img" + i + ".gif");
label.setIcon(images);
th.sleep(6000);
}
}
catch(InterruptedException e){}
}
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Burger= M16.00"))
{
try
{
calculate.burger();
}
catch (Exception ex)
{
}
}
else if (actionCommand.equals("Drink= M8.00"))
{
try
{
calculate.Drinks();
}
catch (Exception ex)
{
}
}
else if (actionCommand.equals("Pizza= M20.00"))
{
try
{
calculate.Pizza();
}
catch (Exception ex)
{
}
}
else if (actionCommand.equals("Chips= M10.00"))
{
try
{
calculate.Chips();
}
catch (Exception ex)
{
}
}
else if (actionCommand.equals("proceed"))
{
try
{ Double c=Double.parseDouble(amountText.getText());
Double a=Double.parseDouble(orderTotalText.getText());
change=c-a;
if(a>c){
throw new Throwable();
}
changeText.setText(""+change+"0");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"Please fill up the amount in amount column","Notice!",JOptionPane.ERROR_MESSAGE);
}
catch(Throwable a){
JOptionPane.showMessageDialog(null,"SORRY, you don't have enough cash! insert more cash to continue ","Notice!",JOptionPane.ERROR_MESSAGE);
}
}
else if (actionCommand.equals("Cancel"))
{
try
{
changeText.setText("");
amountText.setText("");
orderTotalText.setText("");
calculate.totalAmount=0.0;
calculate.amount=0.0;
calculate.totalAmnt=0.0;
appendList.clear();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"Error","Notice!",JOptionPane.ERROR_MESSAGE);
}
}
else if(actionCommand.equals("."))
{
try{
fmenu.setVisible(true);
}
catch (Exception ex){
JOptionPane.showMessageDialog(null,"Error","Notice!",JOptionPane.ERROR_MESSAGE);
}
}
else if (actionCommand.equals("DONE"))
{
try{
if(changeText.getText().equals("")||amountText.getText().equals("")||orderTotalText.getText().equals("")){
throw new Throwable();
}
bar.setVisible(true);
bar.panel.setVisible(true);
bar.panel.setSize(250,90);
bar.panel.setLayout(new FlowLayout());
bar.panel.add(bar.pbr);
bar.setSize(250,90);
bar.add(bar.panel,BorderLayout.SOUTH);
dispose();
Thread stepper=new control(bar.pbr);
stepper.start();
}
catch(Throwable b){
JOptionPane.showMessageDialog(null,"PRESS OK TO CONTINUE!!! try to fillup all Transactions","Notice!",JOptionPane.ERROR_MESSAGE);
}
}
else if(actionCommand.equals("EXIT"))
{
System.exit(0);
}
else
{
System.out.println("Error in button interface.");
}
}
public static void main(String[] args)
{
wing_cafe frame= new wing_cafe();
frame.setSize(800, 750);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addComponent(Component component, int row, int col, int width, int height)
{
cons.gridx=col;
cons.gridy= row;
cons.gridheight= height;
cons.gridwidth= width;
layout.setConstraints(component, cons);
container.add( component );
}
public void hlpmsg( ){
String outPut ="HOW TO MAKE AN ORDER IN KAPITSENG RESTURANT\n\n?"+
"Please! CLICK on start menu and choose the meal you want to oder!\n"+
" quantity frame then insert quantity of things you buy, meaning number of them.\n"+
" ENTER 1 if you don't want to change the quantity,\n" +
" \nchange the quantity number then ok if you want to change it.0 to EXIT\n"+
" after selecting menu, the total will be on total order field,\n"+
" insert the money you want to pay then your change will be \n"+
"calculated for you. print reciept then Proceed to send to database.";
JTextArea content = new JTextArea();
content.setText(outPut );
content.setBackground(Color.GREEN);
content.setForeground(Color.WHITE);
content.setEditable(false);
JOptionPane.showMessageDialog( null, content,
"How to use", JOptionPane.INFORMATION_MESSAGE );
}
private class control extends Thread
{
int bardelay = 100;
JProgressBar pbr;
JFrame panel;
public control(JProgressBar defaulty)
{
pbr=defaulty;
}
public void run()
{
int min = pbr.getMinimum();
int max = pbr.getMaximum();
Runnable runner = new Runnable()
{
public void run()
{
int value = pbr.getValue();
pbr.setValue(value+1);
}
};
for(int i=min; i {
try
{
EventQueue.invokeAndWait(runner);
Thread.sleep(bardelay);
}
catch (InterruptedException ignoredException)
{}
catch (InvocationTargetException ignoredException)
{}
if(i==max-1)
{
bar.setVisible(false);
JOptionPane.showMessageDialog(panel,"COMPLETE, THEN PRESS OK TO VIEW YOUR THE RECEIPT");
bar.answer();
setVisible(true);
}
}
}
}
public static void password() {
JTextField userField = new JTextField( );
JPasswordField passField = new JPasswordField( );
String message = "Please Enter User Name and your Password.";
String name="UserName:";
String pswd="Password:";
JOptionPane.showOptionDialog(null,
new Object[] { message, name, userField,pswd, passField },
"Login", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null);
JOptionPane.showMessageDialog(null, "thier is no Database found!.",
"DataBaseManagement", JOptionPane.WARNING_MESSAGE);
}
}
package wing_cafe;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class selectMenu extends JDialog implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("proceed"))
{
try
{
if(recalcTotal()!=0){
wing_cafe.appendList.addElement("Full Meal");
CafeCalculation.totalBalance( recalcTotal());
dispose();
}
else
throw new Throwable();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"Error","Notice!",JOptionPane.ERROR_MESSAGE);
}
catch(Throwable b){
JOptionPane.showMessageDialog(null,"Please choose Menu and press ok OR cancel to exit!","Notice!",JOptionPane.ERROR_MESSAGE);
}
}
else
if (actionCommand.equals("Cancel"))
{
try
{
dispose();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"Error","Notice!",JOptionPane.ERROR_MESSAGE);
}
}
}
protected JList m_list;
protected JLabel m_total;
JPanel buttons=new JPanel();
JButton jb1 = new JButton("Cancel");
JButton jb2 = new JButton("OK!");
public selectMenu() {
super();
setTitle("Food List [Check boxes]");
setModal(true);
setSize(280, 300);
getContentPane().setLayout(new FlowLayout());
FoodList[] options = {
new FoodList("Supper (Rice and Chicken)", 40),
new FoodList("Lunch (Papa and Chicken/stew)", 30),
new FoodList("Burger and Coke 500ml", 60),
new FoodList("Chips only", 10),
new FoodList("Breakfast(Burns and Coffee)", 20),
new FoodList("Other meals (chips and Chicken)", 50),
new FoodList("Other meals (Papa and Linaoa)", 15),
new FoodList("Other meals (Bread and Drink/cofee )", 25),
};
m_list = new JList(options);
CheckListCellRenderer renderer = new CheckListCellRenderer();
m_list.setCellRenderer(renderer);
m_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
CheckListener lst = new CheckListener(this);
m_list.addMouseListener(lst);
m_list.addKeyListener(lst);
JScrollPane ps = new JScrollPane();
ps.getViewport().add(m_list);
m_total = new JLabel("Amount To Pay: M0.00");
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(ps, BorderLayout.CENTER);
p.add(m_total, BorderLayout.SOUTH);
p.setBorder(new TitledBorder(new EtchedBorder(),
"Please select Food you want to order:") );
getContentPane().add(p);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dispose();
}
};
add(buttons);
buttons.add(jb1);
buttons.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
addWindowListener(wndCloser);
setVisible(false);
recalcTotal();
}
public Double recalcTotal() {
ListModel model = m_list.getModel();
Double total = 0.00;
for (int k=0; k<model.getsize();> FoodList data = (FoodList)model.getElementAt(k);
if (data.isSelected())
total += data.getSize();
}
m_total.setText("Amount To Pay: M"+total+"0");
return total;
}
}
class CheckListCellRenderer extends JCheckBox
implements ListCellRenderer
{
protected static Border m_noFocusBorder =
new EmptyBorder(1, 1, 1, 1);
public CheckListCellRenderer() {
super();
setOpaque(true);
setBorder(m_noFocusBorder);
}
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus)
{
setText(value.toString());
setBackground(isSelected ? list.getSelectionBackground() :
list.getBackground());
setForeground(isSelected ? list.getSelectionForeground() :
list.getForeground());
FoodList data = (FoodList)value;
setSelected(data.isSelected());
setFont(list.getFont());
setBorder((cellHasFocus) ?
UIManager.getBorder("List.focusCellHighlightBorder")
: m_noFocusBorder);
return this;
}
}
class CheckListener implements MouseListener, KeyListener
{
protected selectMenu m_parent;
protected JList m_list;
public CheckListener(selectMenu parent) {
m_parent = parent;
m_list = parent.m_list;
}
public void mouseClicked(MouseEvent e) {
if (e.getX() < 20)
doCheck();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == ' ')
doCheck();
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
protected void doCheck() {
int index = m_list.getSelectedIndex();
if (index < 0)
return;
FoodList data = (FoodList)m_list.getModel().
getElementAt(index);
data.invertSelected();
m_list.repaint();
m_parent.recalcTotal();
}
}
class FoodList
{
protected String m_name;
protected int m_size;
protected boolean m_selected;
public FoodList(String name, int size) {
m_name = name;
m_size = size;
m_selected = false;
}
public String getName() { return m_name; }
public int getSize() { return m_size; }
public void setSelected(boolean selected) {
m_selected = selected;
}
public void invertSelected() { m_selected = !m_selected; }
public boolean isSelected() { return m_selected; }
public String toString() { return m_name+" =M"+m_size+".00"; }
}
package wing_cafe;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.lang.reflect.InvocationTargetException;
class Progressbar extends JDialog {
private static String tag;
public static JPanel panel = new JPanel();
public static DefaultBoundedRangeModel model =new DefaultBoundedRangeModel(10,1,0,50);
public static JProgressBar pbr=new JProgressBar(model);
public Progressbar(){
setLayout(new FlowLayout());
setTitle("Printing the receipt");
pbr.setBorderPainted(true);
pbr.setStringPainted(true);
pbr.setFont(new Font("algerian",Font.BOLD,20));
pbr.setForeground(Color.GREEN);
setVisible(false);
setSize(300,190);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
private static String setTag ()
{
Random gen = new Random ();
tag = "";
for (int i = 0; i < 4; i++)
{
int code = gen.nextInt (26)+ 65;
tag += (char) code;
}
return tag;
}
public static void answer( ){
String outPut = "\tTHIS IS YOUR ORDER RECEIPT\n"+
":::::::::::::::::::::::::::::::::::::"+
"::::::::::::::::::::::::::::::::::::::"+
"::::::::::::::::::::::::::::::::::::::::::::\n";
JTextArea content = new JTextArea();
content.setText(outPut );
content.setBackground(Color.GREEN);
content.setForeground(Color.WHITE);
content.setEditable(false);
JOptionPane.showMessageDialog( null, content,
"Order Slip", JOptionPane.PLAIN_MESSAGE );
}
}
package wing_cafe;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CafeCalculation
{
public Double amount;
Double tax=0.14;
public static double totalAmnt = 0.0;
public Double totalAmount = 0.0;
public CafeCalculation() {
super();
}
public void burger(){
int quantity=0;
quantity=recievInt(quantity);
if(quantity>0){
amount=16.00;
amount=amount*quantity;
totalAmount+=amount;
wing_cafe.appendList.addElement("Burger x"+"["+quantity+"]");
totalBalance( totalAmount);
totalAmount=0.0;
}
}
public void Drinks(){
int quantity=0;
quantity=recievInt(quantity);
if(quantity>0){
amount=8.00;
amount=amount*quantity;
totalAmount+=amount;
wing_cafe.appendList.addElement("Drink x"+"["+quantity+"]");
totalBalance( totalAmount);
totalAmount=0.0;
}
}
public void Chips(){
int quantity=0;
quantity=recievInt(quantity);
if(amount>0){
amount=10.00;
amount=amount*quantity;
totalAmount+=amount;
wing_cafe.appendList.addElement("Chips x"+"["+quantity+"]");
totalBalance( totalAmount);
totalAmount=0.0;
}
}
public void Pizza(){
int quantity=0;
quantity=recievInt(quantity);
if(quantity>0){
amount=20.00;
amount=amount*quantity;
totalAmount+=amount;
wing_cafe.appendList.addElement("Pizza x"+"["+quantity+"]");
totalBalance( totalAmount);
totalAmount=0.0;
}
}
public static int recievInt(int Quantity){
try {
Quantity=Integer.parseInt
(JOptionPane.showInputDialog("Enter Quantity!"));
if(Quantity<=0){
throw new Throwable();
}
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Quantity are Integers Please (EXIT)", "Notice",JOptionPane.ERROR_MESSAGE);
}
catch(Throwable a){
JOptionPane.showMessageDialog(null, "Quanty is never 0 please (EXIT)", "Notice",JOptionPane.ERROR_MESSAGE);
}
return Quantity;
}
public static void totalBalance( Double value){
totalAmnt += value;
wing_cafe.orderTotalText.setText(""+totalAmnt+"0");
}
}