2016-08-13 33 views
0

我尝试测试是否可以更改组合框选定的索引时,我按下按钮,但它不适用于我什么我试试如果我的组合框被添加到我的框架从另一个班级,请问我想念什么?如何将选定的索引设置为0从另一个类的组合框

,我创建的组合框我的班级:

package MyPackage; 

import javax.swing.DefaultComboBoxModel; 
import javax.swing.JComboBox; 

public class AddMyBox { 

    private JComboBox combobox; 
    String[] array = {"Select", "1", "2", "3"}; 

    public JComboBox theBox() { 
     combobox = new JComboBox(); 
     combobox.setModel(new DefaultComboBoxModel(array)); 
     combobox.setBounds(10, 11, 414, 20); 
     return combobox; 
    } 

} 

,并在那里创建了我的躯体,并在那里我添加成分是类:

package MyPackage; 

import javax.swing.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class MyFrame extends JFrame { 

    public MyFrame() { 
     getContentPane().setLayout(null); 
     setVisible(true); 

     // adding the comboBox from class AddMyBox 
     AddMyBox getBox = new AddMyBox(); 
     getContentPane().add(getBox.theBox()); 

     JButton btnNewButton = new JButton("New button"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       try { 
        // if selected index is 1 make it 0 when the button is pressed 
        if(getBox.theBox().getSelectedIndex() != 0) { 
         getBox.theBox().setSelectedIndex(0); 
        } 
       } catch (Exception e) { 
        // TODO: handle exception 
       } 
      } 
     }); 
     btnNewButton.setBounds(10, 63, 414, 23); 
     getContentPane().add(btnNewButton); 

     setSize(500,400); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    public void MyFrame1() { 
     // TODO Auto-generated method stub 
    } 

} 

我的主类是: 包MyPackage;

public class MyMain { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     MyFrame getFrame = new MyFrame(); 
     getFrame.MyFrame1(); 
    } 

} 
+0

*“你可以告诉我我错过了什么吗?”*基本的面向对象,应该在两个类中计算出来,这两个类意味着从命令行运行。 GUI是一个高级主题,您应该已经了解对象引用和通过方法访问来封装属性。 –

回答

5
public JComboBox theBox() { 
    combobox = new JComboBox(); 
    combobox.setModel(new DefaultComboBoxModel(array)); 
    combobox.setBounds(10, 11, 414, 20); 
    return combobox; 
} 

每次调用theBox()方法创建一个新的组合框,让你的ActionListener逻辑被引用的组合框不可见在框架上,所以你看到的组合框将永远更改。

你班的结构是错误的。您需要:

  1. 为类创建一个构造函数,该类只创建组合框。基本上你需要将前3个语句移到构造函数中。
  2. 更改theBox()方法以简单地返回comboBox变量。 (这是后你唯一剩下的声明中删除前3所陈述

编辑:

我复制从气垫船回答其他问题,因为OP将无法引用它们:

其他问题:

  • combobox.setBounds(10, 11, 414, 20);是不是你想做的事 - 包括使用幻数,使得这种方法非常不灵活,并建议您使用的是null布局,你真的想避免的东西。
  • getContentPane().setLayout(null);是的。不要这样做。虽然Swing的新手可能看起来像是创建复杂GUI的最简单也是最好的方式,但更多Swing GUI的创建使用它们时会遇到更严重的困难。它们不会在GUI大小调整时调整组件的大小,它们是增强或维护的皇室女巫,当它们放在滚动窗格中时它们会完全失败,在所有平台或屏幕分辨率与原始视图不同时,它们看起来会非常糟糕。
  • catch (Exception e) { // TODO: handle exception } - 做评论建议 - 处理你的例外,永远不会忽视它们。否则,你会在闭上眼睛的情况下进行相当于驾驶汽车的编程。
+0

不,你说得对。这是我的大脑尚未发挥作用 - 需要更多的咖啡!1+ –

+0

@camickr Wooooow,它似乎是如此复杂的话题,实际上是什么气垫船充分鳗鱼建议帮助我,使它的工作。 我会尽力一次又一次地阅读你的评论,真正理解它,非常感谢你 –

+0

@AboelmagdSaad,它不那么复杂。它是任何类的基本设计构造函数应该用来创建类的对象。 “getter”方法应该只返回对象的引用,而不是创建一个新的引用。所以你的'theBox()'方法应该被称为'getBox()'。查看JDK中任何类的API,您将看到该类的“getter”和“setter”方法。 – camickr

相关问题