简体   繁体   English

如何仅在Java JFrame中按下单选按钮后才启用按钮

[英]How to enable a button only after a radio button was pressed in java JFrame

After a lot of googling i still haven't found what im looking for, mostly because i don't know what i'm looking for. 经过大量的搜寻后,我仍然找不到我要找的东西,主要是因为我不知道自己在寻找什么。

Basically i want the lock in button to be disabled until one of the radio buttons is selected, and i only want one of the radio buttons to be selected at aa time. 基本上,我希望在选择单选按钮之一之前禁用锁定按钮,并且我只希望一次选择一个单选按钮。

I haven't done any formatting yet so its still ugly. 我还没有进行任何格式化,所以它仍然很难看。

My JFrame 我的JFrame

My Code: 我的代码:

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

Start by taking a look at How to Use Buttons, Check Boxes, and Radio Buttons . 首先看一下如何使用按钮,复选框和单选按钮

You can use a ButtonGroup to ensure that only one button within the group is selected 您可以使用ButtonGroup来确保仅选择该组中的一个按钮

You can use an ActionListener to detect changes to the JRadioButton state 您可以使用ActionListener来检测对JRadioButton状态的更改

First you need to add an action listener to whichever RadioButton needs to be pressed before the button is active. 首先,您需要将一个动作侦听器添加到该按钮处于活动状态之前需要按下的任何RadioButton上。

Like this: 像这样:

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

And secondly for the only one RadioButton used at a time you need a ButtonGroup such as: 其次,对于一次仅使用一个RadioButton,您需要一个ButtonGroup,例如:

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

Add all the RadioButtons you want into a group. 将所有想要的单选按钮添加到组中。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM