【问题标题】:how to get a value from a Jcombobox array using a anonymous class如何使用匿名类从 Jcombobox 数组中获取值
【发布时间】:2015-07-22 05:45:20
【问题描述】:

我有一个 Jradiobuttons 数组。我试图让 java 匿名类实现 ActionListener,所以当用户按下单选按钮时,我可以做一些事情,但由于这是一个数组,我无法使用 while 循环给出数组索引那么如何识别我正在使用的 Jradiobutton。我想获取该单选按钮的文本并将其保存在另一个变量中...我该怎么做?

这是我到目前为止所做的:

if(count!=0) {
   rs=pst.executeQuery();
   JRadioButton a []=new JRadioButton[count];                       
   jPanel3.setLayout(new GridLayout());
   int x=0;
   ButtonGroup bg=new ButtonGroup();

   while(rs.next()) {    
     a[x]=new JRadioButton(rs.getString("name"));
     bg.add(a[x]);
     jPanel3.add(a[x]); 
     a[x].setVisible(true);

     a[x].addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {      

            JOptionPane.showMessageDialog(null,a[x].getText()); //here i cant use this x...even though i make x global value of x always will be 6 becouse of while loop.

        }
     });                  
     x++;
   }                            
}      

【问题讨论】:

    标签: java arrays swing actionlistener anonymous-class


    【解决方案1】:

    如果我理解正确,您可以设置单选按钮的名称:

    a[x]=new JRadioButton(rs.getString("name"));
    a[x].setName(rs.getString("name"));
    

    ActionPerformed 中你会得到动作的来源:

    public void actionPerformed(ActionEvent e) {
    
    if( e.getSource() instanceof JRadioButton){
    
      String selectedRadioName = ((JRadioButton) e.getSource()).getName();
    
      JOptionPane.showMessageDialog( null, selectedRadioName );
    
    }
    

    【讨论】:

    • 另外你能告诉我这个 JRadioButton 的 instanceof 是什么意思吗
    • 因为你的 ActionListener 是作为匿名类实现的,所以你不需要它。如果您单击了 JRadioButton(即动作源是单选按钮),则检查/确认和处理,这样您就不会在类型转换时遇到类转换异常。
    【解决方案2】:

    你可以...

    为每个JRadioButton 提供ActionCommand,这将通过ActionEvent 提供

    a[x]=new JRadioButton(rs.getString("name"));
    a[x].setActionCommand(String.valueOf(x));
    //...
    a[x].addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
                String cmd = e.getActionCommand();
                int value = Integer.parseInt(cmd);
    
                JOptionPane.showMessageDialog(null, a[value].getText());
            }
    }); 
    

    详情请见How to Use Buttons, Check Boxes, and Radio Buttons

    你可以...

    使用Action API 将消息和操作包围在一个独立的工作单元中...

    public class MessageAction extends AbstractAction {
    
        private String message;
    
        public MessageAction(String text, String message) {
            this.message = message;
            putValue(NAME, text);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, message);
        }
    
    }
    

    然后将其应用于您的按钮,例如...

    a[x] = new JRadioButton(new MessageAction(rs.getString("name"), "Hello from " + x);
    

    更多详情请见How to Use Actions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 2020-06-23
      • 2018-09-04
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多