怎么添加单选按钮组,怎么使用ButtonGroup这个类.
谢谢大家。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyRadioButtons extends JFrame implements ActionListener
{
JRadioButton rad1,rad2;
ButtonGroup rbgrp;
public MyRadioButtons()
{
setTitle("单选按钮");
rbgrp=new ButtonGroup();
JPanel cpane=(JPanel)getContentPane();
cpane.setLayout(new GridLayout());
rad1=new JRadioButton("启用");
rad1.addActionListener(this);
cpane.add(rad1);
rad2=new JRadioButton("禁用");
rad2.addActionListener(this);
cpane.add(rad2);
rbgrp.add(rad1);
rbgrp.add(rad2);
radadapter radapp=new radadapter();
addWindowListener(radapp);
}
class radadapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==rad1)
{
//setTitle("启用第一个单选按钮");
rad1.setEnabled(false);
rad2.setEnabled(true);
}
else if(e.getSource()==rad2)
{
//setTitle("启用第二个单选按钮");
rad1.setEnabled(true);
rad2.setEnabled(false);
}
}
public static void main(String args[])
{
MyRadioButtons radb=new MyRadioButtons();
radb.setSize(300,200);
radb.setVisible(true);
}
}
此类用于为一组按钮创建一个多斥(multiple-exclusion)作用域。使用相同的 ButtonGroup
对象创建一组按钮意味着“开启”其中一个按钮时,将关闭组中的其他所有按钮
晕,看不懂。我们今天只学了怎么用JBuilder自动生成代码
有个地方不会写。。。。
package studentapplication;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class StudentApplicationFrameClass extends JFrame {
JPanel contentPane;
JLabel lblName = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextArea jTextArea1 = new JTextArea();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JRadioButton jRadioButton1 = new JRadioButton();
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
JCheckBox jCheckBox3 = new JCheckBox();
ButtonGroup button = new ButtonGroup();
这里接下来这么写,就是让性别只能选一个
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
Border border1 = BorderFactory.createEtchedBorder(Color.lightGray,
new Color(148, 145, 140));
public StudentApplicationFrameClass() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Students Detail");
lblName.setText("姓名");
lblName.setBounds(new Rectangle(34, 55, 42, 15));
jTextField1.setBounds(new Rectangle(69, 54, 72, 21));
jLabel2.setText("资历");
jLabel2.setBounds(new Rectangle(214, 55, 42, 15));
jComboBox1.setBounds(new Rectangle(283, 55, 66, 23));
jComboBox1.addItem("本科生");
jComboBox1.addItem("研究生");
jComboBox1.addItem("工程师");
jComboBox1.addItem("博士");
jComboBox1.addItem("其他");
jLabel4.setText("兴趣");
jLabel4.setBounds(new Rectangle(214, 113, 42, 15));
jLabel5.setText("性别");
jLabel5.setBounds(new Rectangle(34, 203, 42, 15));
jLabel3.setText("地址");
jLabel3.setBounds(new Rectangle(34, 118, 42, 15));
jTextArea1.setBounds(new Rectangle(69, 115, 83, 60));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(271, 100, 95, 77));
jPanel1.setLayout(null);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(63, 191, 97, 79));
jPanel2.setLayout(null);
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(17, 12, 103, 23));
jCheckBox1.setText("阅读");
jCheckBox1.setBounds(new Rectangle(19, 6, 85, 23));
jCheckBox2.setText("唱歌");
jCheckBox2.setBounds(new Rectangle(19, 27, 85, 23));
jCheckBox3.setText("跳舞");
jCheckBox3.setBounds(new Rectangle(19, 50, 85, 23));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(17, 45, 103, 23));
jButton1.setBounds(new Rectangle(207, 244, 83, 25));
jButton1.setText("验证");
jButton2.setBounds(new Rectangle(308, 244, 83, 25));
jButton2.setText("重置");
contentPane.setBorder(border1);
contentPane.add(lblName);
contentPane.add(jLabel2);
contentPane.add(jComboBox1);
contentPane.add(jLabel4);
contentPane.add(jLabel5);
contentPane.add(jLabel3);
jPanel1.add(jCheckBox1);
jPanel1.add(jCheckBox2);
jPanel1.add(jCheckBox3);
jPanel2.add(jRadioButton2);
jPanel2.add(jRadioButton1);
contentPane.add(jPanel1);
contentPane.add(jTextArea1);
contentPane.add(jTextField1);
contentPane.add(jButton1);
contentPane.add(jButton2);
contentPane.add(jPanel2);
}
}
材料学本科,现在在上培训机构。
package studentapplication;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class StudentApplicationFrameClass extends JFrame {
JPanel contentPane;
JLabel lblName = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextArea jTextArea1 = new JTextArea();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JRadioButton jRadioButton1 = new JRadioButton();
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
JCheckBox jCheckBox3 = new JCheckBox();
ButtonGroup button = new ButtonGroup();
这里下面该怎么写啊?
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
Border border1 = BorderFactory.createEtchedBorder(Color.lightGray,
new Color(148, 145, 140));
public StudentApplicationFrameClass() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Students Detail");
lblName.setText("姓名");
lblName.setBounds(new Rectangle(34, 55, 42, 15));
jTextField1.setBounds(new Rectangle(69, 54, 72, 21));
jLabel2.setText("资历");
jLabel2.setBounds(new Rectangle(214, 55, 42, 15));
jComboBox1.setBounds(new Rectangle(283, 55, 66, 23));
jComboBox1.addItem("本科生");
jComboBox1.addItem("研究生");
jComboBox1.addItem("工程师");
jComboBox1.addItem("博士");
jComboBox1.addItem("其他");
jLabel4.setText("兴趣");
jLabel4.setBounds(new Rectangle(214, 113, 42, 15));
jLabel5.setText("性别");
jLabel5.setBounds(new Rectangle(34, 203, 42, 15));
jLabel3.setText("地址");
jLabel3.setBounds(new Rectangle(34, 118, 42, 15));
jTextArea1.setBounds(new Rectangle(69, 115, 83, 60));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(271, 100, 95, 77));
jPanel1.setLayout(null);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(63, 191, 97, 79));
jPanel2.setLayout(null);
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(17, 12, 103, 23));
jCheckBox1.setText("阅读");
jCheckBox1.setBounds(new Rectangle(19, 6, 85, 23));
jCheckBox2.setText("唱歌");
jCheckBox2.setBounds(new Rectangle(19, 27, 85, 23));
jCheckBox3.setText("跳舞");
jCheckBox3.setBounds(new Rectangle(19, 50, 85, 23));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(17, 45, 103, 23));
jButton1.setBounds(new Rectangle(207, 244, 83, 25));
jButton1.setText("验证");
jButton2.setBounds(new Rectangle(308, 244, 83, 25));
jButton2.setText("重置");
contentPane.setBorder(border1);
contentPane.add(lblName);
contentPane.add(jLabel2);
contentPane.add(jComboBox1);
contentPane.add(jLabel4);
contentPane.add(jLabel5);
contentPane.add(jLabel3);
jPanel1.add(jCheckBox1);
jPanel1.add(jCheckBox2);
jPanel1.add(jCheckBox3);
jPanel2.add(jRadioButton2);
jPanel2.add(jRadioButton1);
contentPane.add(jPanel1);
contentPane.add(jTextArea1);
contentPane.add(jTextField1);
contentPane.add(jButton1);
contentPane.add(jButton2);
contentPane.add(jPanel2);
}
}
我刚才把你给改好了 贴上来吧 看看和你的样不样
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class StudentApplicationFrameClass extends JFrame {
JPanel contentPane;
JLabel lblName = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextArea jTextArea1 = new JTextArea();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JRadioButton jRadioButton1 = new JRadioButton();
//你没的声明
JRadioButton jRadioButton2 = new JRadioButton();
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
JCheckBox jCheckBox3 = new JCheckBox();
ButtonGroup button = new ButtonGroup();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public StudentApplicationFrameClass()
{
try
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Students Detail");
lblName.setText("姓名");
lblName.setBounds(new Rectangle(34, 55, 42, 15));
jTextField1.setBounds(new Rectangle(69, 54, 72, 21));
jLabel2.setText("资历");
jLabel2.setBounds(new Rectangle(214, 55, 42, 15));
jComboBox1.setBounds(new Rectangle(283, 55, 66, 23));
jComboBox1.addItem("本科生");
jComboBox1.addItem("研究生");
jComboBox1.addItem("工程师");
jComboBox1.addItem("博士");
jComboBox1.addItem("其他");
jLabel4.setText("兴趣");
jLabel4.setBounds(new Rectangle(214, 113, 42, 15));
jLabel5.setText("性别");
jLabel5.setBounds(new Rectangle(34, 203, 42, 15));
jLabel3.setText("地址");
jLabel3.setBounds(new Rectangle(34, 118, 42, 15));
jTextArea1.setBounds(new Rectangle(69, 115, 83, 60));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(271, 100, 95, 77));
jPanel1.setLayout(null);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(63, 191, 97, 79));
jPanel2.setLayout(null);
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(17, 12, 103, 23));
jCheckBox1.setText("阅读");
jCheckBox1.setBounds(new Rectangle(19, 6, 85, 23));
jCheckBox2.setText("唱歌");
jCheckBox2.setBounds(new Rectangle(19, 27, 85, 23));
jCheckBox3.setText("跳舞");
jCheckBox3.setBounds(new Rectangle(19, 50, 85, 23));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(17, 45, 103, 23));
jButton1.setBounds(new Rectangle(207, 244, 83, 25));
jButton1.setText("验证");
jButton2.setBounds(new Rectangle(308, 244, 83, 25));
jButton2.setText("重置");
// contentPane.setBorder(border1);
contentPane.add(lblName);
contentPane.add(jLabel2);
contentPane.add(jComboBox1);
contentPane.add(jLabel4);
contentPane.add(jLabel5);
contentPane.add(jLabel3);
jPanel1.add(jCheckBox1);
jPanel1.add(jCheckBox2);
jPanel1.add(jCheckBox3);
//把你的jRadioButton1和jRadioButton2给添到ButtonGroup里
button.add(jRadioButton2);
button.add(jRadioButton1);
jPanel2.add(jRadioButton2);
jPanel2.add(jRadioButton1);
contentPane.add(jPanel1);
contentPane.add(jTextArea1);
contentPane.add(jTextField1);
contentPane.add(jButton1);
contentPane.add(jButton2);
contentPane.add(jPanel2);
}
//我用JC添上去的
public static void main(String args[])
{
StudentApplicationFrameClass sa = new StudentApplicationFrameClass();
sa.setSize(800,600);
sa.setVisible(true);
}
}