ile JComboBox'tan toplam maliyet hesaplanıyor İki adet JComboBox
bileşen ve bir JLabel
ürünüm var. İki açılan kutunun her ikisi de Dizeleri içerir ve etiketin fiyatın eklenmesi gerekir. Sorunum, birleşik giriş kutusu seçimlerine int değerlerinin nasıl ayarlanacağını anlayamıyorum ve daha sonra bu tam sayıları ekleyip etiket üzerinde çıkış yapmasını sağlayamam. Şimdiye kadar 0'da yapışıyor ve değişmiyor.Öğe
Tüm kod düzeltmeleri/yapıcı eleştiriler hoş geldiniz. Zaten seçili dizine fiyat dizilerde endeksi ile aynıdır biliyorum çünkü
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class CottageRental extends JFrame implements ItemListener {
// Declare all instance data (primitives and objects used)
private int WIDTH = 675;
private int HEIGHT = 320;
Container con;
JButton [] button;
JLabel label1, label2, label3;
JComboBox box1, box2;
String[] box1options = {"1 Bedroom - $600", "2 Bedroom - $800", "3 Bedrooms - $1,000"};
String[] box2options = {"Horse Back Riding - $60", "Rafting - $40", "Row Boat Rental - $50"};
int[] box1prices = {600, 800, 1000};
int[] box2prices = {60, 40, 50};
private Font font1 = new Font("Arial", Font.BOLD, 30);
public int total1, total2;
//constructor
public CottageRental() {
super("Cottage Rental");
con = getContentPane();
con.setBackground(Color.GRAY);
con.setLayout(new BorderLayout());
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//instantiate all the components in the constructor
}
public void createGUI() {
label1 = new JLabel("WoodBerry Cottage Rental");
label1.setFont(font1);
label1.setBackground(Color.WHITE);
label2 = new JLabel("Rental Amount Due: " + total1 + " and " + total2);
box1 = new JComboBox(box1options);
box2 = new JComboBox(box2options);
con.add(label1, BorderLayout.NORTH);
con.add(label2, BorderLayout.SOUTH);
con.add(box1, BorderLayout.WEST);
con.add(box2, BorderLayout.EAST);
box1.addItemListener(this);
box2.addItemListener(this);
}
public static void main(String[] args) {
CottageRental object = new CottageRental();
object.createGUI();
}
@Override
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
if(e.getStateChange()==ItemEvent.SELECTED) {
if (source == box1) {
int choice1 = box1.getSelectedIndex();
if (choice1 == 0) {
total1 = box1prices[0];
} else if (choice1 == 1) {
total1 = box1prices[1];
} else if (choice1 == 2) {
total1 = box1prices[2];
}
}
if (source == box2) {
int choice2 = box2.getSelectedIndex();
if (choice2 == 0) {
total2 = box2prices[0];
} else if (choice2 == 1) {
total2 = box2prices[1];
} else if (choice2 == 2) {
total2 = box2prices[2];
}
}
}
}
}
kodunun geri kalanını değiştirmek zorunda kalmadan fiyatı (veya tanımı) değiştirmeyi kolaylaştırarak çok teşekkür ederiz! Bunu düşünmediğime inanamıyorum. Gerçekten onu takdir ederim. – Cory