java JComboBox索引仅在请求时返回0
我只是在一个JFrame上工作。我在那里添加了一个JComboBox,但不幸的是,JComboBox索引,因此所选项目在“更改操作”时不会更改,我的意思是当我在Swing框架上选择另一个项目时。当被请求时,它只返回0的索引,无论选择了什么项目。组合框的名称为“Kataloge”
它不会返回任何错误。 我怎样才能解决这个问题
static BufferedImage icon;
private JButton update;
private JButton getKata;
private JComboBox<String> Kataloge;
private JLabel Title;
private JLabel WhichKatalog;
private JLabel WhichDatum;
private JLabel line;
private JPanel topper;
private JPanel middle;
private JPanel bottom;
private JPanel frame;
public String Katalog = "Fragenkatalog 1 (normiert)";
public static void main(String args[]) {
MainFrame frame = new MainFrame();
frame.draw();
}
public MainFrame(){
setTitle("Fragebogen erstellen Section");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
}
draw();
setResizable(false);
try {
icon = ImageIO.read(new File("icon.png"));
} catch (IOException e) {
e.printStackTrace();
}
setIconImage(icon);
setVisible(true);
}
public void draw(){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
update = new JButton();
getKata = new JButton();
Title = new JLabel();
line = new JLabel();
WhichKatalog = new JLabel();
WhichDatum = new JLabel();
topper = new JPanel();
middle = new JPanel();
bottom = new JPanel();
frame = new JPanel();
setSize(575, 220);
getKata.setFont(new java.awt.Font("Tahoma", 0, 14));
getKata.setText("Fragebogen erstellen");
getKata.addActionListener(new ActionHandler());
update.setFont(new java.awt.Font("Tahoma", 0, 14));
update.setText("Katalog bearbeiten");
update.addActionListener(new ActionHandler());
Kataloge = new JComboBox<String>();
Kataloge.addItem("Fragenkatalog 1 (normiert)");
Kataloge.addItem("Fragenkatalog 2 (normal)");
Kataloge.setFont(new java.awt.Font("Tahoma", 0, 14));
Kataloge.addItemListener(new ItemHandlerMainFrame());
Title.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
Title.setText("Main Menu");
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog);
WhichDatum.setText(new Datum().getDatum());
line.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
frame.setSize(575, 220);
topper.setSize(575,40);
topper.setLayout(new FlowLayout());
topper.add(Title, CENTER_ALIGNMENT);
middle.setSize(575, 150);
middle.setLayout(null);
middle.add(getKata).setBounds(15, 30, 160, 30);;
middle.add(update).setBounds(195,30,145,30);;
middle.add(Kataloge).setBounds(360, 30, 200, 30);;
bottom.setSize(575,30);
bottom.setLayout(new BorderLayout(50,5));
bottom.add(line, BorderLayout.NORTH);
bottom.add(WhichKatalog, BorderLayout.WEST);
bottom.add(WhichDatum, BorderLayout.EAST);
frame.setLayout(null);
frame.add(topper).setBounds(0, 10, 575, 40);;
frame.add(middle).setBounds(0,45,575,60);;
frame.add(bottom).setBounds(15, 150, 535, 30);;
getContentPane().add(frame);
setLocationRelativeTo(null);
setLocationRelativeTo(null);
}
public void close(){
this.setVisible(false);
this.dispose();
}
private class ActionHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Fragebogen erstellen"){
close();
FragebogenErstellen frame = new FragebogenErstellen();
frame.drawIt();
}
}
}
private class ItemHandlerMainFrame implements ItemListener{
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
System.out.println("Changed to: "+Kataloge.getSelectedIndex());
}
//Katalog = (String) Kataloge.getSelectedItem();
if (Katalog == "Fragenkatalog 1 (normiert)"){
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog);
}if (Katalog == "Fragenkatalog 2 (normal)"){
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog+" ");
}
}
}
# 1 楼答案
您将调用
draw
两次,这将导致问题的无休止,因为它正在创建许多对象基本上发生的事情是,你把你的组件分成两层,你实际上在屏幕上有两个
JComboBox
,一个你可以交互,另一个你不能交互。在你的例子中,它实际上是最后创建的一个你不能交互的组件,当你调用getSelectedIndex
时,它会不断返回0
,因为这是被选中的在本例中,
draw
没有理由成为public
,事实上,在大多数情况下,它可能只是类构造函数的一部分e.getActionCommand()=="Fragebogen erstellen"
不是Java中String
的比较方式,您应该使用String#equals
避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题