JComboBox Index只在请求时返回0

时间:2017-05-01 10:21:00

标签: java swing indexing jframe jcombobox

我刚刚在JFrame上工作。我在那里添加了一个JComboBox,但不幸的是JComboBox索引,所以所选项目在“更改动作”时没有改变,我的意思是当我在Swing Frame上选择另一个Item时。请求时,无论选择哪个项目,它都只返回索引0。 ComboBox的名称是“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 个答案:

答案 0 :(得分:0)

你正在调用draw两次,这将导致问题没有结束,因为它正在重新创建许多对象。

基本上发生了什么,是你的组件双重分层,你在屏幕上实际上有两个JComboBox,一个你与之交互,一个你不能。在你的情况下,它实际上是你最后创建的无法与之交互的那个,当你调用0时它会不断返回getSelectedIndex,因为这就是所选的

在这种情况下,draw没有理由public,事实上,在大多数情况下,它可能只是类构造函数的一部分

e.getActionCommand()=="Fragebogen erstellen"不是在Java中比较String的方式,您应该使用String#equals

避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正