【问题标题】:trouble in JComboBoxJComboBox 中的问题
【发布时间】:2014-03-07 12:41:32
【问题描述】:

您好,我在从 JComboBox 读取值时感到困惑。我想要一个程序,如果用户单击并从JComboBox 中选择任何项目。它将显示为输出。例如我选择苹果它会出现苹果这个主要问题是我的程序没有按钮所以我真的需要它来点击然后输出这里是我到目前为止的代码。

 import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class imagebut extends JFrame 
{
    ImageIcon we = new ImageIcon(getClass().getResource("ban.png"));
    ImageIcon wer = new ImageIcon(getClass().getResource("ba.png"));
    public static void main(String args [])
    {
        imagebut w = new imagebut();
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        w.setSize(300,300);
        w.setVisible(true);


    }
    String []kwe = {"Convertion","Adv.Calculator","Looping","Remarks","Average","MagicSquare","Calendar","Multiplication"};
      JComboBox box = new JComboBox(kwe);

    public imagebut()
    {   

      /*  JButton converter = new JButton("Convertion");
        JButton advancecalc = new JButton("Adv.Calculator");
        JButton calc = new JButton("Calculator");
        JButton Multiplication = new JButton("Multiplication");
        JButton Looping = new JButton("Looping");
        JButton Calendar = new JButton("Calendar");
        JButton Remarks = new JButton("Remarks");
        JButton Average = new JButton("Average");
        JButton Magicsq = new JButton("Magic Square");*/
        JLabel background = new JLabel(new ImageIcon(getClass().getResource("gif.gif")));   
        JPanel pan = new JPanel();

        box.setBounds(10,10,150,25);

        getContentPane().add(background);
        background.add(box);
       /* background.add(converter);
        background.add(calc);
        background.add(advancecalc);
        background.add(Magicsq);
        background.add(Remarks);
        background.add(Calendar);
        background.add(Average);
        background.add(Looping);
        background.add(Multiplication);*/





    }


}

这是我的更新,例如,如果我单击转换,如果平均另一个帧,它将生成一个用于转换的帧。

【问题讨论】:

  • 您能否提供有关您的问题的更多详细信息?你有任何错误吗?您的结果与您的预期不同吗?
  • 你的jcombobox里没有苹果
  • 为了尽快获得更好的帮助,请发布MCTaRE(经过测试和可读的最小完整示例)。
  • 先生,苹果只是一个例子。
  • 你的意思是JComboBox的ActionListener??

标签: java swing user-interface listener jcombobox


【解决方案1】:

ActionListener 添加到您的JComboBox 组件并点赞:

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
     System.out.println(jComboBox1.getSelectedItem().toString());
    }

这将从您的组合框中获取所选项目,它将触发您从组合框中选择的每个项目

【讨论】:

  • @user3392631 Sarz 和 Alex 的两个答案都是可以接受的。试试看。如果你不明白,我建议你去How to use ComboBoxes上的教程
【解决方案2】:

看来你需要使用ItemListener,例如:

JComboBox box = new JComboBox(kwe);    
box.addItemListener(new ItemListener() {

    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getStateChange() == ItemEvent.SELECTED){
            System.out.println(((JComboBox)e.getSource()).getSelectedItem());
            // other actions
        }
    }
});

【讨论】:

  • @user3392631 Sarz 和 Alex 的两个答案都是可以接受的。试试看。如果你不明白,我建议你去How to use ComboBoxes上的教程
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2013-12-18
  • 2012-06-26
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多