如何在java JFrame中按下单选按钮后启用按钮

时间:2014-03-30 21:54:21

标签: java swing jbutton actionlistener jradiobutton

经过大量的谷歌搜索后,我仍然没有找到我想要的东西,主要是因为我不知道我在寻找什么。

基本上我希望在选择其中一个单选按钮之前禁用锁定按钮,我只希望一次选择一个单选按钮。

我还没有完成任何格式化,所以它仍然很难看。

My JFrame

我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;

public class Game extends JFrame
{
JLabel lblQuestion;
JRadioButton btA;
JRadioButton btB;
JRadioButton btC;
JRadioButton btD;
JButton btLock;
JTextField txtQuestion;
int question = 0;

public Game()
{
    getContentPane().setLayout(null);
    setupGUI();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

void setupGUI()
{
    txtQuestion = new JTextField();
    txtQuestion.setLocation(10,10);
    txtQuestion.setSize(100,25);
    txtQuestion.setText(Integer.toString(question));
    getContentPane().add(txtQuestion);

    lblQuestion = new JLabel();
    lblQuestion.setLocation(50,82);
    lblQuestion.setSize(300,50);
    lblQuestion.setText("No_Label");
    getContentPane().add(lblQuestion);

    btA = new JRadioButton();
    btA.setLocation(50,160);
    btA.setSize(100,50);
    btA.setText("No_Label");
    btA.setSelected(false);
    getContentPane().add(btA);

    btB = new JRadioButton();
    btB.setLocation(250,160);
    btB.setSize(100,50);
    btB.setText("No_Label");
    btB.setSelected(false);
    getContentPane().add(btB);

    btC = new JRadioButton();
    btC.setLocation(50,240);
    btC.setSize(100,50);
    btC.setText("No_Label");
    btC.setSelected(false);
    getContentPane().add(btC);

    btD = new JRadioButton();
    btD.setLocation(250,240);
    btD.setSize(100,50);
    btD.setText("No_Label");
    btD.setSelected(false);
    getContentPane().add(btD);

    btLock = new JButton();
    btLock.setLocation(150,303);
    btLock.setSize(100,50);
    btLock.setText("Lock in");
    getContentPane().add(btLock);

    btLock.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            question = question + 1;
            txtQuestion.setText(Integer.toString(question));
            try{
                ArrayList<String> list = questions(question);
            }catch(Exception ex){}
        }
    });

    setTitle("Who wants to be a millionare");
    setSize(570,400);
    setVisible(true);
    setResizable(true);
    setLocationRelativeTo(null);


}

public ArrayList<String> questions(int quesion) throws IOException{
    File file = new File("questions.txt");
    if (!file.exists()){
        throw new FileNotFoundException("Could not find \"users\" file");
    }
    FileReader fr = new FileReader(file.getAbsoluteFile());
    BufferedReader br = new BufferedReader(fr);

    ArrayList<String> list = new ArrayList<String>();
    String s;
    for(int i = 0; i*4 <= quesion; i++){
        br.readLine();
    }
    while((s=br.readLine()) !=null){
        list.add(s);
    }
    br.close();
    return list;   
}

public static void main( String args[] )
{
    new Game();
}
}  

2 个答案:

答案 0 :(得分:4)

首先看一下How to Use Buttons, Check Boxes, and Radio Buttons

您可以使用ButtonGroup确保只选择组中的一个按钮

您可以使用ActionListener检测JRadioButton

的更改

答案 1 :(得分:4)

首先,您需要为按钮处于活动状态之前需要按下的RadioButton添加动作侦听器。

像这样:

someRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
        someButton.enabled(true);
}
});

其次,对于一次使用的唯一一个RadioButton,你需要一个ButtonGroup,如:

ButtonGroup myButtonGroup = new ButtonGroup();
myButtonGroup.add(someRadioButton);

将所需的所有RadioButtons添加到组中。

希望这会有所帮助:)