Author Topic: ช่วยดูโค้ดจาว่าอันนี้ให้ทีครับ  (Read 1501 times)  Share 

0 Members and 1 Guest are viewing this topic.

Offline •``GrOsSoTeSt``•

  • มือใหม่หัดเข้าไอที
  • *
  • Posts: 29
  • Karma: +0/-0
  • Gender: Male
ช่วยแก้โค้ดหน่อยค๊าฟฟฟ

สร้าง Swing application ชื่อ Pizza เพื่อแสดง JFrame และภายใยJFrame ประกอบด้วย

1. comboBox เพื่อนแสดงขนาดพิซซ่าได้แก่ small(150บาท) Medium(200บาท) เปนต้น ให้แสดงราคาผ่านJLabel ข้อหนึ่งทำได้แล้วโอเค
2. JCheckBox เลือกหน้าพิซซ่า ซึ่งมีหน้าตามที่อยู่ในโค้ด เลือกได้หน้าเดียว เมื่อเลือกหน้าเสร็จ ให้บวกราคาพิซซ่าเพิ่มอีก100 ทุกๆหน้า
3. JButton สำหรับคำนวณเงินทั้งหมดจากลูกค้าที่ต้องจ่ายผ่านทางJTextField

ประเด็นที่ทำไม่ได้ก้คือ ข้อสองกะข้อสาม ตรงที่รวมราคาแล้วให้แสดงออกทาง JTextField

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class Pizza extends JFrame implements ItemListener,ActionListener {

JComboBox size = new JComboBox();
JLabel Label1 = new JLabel();


public Pizza(){
super("Pizza ");
setSize(250,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

Container contentArea = getContentPane();
contentArea.setBackground(Color.red);

FlowLayout flowManager = new FlowLayout();
contentArea.setLayout(flowManager);


size.addItem("Size");
size.addItem("Small");
size.addItem("Medium");
size.addItem("Large");
size.addItem("Extra-large");
size.addItemListener(this);
contentArea.add(size);
contentArea.add(Label1);
setContentPane(contentArea);

ButtonGroup a=new ButtonGroup();
JCheckBox ch1 = new JCheckBox("Seafood");
JCheckBox ch2 = new JCheckBox("Chicken");
JCheckBox ch3 = new JCheckBox("Pork");
JCheckBox ch4 = new JCheckBox("Fish");
JCheckBox ch5 = new JCheckBox("Vegetable");
a.add(ch1);
a.add(ch2);
a.add(ch3);
a.add(ch4);
a.add(ch5);
contentArea.add(ch1);
contentArea.add(ch2);
contentArea.add(ch3);
contentArea.add(ch4);
contentArea.add(ch5);

JButton pay = new JButton("Pay");
contentArea.add(pay);
pay.addActionListener(this);

JTextField sum = new JTextField(4);
contentArea.add(sum);










}
public void itemStateChanged(ItemEvent event){
String choice = event.getItem().toString();
if(choice=="Size"){
Label1.setText("Size");
}
else if(choice=="Small"){
Label1.setText("Price 150 Baht");

}
else if(choice=="Medium") {
Label1.setText("Price 200 Baht");
int num=200;
}
else if(choice=="Large") {
Label1.setText("Price 250 Baht");

}
else if(choice=="Extra-large"){
Label1.setText("Price 350 Baht");

}
}

public void actionPerformed(ActionEvent event){

}

public static void main(String[] args) {
new Pizza();


}
}