我在检索按钮组的值时做错了什么?

时间:2014-02-27 15:23:16

标签: java swing jradiobutton buttongroup

import javax.swing.*;
import javax.swing.event.*; 
import java.awt.*;
import java.awt.event.*;
import net.java.dev.designgridlayout.DesignGridLayout;
import java.sql.*;

class databaseprob 
{
JFrame JF;
Container C,C1;
JDesktopPane JDP;
JInternalFrame JIF5;
JLabel i1l1,i1l2;
JTextField i1t1;
JRadioButton i1r1,i1r2,i1r3,i1r4;
JButton i1b1,i1b2,i1b3;
JInternalFrame JIF1;
ButtonGroup i1bg;
String i1type;


public databaseprob()
{
JF = new JFrame();
JDP = new JDesktopPane();
JF.setVisible(true);
JF.pack();


    JIF1 = new JInternalFrame("Register",true,true, true, true);
C=JF.getContentPane();
C.add(JDP,BorderLayout.CENTER);
JIF1.setVisible(true);
JIF1.setBounds(10, 10, 600, 500); 
    C1 = JIF1.getContentPane();
DesignGridLayout layout = new DesignGridLayout(C1);



    i1l1 = new JLabel("Head ID : ");
    i1l2 = new JLabel("Type : ");

    i1t1 = new JTextField(10);


    i1bg = new ButtonGroup();
    ActionListener actionListener = new ActionListener() { @Override
        public void actionPerformed(ActionEvent e) {
                JRadioButton radioButton = (JRadioButton)e.getSource();
                i1type = radioButton.getText();
                System.out.println(i1type);
            }
        };
    i1r1 = new JRadioButton("Customer");
    i1r1.addActionListener(actionListener);
    i1bg.add(i1r1);

    i1r2 = new JRadioButton("Supplier");
    i1r2.addActionListener(actionListener);
    i1bg.add(i1r2);

    i1r3 = new JRadioButton("Staff");
    i1r3.addActionListener(actionListener);
    i1bg.add(i1r3);

    i1r4 = new JRadioButton("Others");
    i1r4.addActionListener(actionListener);
    i1bg.add(i1r4);






   i1b1 = new JButton("Save");
i1b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
    {
            try
        {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection i1conn =   DriverManager.getConnection("Jdbc:Odbc:TomsJava");

        int i1headID = Integer.parseInt(i1t1.getText());


        Statement i1stmt = i1conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        ResultSet i1rs = i1stmt.executeQuery("SELECT * FROM i1Register");
        i1rs.moveToInsertRow();
        i1rs.updateInt("Head_ID", i1headID);
        i1rs.updateString("Type",i1type);

        i1rs.insertRow();   
        i1stmt.close();
        i1rs.close();
        }       

    catch(SQLException e)
    {
    System.out.println(e);
    }
    catch(ClassNotFoundException z)
    {
    System.out.println(z);
    }
    catch(NumberFormatException n)
    {
    System.out.println(n);
    }
    }
});

   i1b2 = new JButton("Reset");
   i1b3 = new JButton("Close");



layout.row().grid(i1l1).add(i1t1);
layout.row().grid(i1l2).add(i1r1).add(i1r2).add(i1r3).add(i1r4);

layout.emptyRow();
layout.row().center().add(i1b1).add(i1b2).add(i1b3);

JDP.add(JIF1);
}
public static void main(String args[])
{
new databaseprob();
}
}

似乎这段代码正在更新数据库。建议我做任何更改。变量声明创建了我猜的问题,String i1type在radiobutton中声明,并且不能用于Save Button。

3 个答案:

答案 0 :(得分:3)

isSelected(您缺少一些参数)确定ButtonGroup中是否选择了任何单选按钮。

您需要getSelection

if (i1bg.getSelection() != null) {
   String i1type = i1bg.getSelection().getActionCommand();
   ...
}

为此,需要为单选按钮

明确设置ActionCommand

答案 1 :(得分:2)

您忘记了JRadioButtons ButtonGroup {{1}}。

答案 2 :(得分:2)

  

我的问题是我正在尝试检索中的radiobutton值   字符串格式。

首先,这一行甚至不会编译:

String i1type = i1bg.isSelected();

由于:

  1. ButtonGroup.isSelected(ButtonModel model)需要一个 ButtonModel作为论据。
  2. 此方法会返回您尝试分配给boolean变量的String
  3. 您可以按ActionListener教程中所述实现How to Use Buttons, Check Boxes, and Radio Buttons并将此动作侦听器附加到单选按钮来实现目标。例如:

    ActionListener actionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JRadioButton radioButton = (JRadioButton)e.getSource();
            String selectedOption = radioButton.getText();
            System.out.println(selectedOption );
        }
    };
    
    i1r1.addActionListener(actionListener);
    i1r2.addActionListener(actionListener);
    ...
    

    如果您需要的值和单选按钮文字可能不同,那么您可以使用putClientProperty()方法(继承自JComponent),如this answer中所示。