Java 无法解析RadioButton-如何初始化按钮?

Java 无法解析RadioButton-如何初始化按钮?,java,swing,Java,Swing,我正在使用一个简单的单选按钮组。然而,我不知道如何初始化单选按钮,以便将它们添加到if语句中 我创建的按钮如下所示: JRadioButton rdbtn_speed1 = new JRadioButton("Speed 1"); rdbtn_speed1.setSelected(true); buttonGroup_1.add(rdbtn_speed1); rdbtn_speed1.setBounds(10, 91, 97, 23); frame.get

我正在使用一个简单的单选按钮组。然而,我不知道如何初始化单选按钮,以便将它们添加到if语句中

我创建的按钮如下所示:

    JRadioButton rdbtn_speed1 = new JRadioButton("Speed 1");
    rdbtn_speed1.setSelected(true);
    buttonGroup_1.add(rdbtn_speed1);
    rdbtn_speed1.setBounds(10, 91, 97, 23);
    frame.getContentPane().add(rdbtn_speed1);
然后稍后尝试:

    if(rdbtn_speed1.isSelected()){
        System.out.println("1");
    }
    else if(rdbtn_speed2.isSelected()){
        System.out.println("2");
    }
但这不起作用,因为它找不到RDBTN。(即:rdbtn_speed1无法解决)我必须重新声明它们,但到目前为止我发现的任何东西都没有显示这一点。我错过了什么

以下是全套:

公共类框架1{

private JFrame frame;
private JTextField Customer_ID;
private JTextField Destination;
private final ButtonGroup buttonGroup_1 = new ButtonGroup();
/**
 * Launch the application.
 */

public static void main(String[] args) throws ClassNotFoundException,SQLException{
    Class.forName("com.mysql.jdbc.Driver");

    EventQueue.invokeLater(new Runnable() {

        public void run() {
            try {
                Frame1 window = new Frame1();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Frame1() {

    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);



    JButton btnClickMe = new JButton("Click Me!");
    btnClickMe.setBounds(10, 218, 414, 32);
    btnClickMe.setFont(new Font("Palatino Linotype", Font.BOLD, 29));
    btnClickMe.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                submitSQL();
            } catch (ClassNotFoundException | SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });
    frame.getContentPane().add(btnClickMe);

    JLabel lblNewLabel = new JLabel("Shipment Information Input:");
    lblNewLabel.setForeground(Color.BLACK);
    lblNewLabel.setFont(lblNewLabel.getFont().deriveFont(lblNewLabel.getFont().getStyle() | Font.BOLD));
    lblNewLabel.setBounds(10, 11, 226, 20);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblFirstName = new JLabel("Customer ID:");
    lblFirstName.setBounds(10, 42, 103, 14);
    frame.getContentPane().add(lblFirstName);

    Customer_ID = new JTextField();
    Customer_ID.setBounds(107, 39, 86, 20);
    frame.getContentPane().add(Customer_ID);
    Customer_ID.setColumns(10);

    Destination = new JTextField();
    Destination.setBounds(107, 60, 86, 20);
    frame.getContentPane().add(Destination);
    Destination.setColumns(10);

    JLabel lblSendingLocation = new JLabel("Destination:");
    lblSendingLocation.setBounds(10, 63, 87, 14);
    frame.getContentPane().add(lblSendingLocation);

    JRadioButton rdbtn_speed1 = new JRadioButton("Speed 1");
    rdbtn_speed1.setSelected(true);
    buttonGroup_1.add(rdbtn_speed1);
    rdbtn_speed1.setBounds(10, 91, 97, 23);
    frame.getContentPane().add(rdbtn_speed1);

    JRadioButton rdbtn_speed2 = new JRadioButton("Speed 2");
    buttonGroup_1.add(rdbtn_speed2);
    rdbtn_speed2.setBounds(10, 117, 87, 23);
    frame.getContentPane().add(rdbtn_speed2);

    JRadioButton rdbtn_speed3 = new JRadioButton("Speed 3");
    buttonGroup_1.add(rdbtn_speed3);
    rdbtn_speed3.setBounds(10, 143, 87, 23);
    frame.getContentPane().add(rdbtn_speed3);

    JCheckBox chckbx_international = new JCheckBox("International");
    chckbx_international.setBounds(107, 87, 97, 23);
    frame.getContentPane().add(chckbx_international);

    JCheckBox chckbxOversize = new JCheckBox("Oversized");
    chckbxOversize.setBounds(107, 117, 97, 23);
    frame.getContentPane().add(chckbxOversize);

    JCheckBox chckbx_Hazard = new JCheckBox("Hazardous ");
    chckbx_Hazard.setBounds(107, 143, 97, 23);
    frame.getContentPane().add(chckbx_Hazard);


}


public void submitSQL() throws ClassNotFoundException,SQLException{

    String connectionURL = "jdbc:mysql://localhost:3306/projecttest?autoReconnect=true&useSSL=false";
    Connection connection = DriverManager.getConnection(connectionURL, "root", "");
    Statement statement = connection.createStatement();
    //Table Creation

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime now = LocalDateTime.now();
    System.out.println(dtf.format(now)); //2016/11/16 12:08:43

    String CID = Customer_ID.getText();
    String PDestination = Destination.getText();



    if(rdbtn_speed1.isSelected()){
        System.out.println("1");
    }
    else if(rdbtn_speed2.isSelected()){
        System.out.println("2");
    }


    //String insertintosql = "insert into shipment  (ShipName, ShipDate)  VALUES  ('"+name+"','"+dtf.format(now)+"');";

    //statement.executeUpdate(insertintosql);

    connection.close();


}

}
rdbtn_speed1
仅在
initialize
方法中有效,因为它是在该方法中声明的


如果希望跨方法使用它,应该在类级别声明它。

rdbtn\u speed1
仅在
initialize
方法内部有效,因为它是在类级别声明的

如果希望跨方法使用它,则应该在类级别声明它