网站建设资讯

NEWS

网站建设资讯

java使用单选按钮代码,javaweb单选按钮

编写一个Java应用程序,窗体上有一个单选按钮,具体界面设计如下(只需要代码):

import javax.swing.BorderFactory;

淳安ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

import javax.swing.ButtonGroup;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

public class aaa

{

/**

* @param args

*/

public static void main(String[] args)

{

TextFrame frame = new TextFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class TextFrame extends JFrame

{

public TextFrame()

{

setTitle("考试题目");

setBounds(300,300,200,120);

TextPanel panel = new TextPanel();

add(panel);

}

}

class TextPanel extends JPanel

{

private JRadioButton r1,r2;

public TextPanel()

{

//实例化单选按钮

r1 = new JRadioButton("男");

r2 = new JRadioButton("女");

JPanel p = new JPanel();

p.setBorder(BorderFactory.createTitledBorder("请选择性别"));

p.add(r1);

p.add(r2);

ButtonGroup bg = new ButtonGroup();

//将需要划分为一组的单选按钮对象添加到按钮组(注意只是逻辑上添加 和界面没有关系)

bg.add(r1);

bg.add(r2);

add(p);

}

}

java单选按钮

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.applet.*;

import java.sql.*;

public class Test extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;

private JPanel jPanel;

private JButton jButton1,jButton2,jButton3;

private JTextArea ta1;

private JTextField tf1;

private Checkbox cb1,cb2;

private CheckboxGroup g;

public Test(String title) {

super(title);

init();

}

private void init() {

jPanel=new JPanel();

jPanel.setLayout(new FlowLayout());

ta1=new JTextArea();

tf1=new JTextField(20);

jButton1=new JButton("查询记录");

jButton2=new JButton("添加用户");

jButton3=new JButton("添加记录");

jButton1.addActionListener(this);

jButton2.addActionListener(this);

jButton3.addActionListener(this);

g = new CheckboxGroup();

cb1 = new Checkbox("1", g, false);

cb2 = new Checkbox("2", g, true);

jPanel.add(ta1);

jPanel.add(jButton1);

jPanel.add(jButton2);

jPanel.add(jButton3);

jPanel.add(tf1);

jPanel.add(cb1);

jPanel.add(cb2);

this.add(jPanel);

this.setSize(300,700);

this.setResizable(false);

this.setVisible(true);

this.addWindowListener(new WindowAdapter() {

public void windowClosing(final WindowEvent e) {

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e) {

if(e.getSource().equals(jButton1)){

if(cb1.isSelected()){

System.out.println("1");

}

else{

System.out.println("2");

}

}

}

}

给你做了小小的修改,你再试试

用JAVA面版显示性别单选按钮怎么实现

/*

* 导入所需的包

*/

import javax.swing.*;

public class Show {

public static void main(String args[]) {

JFrame frame=new JFrame("单选按钮实例"); //创建窗口对象

JPanel panel=new JPanel(); //创建面板对象

ButtonGroup bg=new ButtonGroup(); //创建单选按钮组

JRadioButton male=new JRadioButton("男"); //创建单选按钮

JRadioButton female=new JRadioButton("女");

bg.add(male); //把单选按钮添加到组中

bg.add(female);

panel.add(male); //把按钮添加到面板中

panel.add(female);

frame.getContentPane().add(panel); //获取窗口的内容面板,将自定义面板添加进去

frame.setSize(220,200); //设置窗口大小

frame.setVisible(true); //显示窗口

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭窗口操作

}

}

java怎么制作单选按钮

你好:

JRadioButton类的常用构造单选按钮有以下几个:

1.JRadioButton():用空标题构造单选按钮。

2.JRadioButton(String s):用给定的标题s构造单选按钮。

3.JRadioButton(String s,boolean b):用给定的标题s构造单选按钮,参数b设置选中与否的初始状态。

单选按钮使用时需要使用ButtonGroup将单选按钮分组,单选按钮的分组方法是先创建对象,然后将同组的单选按钮添加到同一个ButtonGroup对象中。

java让单选按钮默认选中的方法

JRadioButton(Icon

icon,

boolean

selected)

创建一个具有指定图像和选择状态的单选按钮,但无文本。

JRadioButton(String

text,

boolean

selected)

创建一个具有指定文本和选择状态的单选按钮。

JRadioButton(String

text,

Icon

icon,

boolean

selected)

创建一个具有指定的文本、图像和选择状态的单选按钮。

=================================================

这3种构造函数都可以生成默认选种的单选按钮

举例:

new

JRadioButton("默认选种的",true);


名称栏目:java使用单选按钮代码,javaweb单选按钮
分享路径:http://cdweb.net/article/dseisgc.html