📄 ComboBoxGallery.java .java dosyası
⬇️ İndir
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxGallery {
public static JFrame frame;
public static JLabel labelFileName = new JLabel();
public static String fileName = "combo_acilis.jpg";
public static JLabel resimliLabel = new JLabel();
public ComboBoxGallery() {
frame = new JFrame("Bağlantılı JComboBox Örneği String[] ile Java Blackboard 061");
frame.setLayout(null);
String[] diziKategori = { "Seçiminiz...", "Alet", "Duygu", "Yiyecek", "Şehir"};
String[] aletListe = { "Alet seçiniz.", "Asma kilit", "Karga burun", "Testere", "Süpürge", "Musluk"};
String[] duyguListe = { "Duygu seçiniz.", "Agresif", "Eğlenceli", "Kibirli", "Üzgün" };
Object[] yiyecekListe = { "Yiyecek seçiniz.", "Armut", "Çorba", "Makarna", "Pasta", "Baklava" };
String[] sehirListe = { "Şehir seçiniz.", "Adıyaman", "Ankara", "Çanakkale", "Edirne", "Istanbul" };
JLabel labelBaslik = new JLabel("Bağlantılı ComboBox ile Resim Gösterme");
labelBaslik.setFont(new Font("Verdana", Font.BOLD, 24));
labelBaslik.setHorizontalAlignment(JLabel.CENTER);
labelBaslik.setBounds(0, 15, 600, 30);
labelFileName.setFont(new Font("Verdana", Font.PLAIN, 17));
labelFileName.setHorizontalAlignment(JLabel.CENTER);
labelFileName.setBounds(0, 60, 600, 30);
JLabel labelKategori = new JLabel("Kategori : ");
labelKategori.setBounds(30, 100, 65, 30);
JComboBox comboKategori = new JComboBox(diziKategori);
comboKategori.setBounds(100, 100, 120,30);
JLabel labelResim = new JLabel("Resim : ");
labelResim.setBounds(225, 100, 50, 30);
JComboBox comboResim = new JComboBox();
comboResim.setBounds(280, 100, 120, 30);
JButton butonGoster = new JButton("Göster");
butonGoster.setBounds(420, 100, 130, 30);
comboKategori.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (comboKategori.getSelectedItem().equals("Alet")) {
comboResim.setFont(new Font("Verdana", Font.PLAIN, 13));
comboResim.setModel(new DefaultComboBoxModel(aletListe));
} else if (comboKategori.getSelectedItem().equals("Duygu")) {
comboResim.setFont(new Font("Verdana", Font.PLAIN, 13));
comboResim.setModel(new DefaultComboBoxModel<String>(duyguListe));
} else if (comboKategori.getSelectedItem().equals("Yiyecek")) {
comboResim.setFont(new Font("Verdana", Font.PLAIN, 13));
comboResim.setModel(new DefaultComboBoxModel<Object>(yiyecekListe)); // <String> yazarsak hata aliriz!!!
} else if (comboKategori.getSelectedItem().equals("Şehir")) {
comboResim.setFont(new Font("Verdana", Font.PLAIN, 13));
comboResim.setModel(new DefaultComboBoxModel<Object>(sehirListe)); // Object Stringín abisi oldugu için burada sorunsuz çalisir.
}
}
});
butonGoster.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String resimIsmi = (String) comboResim.getSelectedItem();
comboResim.setSelectedIndex(0);
switch (resimIsmi) {
case "Asma kilit": fileName = "combo_tool_padlock.jpg";
break;
case "Karga burun": fileName = "combo_tool_pliers.jpg";
break;
case "Testere": fileName = "combo_tool_wood_saw.jpg";
break;
case "Süpürge": fileName = "combo_tool_broom.jpg";
break;
case "Musluk": fileName = "combo_tool_faucet.jpg";
break;
case "Agresif": fileName = "combo_emotion_aggressive.png";
break;
case "Eğlenceli": fileName = "combo_emotion_amusing.png";
break;
case "Kibirli": fileName = "combo_emotion_arrogant.png";
break;
case "Üzgün": fileName = "combo_emotion_sad.png";
break;
case "Armut": fileName = "combo_yiyecek_armut.png";
break;
case "Çorba": fileName = "combo_yiyecek_corba.jpg";
break;
case "Makarna": fileName = "combo_yiyecek_makarna.jpg";
break;
case "Pasta": fileName = "combo_yiyecek_pasta.jpg";
break;
case "Baklava": fileName = "combo_yiyecek_baklava.jpg";
break;
case "Adıyaman": fileName = "combo_sehir_adiyaman.jpg";
break;
case "Ankara": fileName = "combo_sehir_ankara.jpg";
break;
case "Çanakkale": fileName = "combo_sehir_canakkale.jpg";
break;
case "Edirne": fileName = "combo_sehir_edirne.jpg";
break;
case "Istanbul": fileName = "combo_sehir_istanbul.jpg";
break;
}
frame.add(resimGosterLabel(fileName));
}
});
frame.add(labelBaslik);
frame.add(labelFileName);
frame.add(labelKategori);
frame.add(comboKategori);
frame.add(labelResim);
frame.add(comboResim);
frame.add(butonGoster);
frame.add(resimGosterLabel(fileName));
frame.setSize(600, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static JLabel resimGosterLabel(String file) {
labelFileName.setText("Dosya ismi : " + file);
frame.add(labelFileName);
ImageIcon secilenResim = new ImageIcon(new ImageIcon("src/images/"+file).getImage().getScaledInstance(540, 320, Image.SCALE_DEFAULT));;
resimliLabel.setIcon(secilenResim);
resimliLabel.setBounds(30, 140, 540, 320);
return resimliLabel;
}
}