英文:
Why do the JOptionpanes not appear?
问题
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MusicGenre extends JFrame {
String MusicGenre[] = {"Lo-Fi", "Future-House", "Deep-House", "Anderes"};
public MusicGenre() {
setTitle("Genre-Auswahl");
setSize(250, 250);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
JLabel frage = new JLabel("An welches Genre denkst du?");
panel.add(frage);
JComboBox genreAuswahl = new JComboBox(MusicGenre);
panel.add(genreAuswahl);
JButton okBtn = new JButton("Okay");
okBtn.setBackground(Color.BLACK);
okBtn.setForeground(Color.GREEN);
okBtn.setBorder(new LineBorder(Color.RED));
okBtn.setOpaque(true);
JButton backBtn = new JButton("Zurück");
backBtn.setBackground(Color.BLACK);
backBtn.setForeground(Color.RED);
backBtn.setBorder(new LineBorder(Color.RED));
backBtn.setOpaque(true);
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
String selectedGenre = genreAuswahl.getSelectedItem().toString();
if (selectedGenre.equals("Lo-Fi")) {
int lofi = JOptionPane.showConfirmDialog(null, "Produzierst du selber Musik?");
if (lofi == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Meine Idee: Erstelle einen Lo-Fi Beat");
System.exit(0);
} else {
JOptionPane.showMessageDialog(null, "Meine Idee: Hör dir Lo-Fi an");
System.exit(0);
}
} else if (selectedGenre.equals("Future-House")) {
int fh = JOptionPane.showConfirmDialog(null, "Produzierst du selber Musik?");
if (fh == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Meine Idee: Erstelle einen Future-House Beat");
} else {
JOptionPane.showMessageDialog(null, "Meine Idee: Hör dir Future-House an");
}
} else if (selectedGenre.equals("Deep-House")) {
int dh = JOptionPane.showConfirmDialog(null, "Produzierst du selber Musik?");
if (dh == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Meine Idee: Erstelle einen Deep-House Beat");
} else {
JOptionPane.showMessageDialog(null, "Meine Idee: Hör dir Deep-House an");
}
} else if (selectedGenre.equals("Anderes")) {
int other = JOptionPane.showConfirmDialog(null, "Produzierst du selber Musik?");
if (other == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Meine Idee: Erstelle Musik deiner Wahl");
} else {
JOptionPane.showMessageDialog(null, "Hör dir Musik deiner Wahl an");
}
}
}
});
backBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
dispose();
}
});
panel.add(okBtn);
panel.add(backBtn);
add(panel);
setVisible(true);
}
}
英文:
I have created a JCombobox and want that when a certain item is selected in the box, the respective ConfirmDialogs/MessageDialogs appear. What have I done wrong? Because unfortunately nothing happens when I click on my button "okBtn" :/
I have already tried a lot of things, but unfortunately I am still stuck and would appreciate any help (:
My Code:
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MusicGenre extends JFrame {
String MusicGenre[] = {"Lo-Fi", "Future-House", "Deep-House", "Anderes"};
public MusicGenre() {
setTitle("Genre-Auswahl");
setSize(250, 250);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
JLabel frage = new JLabel("An welches Genre denkst du?");
panel.add(frage);
JComboBox genreAuswahl = new JComboBox(MusicGenre);
panel.add(genreAuswahl);
JButton okBtn = new JButton("Okay");
okBtn.setBackground(Color.BLACK);
okBtn.setForeground(Color.GREEN);
okBtn.setBorder(new LineBorder(Color.RED));
okBtn.setOpaque(true);
JButton backBtn = new JButton("Zurück");
backBtn.setBackground(Color.BLACK);
backBtn.setForeground(Color.RED);
backBtn.setBorder(new LineBorder(Color.RED));
backBtn.setOpaque(true);
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (genreAuswahl.equals("Lo-Fi")){
int lofi = JOptionPane.showConfirmDialog(null, "Produzierst du selber Musik?");
if (lofi == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Meine Idee: Erstelle einen Lo-Fi Beat");
System.exit(0);
}else {
JOptionPane.showMessageDialog(null, "Meine Idee: Hör dir Lo-Fi an");
System.exit(0);
}
if (genreAuswahl.equals("Future-House")){
int fh = JOptionPane.showConfirmDialog(null, "Produzierst du selber Musik?");
if (fh == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Meine Idee: Erstelle einen Future-House Beat");
}else {
JOptionPane.showMessageDialog(null, "Meine Idee: Hör dir Future-House an");
}
if (genreAuswahl.equals("Deep-House")){
int dh = JOptionPane.showConfirmDialog(null, "Produzierst du selber Musik?");
if (dh == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Meine Idee: Erstelle einen Deep-House Beat");
}else {
JOptionPane.showMessageDialog(null, "Meine Idee: Hör dir Deep-House an");
}
if (genreAuswahl.equals("Anderes")){
int other = JOptionPane.showConfirmDialog(null, "Produzierst du selber Musik?");
if (other == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Meine Idee: Erstelle Musik deiner Wahl");
}else {
JOptionPane.showMessageDialog(null, "Hör dir Musik deiner Wahl an");
}
}
}
}
}
}
});
backBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
dispose();
}
});
panel.add(okBtn);
panel.add(backBtn);
add(panel);
setVisible(true);
}
}
答案1
得分: 4
在你的 if 语句中,不要像你在代码中所做的那样将 JComboBox 对象与字符串进行比较,比如下面的代码:
genreAuswahl.equals("Lo-Fi")
而是要比较实际选择的项目(它是一个 String 对象):
genreAuswahl.getSelectedItem().equals("Lo-Fi")
对其他的 if 控制语句也采取同样的做法,你的代码应该能够按预期工作。我在本地尝试过,对我来说有效。
你的 if 语句不起作用的原因是 JComboBox 永远不可能等于一个 String 对象。
英文:
In your if statement, instead of comparing a JComboBox object with a string like you do in your code as follows:
genreAuswahl.equals("Lo-Fi")
Compare the actual selected item (which is a String object):
genreAuswhal.getSelectedItem().equals("Lo-Fi")
Do this for other if controls too and your code should work as expected. I tried it locally and worked for me.
The reason any of your if statements won't work is that the JComboBox can never be equal to a String object.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论