当前位置:编程学习 > JAVA >>

JAVA中怎样判断单选按钮和复选框是否选中

我做的简单程序(用jbuilder做的),大家帮我看看怎样才能在我点“提交”按钮时,获得单选按钮的值,和复选框的值

package helloworld;

import java.awt.*;

import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Frame1 extends JFrame {
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JButton jButton1 = new JButton();

public Frame1() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(xYLayout1);
jLabel1.setText("性 别:");
xYLayout1.setWidth(326);
xYLayout1.setHeight(214);
jButton1.setText("提 交");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jRadioButton2.setText("女");
this.getContentPane().add(jButton1, new XYConstraints(124, 135, -1, -1));
this.getContentPane().add(jLabel1, new XYConstraints(49, 60, 46, 21));
this.getContentPane().add(jRadioButton1,
new XYConstraints(127, 60, -1, -1));
this.getContentPane().add(jRadioButton2,
new XYConstraints(216, 60, -1, -1));
jRadioButton1.setText("男");
}

public void jButton1_actionPerformed(ActionEvent e) {

}
}


class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

追问:哪复选框怎么判断呢?
答案:测试代码如下:

import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

public class JRadioButtonTest extends JFrame {
private static final long serialVersionUID = 1L;

private JLabel gender;
private JRadioButton m;
private JRadioButton f;
private JButton ok;

private JRadioButtonTest() throws HeadlessException {
gender = new JLabel("Gender:");
m = new JRadioButton("Male", true);
f = new JRadioButton("Female");
ok = new JButton("OK");
init();
eventHander();
showFrame();
}

//初始化

private void init() {
this.setTitle("JRadioButtonTest");

//单选按钮逻辑分组

ButtonGroup bg = new ButtonGroup();
bg.add(m);
bg.add(f);

this.setLayout(new FlowLayout());
this.add(gender);
this.add(m);
this.add(f);
this.add(ok);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

//事件处理

private void eventHander() {
ok.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.println("you selected :"
+ (m.isSelected() ? "male" : "female"));
}
});
}

//显示

private void showFrame() {
this.setSize(300, 80);
this.setVisible(true);
}

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

}

说明:

1、单选按钮与ButtonGroup对象配合使用可创建一组按钮,一次只能选择其中的一个按钮。(创建一个 ButtonGroup 对象并用其 add 方法将 JRadioButton 对象包含在此组中)。ButtonGroup 对象为逻辑分组,不是物理分组。

2、单选按钮是类 AbstractButton的一个子类,方法isSelected() 返回按钮的状态。

请仔细阅读API,谢谢!

看运行结果不就知道了吗

上一个:本人想找一个java培训班,人在北京,求推荐。
下一个:用Java编写一个绘制图形的小程序

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,