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

我只需要用java代码编程
2025-01-03 21:35:44
推荐回答(1个)
回答1:

import javax.swing.BorderFactory;
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);
}
}