Swing回顾

最近实习,交给一个小任务,用swing写一个自动生成代码的工具,现在可以休息一下,就总结一下自己用到的,留个笔记,以后用时好理。

1. Swing的基本组件

这些组件的使用是基于JFrame的,所以在使用时要基础JFrame,在界面运行时,一定要加 jFrame.setVisible(true);

1.1 输入框
JTextField text = new JTextField ("文本框......");
//取值
String value = text.getText();
//放值
text.setText("hello !");

在这里插入图片描述

1.2 文本域
JTextArea textArea = new JTextArea ("文本域......");
//取值
String value = textArea .getText();
//放值
textArea .setText("hello !");
1.3 标签
JLabel lable = new JLabel("我是一个标签");
//取值、放值同上

在这里插入图片描述

1.4 单选框
CheckboxGroup group = new CheckboxGroup();
Checkbox box1 = new Checkbox("男",true,group);
Checkbox box2 = new Checkbox("女",false,group);
//监听事件
@Override
public void actionPerformed(ActionEvent e) {
	if(box1.getState == true){
		System.out.println("男生");
	}
	if(box2.getState == true){
		System.out.println("女生");
	}
}
1.5 多选框
JCheckBox checkBox1 = new JCheckBox("插入/修改");
JCheckBox  checkBox2 = new JCheckBox("查询");
JCheckBox  checkBox3= new JCheckBox("删除");
//监听事件
@Override
public void actionPerformed(ActionEvent e) {
			if (checkBox1 .isSelected()) {// 选择插入修改方法
				System.out.println("插入/修改");
			}
			if (checkBox2 .isSelected()) {// 选择查询方法
				System.out.println("查询");
			}
			if (checkBox3.isSelected()) {// 选择删除方法
				System.out.println("删除");
			}
}

在这里插入图片描述

1.6 下拉框
String[] databaseName = new String[]{"qq","ww","xx"};
JComboBox databaseBox = new JComboBox(databaseName);// 下拉框
//取被选择的值
String selectItemValue = (String) databaseBox.getSelectedItem();

在这里插入图片描述

1.7 按钮
JButton nextBtn = new JButton ("按钮");
nextBtn.addActionListener(this);
//监听事件
@Override
public void actionPerformed(ActionEvent e) {
	if (e.getSource() == nextBtn ) {
		System.out.println("按钮被点击了");
	}
}

在这里插入图片描述

1.8选项卡
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setName("选项卡面板");
tabbedPane.setBounds(400, 400, 400, 400);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);// 设置选项卡标签的布局方式为滚动布局
tabbedPane.addChangeListener(new ChangeListener() {// 添加监听
	@Override
	public void stateChanged(ChangeEvent arg0) {
		int selectedIndex = tabbedPane.getSelectedIndex();// 获得被选中选项卡的索引
		String title = tabbedPane.getTitleAt(selectedIndex);// 获得指定索引的选项卡标签
		System.out.println(title);
	}
});
tabbedPane.addTab("方式1", null, selectionTab(), "选择表生成代码");// 将标签组件添加到选项卡中,并且要求有提示.selectionTab()是一個組件,可以写成new JLabel("选项卡第一个界面");
tabbedPane.addTab("方式2", null, customTab(), "自定义表生成代码");// 将标签组件添加到选项卡中,并且要求有提示。customTab()替换成new JLabel("选项卡第二个界面");
tabbedPane.setSelectedIndex(0); // 设置索引为1的选项卡被选中

在这里插入图片描述

1.9 警报弹窗

在这里插入图片描述

2.Swing的布局
1.1 流式布局

FlowLayout(),默认情况下为流式布局,居中对齐。

FlowLayout(int align, int hgap, int vgap);hgap和vgap可以省去不写。align表示对齐方式:0- 每一行组件将按照左对齐排列;1-每一行组件将按照居中对齐排列;2-每一行组件将按照右对齐排列
1.2 网格布局

GridLayout (), GridLayout(int rows, int cols, int hgap, int vgap),创建具有指定行数、列数的网格布局,hgap指定网格之间的水平间距,vgap指定网格之间垂直间距。

jPanel.setLayout(new GridLayout(2, 1,10,10));
jPanelAboutDatabase.setLayout(new GridLayout(6, 1));
1.3 边界布局

BorderLayout(),使其符合下列五个区域:北、南、东、西、中,通过相应的常量进行标识:NORTH、SOUTH、EAST、WEST、CENTER。

猜你喜欢

转载自blog.csdn.net/qq_37971615/article/details/86605831