RadioButton을 확인할 수 없습니다. 버튼을 어떻게 초기화합니까?

알렉스

간단한 라디오 버튼 그룹을 사용하고 있습니다. 하지만 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");
    }

그러나 이것은 rdbtns를 찾을 수 없기 때문에 작동하지 않습니다. (예 : rdbtn_speed1을 확인할 수 없음) 다시 선언해야하지만 지금까지 찾은 내용이 표시되지 않습니다. 내가 뭘 놓치고 있니?

다음은 전체 세트입니다.

public class Frame1 {

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();


}

}

조 c

rdbtn_speed1initialize메서드 내에서만 유효합니다 . 왜냐하면 그것이 선언 된 곳이기 때문입니다.

여러 메서드에서 사용하려면 클래스 수준에서 선언해야합니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

토글 버튼 그룹을 어떻게 확인할 수 있습니까?

제출 버튼이없는 HTML5 양식의 유효성을 어떻게 확인할 수 있습니까?

이 표에서 확인 된 라디오 버튼을 어떻게 확인할 수 있습니까?

FontAwesome 아이콘을 RadioButton 버튼으로 어떻게 사용할 수 있습니까?

Tkinter Messagebox 모듈에서 '확인' 버튼을 어떻게 다시 프로그래밍할 수 있습니까?

다중 라디오 버튼을 확인할 수 없습니다.

새 pyqt 창을 어떻게 초기화 할 수 있습니까?

라디오 버튼을 확인할 수 없습니다

버튼을 클릭 한 후 jstree에서 노드가 부모인지 어떻게 확인할 수 있습니까?

로그인 버튼없이 Google 로그인을 어떻게 초기화합니까?

tkinter, 어떻게 버튼을 드래그 할 수 있습니까?

모든 버튼을 어떻게 교체 할 수 있습니까?

AngularJS에서 다른 값을 어떻게 초기화 할 수 있습니까?

mer의 유효성을 어떻게 확인할 수 있습니까? 1 Mai 2019 버튼?

버튼을 눌렀을 때 눌린 다른 버튼을 어떻게 시뮬레이션 할 수 있습니까?

다른 연속 버튼을 눌렀을 때 버튼을 어떻게 선택 해제할 수 있습니까?

사용중인 Jasmine 버전을 어떻게 확인할 수 있습니까?

사용중인 Angular 버전을 어떻게 확인할 수 있습니까?

실행중인 별표 버전을 어떻게 확인할 수 있습니까?

대기 및 반복 코드를 어떻게 수정합니까? 작업을 반복하는 버튼을 클릭 할 수 없습니다.

다른 클래스 버튼을 어떻게 호출 할 수 있습니까?-Swift

버튼이 이미 클릭되었을 때 어떻게 버튼을 비활성화 할 수 있습니까?

내 수업을 어떻게 나열-초기화 할 수 있습니까?

외부 URL을 어떻게 확인할 수 있습니까?

배열 + 유형을 어떻게 확인할 수 있습니까?

PDO 연결을 어떻게 확인할 수 있습니까?

JavaScript에서 IsPostBack을 어떻게 확인할 수 있습니까?

NaN 값을 어떻게 확인할 수 있습니까?

lightuserdata의 유형을 어떻게 확인할 수 있습니까?

TOP 리스트

  1. 1

    JSoup javax.net.ssl.SSLHandshakeException : <url>과 일치하는 주체 대체 DNS 이름이 없습니다.

  2. 2

    상황에 맞는 메뉴 색상

  3. 3

    java.lang.UnsatisfiedLinkError : 지정된 모듈을 찾을 수 없습니다

  4. 4

    SMTPException : 전송 연결에서 데이터를 읽을 수 없음 : net_io_connectionclosed

  5. 5

    std :: regex의 일관성없는 동작

  6. 6

    Ionic 2 로더가 적시에 표시되지 않음

  7. 7

    JNDI를 사용하여 Spring Boot에서 다중 데이터 소스 구성

  8. 8

    정점 셰이더에서 카메라에서 개체까지의 XY 거리

  9. 9

    Xcode10 유효성 검사 : 이미지에 투명성이 없지만 여전히 수락되지 않습니까?

  10. 10

    Android Kotlin은 다른 활동에서 함수를 호출합니다.

  11. 11

    SQL Server-현명한 데이터 문제 받기

  12. 12

    Windows cmd를 통해 Anaconda 환경에서 Python 스크립트 실행

  13. 13

    rclone으로 원격 디렉토리의 모든 파일을 삭제하는 방법은 무엇입니까?

  14. 14

    내 페이지 번호의 서식을 어떻게 지정합니까?

  15. 15

    Cassandra에서 버전이 지정된 계층의 효율적인 모델링

  16. 16

    Quickly 프로그램과 함께 작동하도록 Eclipse를 어떻게 설정할 수 있습니까?

  17. 17

    인코더없이 Azure 미디어 서비스 비디오 트림

  18. 18

    WSL 및 Ubuntu, 초기화 파일 이동 방법

  19. 19

    OpenCV에서. C ++ 컴파일러는 간단한 테스트 프로그램을 컴파일 할 수 없습니다. Clang ++ 사용

  20. 20

    마우스 휠 JQuery 이벤트 핸들러에 대한 방향 가져 오기

  21. 21

    ViewModel에서 UI 요소를 비동 시적으로 업데이트하는 방법

뜨겁다태그

보관