GUI

package 上课;

import java.awt.Component;

import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FrameDemo {

public static void createAndShowFrame() {
	JFrame mainFrame = new JFrame("Frame Demo");
	mainFrame.setSize(500, 300);
	mainFrame.setLocation(400, 350);
	mainFrame.setVisible(true);
	mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	
	// 摆放规则 
	GridLayout gridLayout = new GridLayout(7, 2);
	// 用于将多个组件摆放到同一个位置
	JPanel mainPanel = new JPanel(gridLayout);
	
	// 文本框
	JLabel userNameLbl = new JLabel("用户名");
	JTextField userNameTxt = new JTextField();
	mainPanel.add(userNameLbl);
	mainPanel.add(userNameTxt);
	
	// 密码框
	JLabel passwordLbl = new JLabel("密码");
	JPasswordField passwordTxt = new JPasswordField();
	mainPanel.add(passwordLbl);
	mainPanel.add(passwordTxt);
	
	// 性别——单选按钮
	JLabel genderLbl = new JLabel("性别");
	JRadioButton maleRadio = new JRadioButton("男", true);
	JRadioButton femaleRadio = new JRadioButton("女");
	// 将两个单选按钮加到一个组中,以便于按钮间的互斥
	ButtonGroup genderGroup = new ButtonGroup();
	genderGroup.add(maleRadio);
	genderGroup.add(femaleRadio);
	// 放到一个面板中,以把几个单选按钮放到同一个格内
	JPanel genderPanel = new JPanel();
	genderPanel.add(maleRadio);
	genderPanel.add(femaleRadio);
	mainPanel.add(genderLbl);
	mainPanel.add(genderPanel);
	
	// 爱好——复选框
	JLabel favorLbl = new JLabel("爱好");
	JCheckBox swimChk = new JCheckBox("游泳");
	JCheckBox taijiChk = new JCheckBox("太极");
	JCheckBox gameChk = new JCheckBox("游戏");
	JCheckBox englishChk = new JCheckBox("英语");
	// 爱好面板
	JPanel favorPanel = new JPanel();
	favorPanel.add(swimChk);
	favorPanel.add(taijiChk);
	favorPanel.add(gameChk);
	favorPanel.add(englishChk);
	mainPanel.add(favorLbl);
	mainPanel.add(favorPanel);
	
	// 住址——下拉列表
	JLabel addressLbl = new JLabel("住址");
	JComboBox addressBox = new JComboBox<>();
	addressBox.addItem("北京");
	addressBox.addItem("东京");
	addressBox.addItem("巴黎");
	addressBox.addItem("纽约");
	mainPanel.add(addressLbl);
	mainPanel.add(addressBox);
	
	// 留言——多行文本框
	JTextArea memoArea = new JTextArea(5, 80);
	JLabel memoLbl = new JLabel("留言");
	mainPanel.add(memoLbl);
	mainPanel.add(memoArea);
	
	// 确定和重置按钮
	JButton okBtn = new JButton("确定");
	JButton resetBtn = new JButton("重置");
	mainPanel.add(okBtn);
	mainPanel.add(resetBtn);
	okBtn.addActionListener(e -> {
		// 读取文本框的值
		String userName = userNameTxt.getText();
		// 读取密码框的值
		String password = passwordTxt.getText();
		
		String gender = "男";
		if (femaleRadio.isSelected()) {
			gender = "女";
		}
		
		StringBuilder favorStr = new StringBuilder();
		Component[] favors = favorPanel.getComponents();
		for (Component c : favors) {
			JCheckBox jcb = (JCheckBox) c;
			if (jcb.isSelected()) {
				favorStr.append(jcb.getText());
				favorStr.append(",");
			}
		}
		String favor = favorStr.toString();
		
		String address = (String) addressBox.getSelectedItem();
		String memo = memoArea.getText();
		String register = "用户名:" + userName
				+ ", 密码:" + password
				+ ", 性别:" + gender
				+ ", 爱好:" + favor
				+ ", 住址:" + address
				+ ", 留言:" + memo;
		
		JOptionPane.showMessageDialog(mainFrame, register, "注册成功", JOptionPane.ERROR_MESSAGE);
	});
	resetBtn.addActionListener(e->{
		userNameTxt.setText("");
		passwordTxt.setText("");
		maleRadio.setSelected(true);
		StringBuilder favorStr = new StringBuilder();
		Component[] favors = favorPanel.getComponents();
		for (Component c : favors) {
			JCheckBox jcb = (JCheckBox) c;
			jcb.setSelected(false);
		}
		addressBox.setSelectedItem("北京");
		memoArea.setText("");
	});
	
	mainFrame.add(mainPanel);
}

public static void main(String[] args) {
	SwingUtilities.invokeLater(FrameDemo::createAndShowFrame);
}

}

你可能感兴趣的:(GUI)