JComboBox 对象在 JButton 中的使用 java

JComboBox object use in JButton in java

I want to use JComboBox object in JButton like if I select the desired text from JComboBox and click on button the image appear accorndingly, is it either possible or not, need help because I cant even make object of JCommbo in JButton.

你必须JComomboBox accessible from the ActionListener in order to get the selected text. You also need to use the proper method for getting the selected text, such as JComboBox#getSelectedItem()。考虑这个例子:

JComboBox<String> myComobBox = new JComboBox<String>();
JButton myButton =  new JButton("jButton");
myComboBox.addItem("6.A.M");
// Add button listener
myButton.addActionListener(e -> {
    // Use getSelectedItem instead of getText
    if(((String) myComboBox.getSelectedItem) == "6.A.M") {
         SixAMRoute sam = new SixAMRoute();
         sam.setVisible(true);
         this.dispose();
    }
});

如果您希望您的动作侦听器是它自己的 class,您将需要使用 access modifiers 使侦听器可以访问 JComboBox