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

📄 chatdialog.java

📁 布局管理器示例
💻 JAVA
字号:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


/**
 * 
 * @author Coffey
 * 
 */
public class ChatDialog extends JFrame implements ActionListener, CharInterface {

	Dimension faceSize; // 框架的大小

	/* 容器及布局管理器对象 */
	Container con; // 容器对象
	GridBagLayout gbl; // 网格包布局管理器对象

	/* 所使用的标题对象 */
	JLabel topLabel; // 头标题
	JLabel fileTypeLabel; // 文件类型标题
	JLabel ipInfoTypeLabel; // 主机IP信息标题

	/* 所使用的面板对象 */
	JPanel leftPanel; // 左则的面板
	JPanel topPanel; // 左上的面板
	JPanel operPanel; // 操作的面板
	JPanel downPanel; // 左下的面板
	JPanel rightPanel; // 右则的面板
	JPanel listPanel; // 主机IP信息显示面板
	JPanel buttonPanel; // 按钮显示面板

	/* 所使用的按钮对象 */
	JButton sendButton; // 发送的按钮
	JButton saveButton; // 保存的按钮
	JButton newButton; // 刷新的按钮
	JButton enterButton; // 确定的按钮

	/* 所使用的文本框对象 */
	JTextArea historyText; // 信息记录框
	JTextArea sendText; // 信息发送框

	/* 所使用的其它对象 */
	JList ipList;// 主机IP信息List
	JComboBox fileTypeComboBox; // 文件类型下拉框

	/**
	 * 构造函数
	 */
	public ChatDialog() {

		// 初始化函数
		init();

		// 设置框架的大小
		this.setSize(faceSize);

		// 添加框架的关闭事件处理
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 设置运行时窗口的位置
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((int) (screenSize.width - faceSize.getWidth()) / 2,
				(int) (screenSize.height - faceSize.getHeight()) / 2);

		// 设置标题
		this.setTitle(strFrameTitle);

		// 是否最大化
		this.setResizable(true);

		// 是否可视化
		this.setVisible(true);
	}

	/**
	 * 设置左上面板网格包布局管理器
	 */
	private GridBagConstraints getTopGridBagConstraints() {
		// 组件约束对象
		GridBagConstraints gbc = new GridBagConstraints();
		// 在水平方向和垂直方向上同时调整组件大小
		gbc.fill = GridBagConstraints.BOTH;
		// 分布额外的水平空间
		gbc.weightx = 3.0;
		// 分布额外的垂直空间
		gbc.weighty = 3.0;
		// 显示区域的一行中的单元格数
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		// 显示区域的一列中的单元格数
		gbc.gridheight = 1;
		// 返回约束对象
		return gbc;
	}

	/**
	 * 设置左下面板网格包布局管理器
	 */
	private GridBagConstraints getDownGridBagConstraints() {
		// 组件约束对象
		GridBagConstraints gbc = new GridBagConstraints();
		// 在水平方向和垂直方向上同时调整组件大小
		gbc.fill = GridBagConstraints.BOTH;
		// 分布额外的水平空间
		gbc.weightx = 1.0;
		// 分布额外的垂直空间
		gbc.weighty = 1.0;
		// 显示区域的一行中的单元格数
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		// 显示区域的一列中的单元格数
		gbc.gridheight = 1;
		// 返回约束对象
		return gbc;
	}

	/**
	 * 获得指定的滚动条
	 * 
	 * @param cp
	 * @return
	 */
	private JScrollPane getScrollPane(Component cp) {
		JScrollPane scrollPane = new JScrollPane(cp,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.revalidate();
		return scrollPane;
	}

	/**
	 * 程序初始化函数
	 */
	public void init() {

		/* 设置框架的大小 */
		faceSize = new Dimension(faceSizeX, faceSizeY);

		/* 获得当前对象Pane */
		con = this.getContentPane();

		/* 初始化包布局管理器对象 */
		gbl = new GridBagLayout();

		/* 初始化标题对象 */
		topLabel = new JLabel(strTopLabel);
		fileTypeLabel = new JLabel(strFileTypeLabel);
		ipInfoTypeLabel = new JLabel(strIpInfoTypeLabel);

		/* 初始化面板对象 */
		leftPanel = new JPanel();
		topPanel = new JPanel();
		downPanel = new JPanel();
		operPanel = new JPanel();
		rightPanel = new JPanel();
		listPanel = new JPanel();
		buttonPanel = new JPanel();

		/* 设置面板布局管理器 */
		leftPanel.setLayout(gbl);
		topPanel.setLayout(new BorderLayout());
		downPanel.setLayout(new BorderLayout());
		rightPanel.setLayout(new BorderLayout());
		listPanel.setLayout(new BorderLayout());

		/* 初始化按钮对象 */
		saveButton = new JButton(strSaveButton);
		sendButton = new JButton(strSendButton);
		newButton = new JButton(strNewButton);
		enterButton = new JButton(strEnterButton);

		/* 初始化文本框对象 */
		historyText = new JTextArea();
		sendText = new JTextArea();

		/* 初始化其它对象 */
		ipList = new JList(TestArray);
		fileTypeComboBox = new JComboBox();
		fileTypeComboBox.addItem(strComboBoxItem_0);
		fileTypeComboBox.addItem(strComboBoxItem_1);
		fileTypeComboBox.addItem(strComboBoxItem_2);
		fileTypeComboBox.addItem(strComboBoxItem_3);
		fileTypeComboBox.setSelectedIndex(0);

		/* 左上的面板 */
		topPanel.add(topLabel, "North");// 添加头标题
		topPanel.add(historyText, "Center");// 添加记录框
		topPanel.add(this.getScrollPane(historyText));// 添加滚动条
		gbl.setConstraints(topPanel, this.getTopGridBagConstraints()); // 设置网格包布局管理器

		/* 左下的面板 */
		operPanel.add(fileTypeLabel); // 添加文件类型标题
		operPanel.add(fileTypeComboBox); // 添加文件类型下拉框
		operPanel.add(saveButton); // 添加保存按钮
		operPanel.add(sendButton); // 添加发送按钮
		downPanel.add(operPanel, "North"); // 添加操作面板
		downPanel.add(sendText, "Center"); // 添加发送框
		downPanel.add(this.getScrollPane(sendText));// 添加滚动条
		gbl.setConstraints(downPanel, this.getDownGridBagConstraints()); // 设置网格包布局管理器

		/* 右则的面板 */
		listPanel.add(ipInfoTypeLabel, "North"); // 添加主机IP信息标题
		listPanel.add(ipList, "Center"); // 主机IP信息List
		listPanel.add(this.getScrollPane(ipList));// 添加滚动条
		buttonPanel.add(newButton); // 添加刷新按钮
		buttonPanel.add(enterButton); // 添加确定的按钮
		listPanel.add(buttonPanel, "South"); // 添加按钮面板

		/* 添加到主左面板 */
		leftPanel.add(topPanel);
		leftPanel.add(downPanel);

		/* 添加到主右面板 */
		rightPanel.add(listPanel, "East");

		/* 合并所有面板 */
		con.add(leftPanel, "Center");
		con.add(rightPanel, "East");

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

}

⌨️ 快捷键说明

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