【Java】swing实例demo 弹出框、下拉框、事件监听

swing基本用法 完整demo 可直接运行

package com.zzz.component;

import java.awt.Container;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class SwingDemo implements ActionListener{

	static String[] comboBoxStrArr = new String[]{"Aaaaaaa", "Bbbbbbb"};
	
	JFrame frame = new JFrame("弹框标题");
	JTabbedPane tabPane = new JTabbedPane();
	Container _ONE = new Container();
	JLabel label1 = new JLabel("标题1:");
	JLabel label2 = new JLabel("标题2:");
	JLabel label3 = new JLabel("标题3:");
	JLabel label4 = new JLabel("标题4:");
	JTextField field1 = new JTextField();
	final JComboBox<String> comboBox = new JComboBox<String>(comboBoxStrArr);
	JTextField field3 = new JTextField();
	JTextField field4 = new JTextField();
	JButton button = new JButton("点击触发事件");
	
	public SwingDemo () {
		int labelX = 10, labelY = 10, labelW = 70, fieldX = 80, fieldY = 10, fieldW = 240, height = 20, hSpace = 30;
		int frameW = 350, frameH = 230, btnW = 120;
		double a = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double b = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame.setLocation(new Point((int) (a / 2) - 150, (int) (b / 2) - 150));// 设定窗口出现位置
		frame.setSize(frameW, frameH);// 设定窗口大小
		frame.setContentPane(tabPane);// 设置布局
		label1.setBounds(labelX, labelY, labelW, height);
		label2.setBounds(labelX, labelY = labelY + hSpace, labelW, height);
		label3.setBounds(labelX, labelY = labelY + hSpace, labelW, height);
		label4.setBounds(labelX, labelY = labelY + hSpace, labelW, height);
		field1.setBounds(fieldX, fieldY, fieldW, height);
		comboBox.setBounds(fieldX, fieldY = fieldY + hSpace, fieldW, height);
		field3.setBounds(fieldX, fieldY = fieldY + hSpace, fieldW, height);
		field4.setBounds(fieldX, fieldY = fieldY + hSpace, fieldW, height);
		button.setBounds((frameW-btnW)/2, fieldY = fieldY + hSpace, btnW, height);
		button.addActionListener(this);
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					String value = comboBox.getSelectedItem().toString();
					System.out.println(value);
				}
			}
		});
		_ONE.add(label1);
		_ONE.add(label2);
		_ONE.add(label3);
		_ONE.add(label4);
		_ONE.add(field1);
		_ONE.add(comboBox);
		_ONE.add(field3);
		_ONE.add(field4);
		_ONE.add(button);
		frame.setVisible(true);// 窗口可见
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 使能关闭窗口,结束程序
		tabPane.add("会话1", _ONE);
		field1.setText("文本框1内容设置");
		field3.setText("文本框2内容设置");
		field4.setText("文本框3内容设置");
	}
	
	//监听事件
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(button)) {
			String field1Value = field1.getText();
			String field2Value = field3.getText();
			String field3Value = field4.getText();
			System.out.println(field1Value);
			System.out.println(field2Value);
			System.out.println(field3Value);
		}
		//new SelectTableToGenerageDialog();触发下一个弹框
		frame.dispose();//关闭弹框
	}
	
	public static void main(String[] args) {
		new SwingDemo();
	}
}

效果图如下所示:
【Java】swing实例demo 弹出框、下拉框、事件监听_第1张图片

你可能感兴趣的:(swing)