งาน

1. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Final_151_1 extends JFrame implements ActionListener, MouseListener, KeyListener {
private final static int SHOW = 16;
private int countIce=0;
private int totalPrice=0;
private JLabel lblShow=new JLabel("Select ice scream.",SwingConstants.CENTER);
private JLabel showPic[]=new JLabel[SHOW];
private JLabel showPrice[]=new JLabel[SHOW];
private JPanel panelShow[]=new JPanel[SHOW];
private JPanel panelPic=new JPanel();
private String iceName[]={"ʵÃÍàºÍÃÕè","ªçͤâ¡áÅçµ","¤ÃÕÁªÕÊ","ÁÑÅàºÍÃÕè","᤹µÒÅÙ»","ÁйÒÇ","¡ÅéÇÂËÍÁ",
"ÃÒÊàºÍÃÕè","ÁÐÁèǧ","¡Ð·ÔÊ´","áÍ»à»ÔéÅ","Áͤ¤èÒ","ÇÒ¹ÔÅÅÒ","ÅÔ鹨Õè","ÊéÁ","¹éÍÂ˹èÒ"};
private TextArea txtArea=new TextArea("",7,25,0);
private JTextField txtTotal=new JTextField(5);
private JTextField txtGetMoney=new JTextField(5);
private JTextField txtOutMoney=new JTextField(5);
private JLabel labelTotal=new JLabel("ÃÒ¤Ò");
private JLabel labelGetMoney=new JLabel("ÃѺà§Ô¹");
private JLabel labelOutMoney=new JLabel("·Í¹à§Ô¹");
private JPanel panelMoney=new JPanel();
private JPanel panelOne=new JPanel();
private JPanel panelTwo=new JPanel();
private JPanel panelThree=new JPanel();
private JPanel panel123=new JPanel();
private JPanel panelIceScream=new JPanel();

public Final_151_1(){
super("IceScream Shop");
panelIceScream.setLayout(new BorderLayout());
lblShow.setFont(new Font(lblShow.getFont().toString(),Font.BOLD,20));
panelIceScream.add(lblShow,BorderLayout.NORTH);
panelIceScream.setBackground(Color.white);
panelPic.setLayout(new GridLayout(2,8));
panelPic.setBackground(Color.white);
for(int i=0; i panelShow[i] = new JPanel();
panelShow[i].setLayout(new BorderLayout());
panelShow[i].add(new JLabel(new ImageIcon("ice"+(i+1)+".jpg")),BorderLayout.NORTH);
panelShow[i].add(new JLabel(iceName[i],SwingConstants.CENTER),BorderLayout.CENTER);
panelShow[i].add(new JLabel(" "),BorderLayout.SOUTH);
panelShow[i].setBackground(Color.white);
panelShow[i].addMouseListener(this); //???????????
panelPic.add(panelShow[i]);
}
panelIceScream.add(panelPic,BorderLayout.SOUTH);

panelMoney.setLayout(new BorderLayout());
panelOne.setLayout(new FlowLayout(FlowLayout.RIGHT));
panelTwo.setLayout(new FlowLayout(FlowLayout.RIGHT));
panelThree.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel123.setLayout(new GridLayout(3,1));
panelOne.add(labelTotal);
panelOne.add(txtTotal);
panelTwo.add(labelGetMoney);
panelTwo.add(txtGetMoney);
panelThree.add(labelOutMoney);
panelThree.add(txtOutMoney);
panel123.add(panelOne);
panel123.add(panelTwo);
panel123.add(panelThree);
txtArea.setEditable(false);
panelMoney.add(txtArea,BorderLayout.NORTH);
panelMoney.add(panel123,BorderLayout.CENTER);

Container container = getContentPane();
container.setLayout(new BorderLayout());
container.setBackground(Color.blue);
container.add(panelIceScream,BorderLayout.WEST);
container.add(panelMoney,BorderLayout.EAST);
txtGetMoney.addKeyListener(this); //????????????
setSize(780, 250);
setVisible(true);

}

public static void main(String[] args) {
Final_151_1 app = new Final_151_1();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){}
public void mouseClicked(MouseEvent e){
for(int i=0; i if(e.getSource() == panelShow[i]){
txtArea.append(++countIce+". "+iceName[i].toString()+"n");
totalPrice+=20;
txtTotal.setText(totalPrice+"");
}
}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){
if ((e.getSource()==txtGetMoney)&&(e.getKeyCode()==KeyEvent.VK_ENTER)){
if (Integer.parseInt(txtTotal.getText())<=Integer.parseInt(txtGetMoney.getText())){
txtOutMoney.setText(Integer.parseInt(txtGetMoney.getText())-Integer.parseInt(txtTotal.getText())+"");
txtGetMoney.setForeground(Color.BLACK);
}
else if (Integer.parseInt(txtTotal.getText())>Integer.parseInt(txtGetMoney.getText())){
txtGetMoney.setForeground(Color.RED);
txtOutMoney.setText("");
}
}
}
} //class

2. import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Final_151_2X extends JFrame implements ActionListener, KeyListener {
private final static int foodPrice[] = {10,15,30};
private int countFood=0;
private int totalPrice=0;
private JLabel lblShow=new JLabel("Select foods.",SwingConstants.CENTER);
private String foodName[][]={{"&sup1;&eacute;&Oacute;&Iacute;&sect;&Oslash;&egrave;&sup1;","&sup1;&eacute;&Oacute;&Ecirc;&Ntilde;&raquo;&raquo;&ETH;&Atilde;&acute;","&sup1;&eacute;&Oacute;&Ecirc;&eacute;&Aacute;","&sup1;&eacute;&Oacute;&aacute;&micro;&sect;&acirc;&Aacute;","&sup1;&eacute;&Oacute;&Aacute;&ETH;&frac34;&Atilde;&eacute;&Ograve;&Ccedil;","&sup1;&eacute;&Oacute;&iexcl;&Atilde;&ETH;&agrave;&uml;&Otilde;&ecirc;&Acirc;&ordm;","&sup1;&eacute;&Oacute;&Aring;&Oacute;&atilde;&Acirc;"},
{"&Aring;&Iacute;&acute;&ordf;&egrave;&Iacute;&sect;","&Atilde;&Ccedil;&Aacute;&Aacute;&Ocirc;&micro;&Atilde;","&frac12;&Iacute;&Acirc;&middot;&Iacute;&sect;","&middot;&Ntilde;&ordm;&middot;&Ocirc;&Aacute;&iexcl;&Atilde;&Iacute;&ordm;","&Ecirc;&Ntilde;&sect;&cent;&Acirc;&Ograve;","&Aring;&Ugrave;&iexcl;&ordf;&Oslash;&ordm;","&Euml;&Aacute;&eacute;&Iacute;&aacute;&iexcl;&sect;"},
{"&cent;&eacute;&Ograve;&Ccedil;&Aacute;&Ntilde;&sup1;&auml;&iexcl;&egrave;","&cent;&eacute;&Ograve;&Ccedil;&cent;&Ograve;&Euml;&Aacute;&Ugrave;","&cent;&eacute;&Ograve;&Ccedil;&Euml;&sup1;&eacute;&Ograve;&agrave;&raquo;&ccedil;&acute;","&iexcl;&euml;&Ccedil;&Acirc;&agrave;&micro;&Otilde;&euml;&Acirc;&Ccedil;&raquo;&Aring;&Ograve;","&micro;&eacute;&Aacute;&Acirc;&Oacute;&iexcl;&Oslash;&eacute;&sect;","&frac14;&Ntilde;&acute;&auml;&middot;","&aacute;&iexcl;&sect;&frac34;&ETH;&acirc;&Aring;&eacute;"}};
private JComboBox[] foodBox=new JComboBox[3]; //(foodName[0]);
private TextArea txtArea=new TextArea("",7,25,0);
private JTextField txtTotal=new JTextField(5);
private JTextField txtGetMoney=new JTextField(5);
private JTextField txtOutMoney=new JTextField(5);
private JLabel labelTotal=new JLabel("&Atilde;&Ograve;&curren;&Ograve;");
private JLabel labelGetMoney=new JLabel("&Atilde;&Ntilde;&ordm;&agrave;&sect;&Ocirc;&sup1;");
private JLabel labelOutMoney=new JLabel("&middot;&Iacute;&sup1;&agrave;&sect;&Ocirc;&sup1;");
private JPanel panelMoney=new JPanel();
private JPanel panelOne=new JPanel();
private JPanel panelTwo=new JPanel();
private JPanel panelThree=new JPanel();
private JPanel panel123=new JPanel();
private JPanel panelFoods=new JPanel();
private JPanel panelOrder=new JPanel();

public Final_151_2X(){
super("Foods Shop");
panelOrder.setLayout(new FlowLayout());
for(int i=0; i<3; i++){
foodBox[i]=new JComboBox(foodName[i]);
panelOrder.add(foodBox[i]);
foodBox[i].addActionListener(this); //???????

}
panelFoods.setLayout(new BorderLayout());
lblShow.setFont(new Font(lblShow.getFont().toString(),Font.BOLD,20));
panelFoods.add(lblShow,BorderLayout.NORTH);
panelFoods.add(panelOrder,BorderLayout.CENTER);

panelMoney.setLayout(new BorderLayout());
panelOne.setLayout(new FlowLayout(FlowLayout.RIGHT));
panelTwo.setLayout(new FlowLayout(FlowLayout.RIGHT));
panelThree.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel123.setLayout(new GridLayout(3,1));
panelOne.add(labelTotal);
panelOne.add(txtTotal);
panelTwo.add(labelGetMoney);
panelTwo.add(txtGetMoney);
panelThree.add(labelOutMoney);
panelThree.add(txtOutMoney);
panel123.add(panelOne);
panel123.add(panelTwo);
panel123.add(panelThree);
txtArea.setEditable(false);
panelMoney.add(txtArea,BorderLayout.NORTH);
panelMoney.add(panel123,BorderLayout.CENTER);

Container container = getContentPane();
container.setLayout(new BorderLayout());
container.setBackground(Color.blue);
container.add(panelFoods,BorderLayout.WEST);
container.add(panelMoney,BorderLayout.EAST);
txtGetMoney.addKeyListener(this); //???????
setSize(550, 250);
setVisible(true);
}

public static void main(String[] args) {
Final_151_2X app = new Final_151_2X();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){
for(int i = 0;i<=2;i++){
if(e.getSource() ==foodBox[i]){
txtArea.append(++countFood+". "+foodBox[i].getSelectedItem().toString()+"n");
totalPrice+=foodPrice[i];
txtTotal.setText(totalPrice+"");
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){
if ((e.getSource()==txtGetMoney)&&(e.getKeyCode()==KeyEvent.VK_ENTER)){
if (Integer.parseInt(txtTotal.getText())<=Integer.parseInt(txtGetMoney.getText())){
txtOutMoney.setText(Integer.parseInt(txtGetMoney.getText())-Integer.parseInt(txtTotal.getText())+"");
txtGetMoney.setForeground(Color.BLACK);
}
else if (Integer.parseInt(txtTotal.getText())>Integer.parseInt(txtGetMoney.getText())){
txtGetMoney.setForeground(Color.RED);
txtOutMoney.setText("");
}
}
}
}