【问题标题】:JComboBox Index just returns 0 upon requestJComboBox 索引仅根据请求返回 0
【发布时间】:2017-05-01 10:21:00
【问题描述】:

我只是在处理 JFrame。我在那里添加了一个 JComboBox,但不幸的是 JComboBox 索引,因此选定的项目不会在“改变的动作”时发生变化,我的意思是当我在 Swing Frame 上选择另一个项目时。当请求时,它只返回一个 0 的索引,无论选择什么项目。 ComboBox 的名称是“目录”。

它不会返回任何错误。 我该如何解决这个问题?

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) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) 请学习常见的 Java 命名法(命名约定 - 例如EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并始终如一地使用它。 3) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。 ..
  • .. 而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • e.getActionCommand()=="Fragebogen erstellen" 不是在 Java 中比较 Strings 的方式
  • 您调用draw 两次,这将导致问题无穷无尽,因为它会重新创建许多对象。在这种情况下,draw 没有理由成为public,事实上,在大多数情况下,它可能只是类构造函数的一部分

标签: java swing indexing jframe jcombobox


【解决方案1】:

您调用draw 两次,这将导致问题无穷无尽,因为它会重新创建许多对象。

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

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

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

避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

【讨论】:

  • 谢谢!!那是我的错。
  • 我尝试只使用布局管理器,但实际上我浪费了一整天的时间试图让它们按照我想要的方式工作。最后我创建了一堆 JPanel,但还没有成功。
  • 在没有布局管理器的情况下,您将花费更多时间尝试让它工作
猜你喜欢
  • 2015-12-08
  • 2013-08-02
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2016-02-09
相关资源
最近更新 更多