cbTexture.setSelectedIndex(0);在最后被调用
构造函数,但这无济于事
自动选择子贴图
之后。为什么?

  public class TradeInDialog extends JDialog {

    public TradeInDialog(Database _db) throws SQLException
    {
        ....
        setupTexture();

        cbTexture.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {

                if(e.getStateChange() == ItemEvent.SELECTED) {
                    selectedTexture = cbTexture.getSelectedIndex()+1;
                    setupSubTexture();
            }
        });
          ....

         cbTexture.setSelectedIndex(0);
    }

最佳答案

您必须在事件处理程序类外部进行第一次调用setupSubTexture方法,因此您的代码必须像这样:

公共类TradeInDialog扩展了JDialog {

public TradeInDialog(Database _db) throws SQLException
{
    selectedTexture = 1;
    setupSubTexture();
    setupTexture();

    cbTexture.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {

            if(e.getStateChange() == ItemEvent.SELECTED) {
                selectedTexture = cbTexture.getSelectedIndex()+1;
                setupSubTexture();
        }
    });

    cbTexture.setSelectedIndex(0);
}

10-06 13:07