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

标签 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();
}
}  

最佳答案

首先,您需要在按钮处于 Activity 状态之前向需要按下的任何 RadioButton 添加一个 Action 监听器。

像这样:

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

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

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

将您想要的所有 RadioButton 添加到一个组中。

希望这有帮助:)

关于java - 如何仅在 java JFrame 中按下单选按钮后启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22750372/

相关文章:

Java - 为什么这个初始化不起作用?

java - 数字字符串中的逗号分隔符

java - 无法构建同时包含 Azure SDK 和 Google Play 的应用程序

java - 为什么 HTMLEditorKit 不想在 <b> 元素内插入 <i> 元素?

java - 如何使用 showMessageDialog 中的选项来执行操作?

java - 所有测试的 JUnit 设置

java - 如何在 Griffon 中将第二个 MVC 组显示为对话框

java - 使用 Jackcess 读取 Access 文件并使用数据创建 Jtable

java - 如何为骰子制作 Action 监听器按钮?

java - 尝试通过单击 Jbutton 从另一个类打开新的 Jframe