⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 select_button.java

📁 复选框、单选按钮及对话框的应用
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Select_button extends JFrame
{
	private JLabel name= new JLabel("姓名");
	private JLabel sex= new JLabel("性别");
	private JTextField table = new JTextField(12);
	private JRadioButton man, woman;
	private JCheckBox math, chinese;
	private ButtonGroup radioGroup;
	private JButton ok, cancel;
	private String[] str=new String[4];
	private String output="";
	public Select_button()
	{
		super("单选按钮和复选框窗口程序");
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		c.add(name);
		c.add(table);
		c.add(sex);
		man= new JRadioButton("男", true);
		c.add(man);
		woman = new JRadioButton("女", false);
		c.add(woman);
		radioGroup = new ButtonGroup();
		radioGroup.add(man);
		radioGroup.add(woman);
		math = new JCheckBox("  数学  ");
		chinese = new JCheckBox("  语文  ");
		ok = new JButton("确定");
		ok.addActionListener(new handle1());
		cancel = new JButton("取消");
		cancel.addActionListener(new handle1());
		c.add(math);
		c.add(chinese);
		c.add(ok);
		c.add(cancel);
		setSize(180,230);
		show();
	}
	
	public static void main(String args[])
	{
		Select_button app = new Select_button();
		app.addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
	}
	
	private class handle1 implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==ok)
			{
				str[0]="姓名:"+table.getText();
				if(man.isSelected())
				    str[1]="性别:男";
				else
				    str[1]="性别:女";
				if(math.isSelected())
				    str[2]="mathmatics";
				else
				    str[2]="";
				if(chinese.isSelected())
				    str[3]="chinese";
				else
				    str[3]="";
				for(int i=0; i<2; i++)
				    output=output+str[i]+"\n";
				output = output+"所选课程名如下:"+"\n";
				for(int i=0; i<2;i++)
				{
					if(str[i+2]!="")
					    output = output+str[i+2]+"\n";
				}
				JOptionPane.showMessageDialog(null, output,
				"Demonstrating string class\"index\"Methods", JOptionPane.INFORMATION_MESSAGE);
				   System.exit(0);
			}
			if(e.getSource()==cancel)
			    System.exit(0);

		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -