`
chenzhaomin
  • 浏览: 10347 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

第四、五课时 Swing界面布局

    博客分类:
  • Java
阅读更多


Swing界面布局

Swing里面常用的布局有这么三种
1.BorderLayout
边界布局,JFrame默认布局,分为5个区域(东南西北中)
其中南北只能调节高度,东西只能调节宽度,中间大小根据东南西北确定

2.FlowLayout
流体布局,JPanel默认布局,组件位置会随着窗口大小改变而改变

3.GridLayout
网格布局,顾名思义,就是像网格那样分块

当我们创建一个窗口时,我们要清楚把不同的组件放到对应的区域上去

 

总之,要实现一个界面,首先创建一个容器,然后在容器里放不同的组件,如果要实现相应的功能,则给组件添加事件即可。

实例:
1.实现QQ登陆界面
注意:控制组件大小不能使用setSize()方法,应该使用setPerferredSize();
JFrame分为三层:
BootPane//最底层
LayerPane//中间层
ContentPane//最顶层
在实例中,一般情况下,我们把组件加入ContentPane中,所以我们要把背景图片加入到LayerPane中,然后设置面板透明,就可以实现在JFrame里面添加背景图片了.

package cn.czm0709;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.*;
/**
 * QQ登陆界面
 * @author chenzhaomin
 *
 */
public class QQ extends JFrame {

	private JPanel panel_n;
	private JPanel panel_s;
	private JPanel panel_w;
	private JPanel panel_c;
	private JButton close;
	private JButton mini;
	private JButton login;
	private JLabel background;
	private JComboBox account;
	private JPasswordField password;
	private JLabel jl1;
	private JLabel jl2;
	private JLabel head;
	private JCheckBox remember;
	private JCheckBox auto;
	private Point p;

	public QQ() {
		/**
		 * 框架设置
		 */
		setSize(400, 300);
		setResizable(false);//禁止改变大小
		setLocationRelativeTo(null);
		setUndecorated(true);// 无装饰
/**
 * 背景添加
 */
		background = new JLabel(new ImageIcon(
				"E:/TDDOWNLOAD/LanJie/src/cn/czm0709/5.jpg"));//创建一个带图片的标签
		background.setBounds(0, 0, this.getWidth(), this.getHeight());//设置大小使其匹配框架
		background.setBorder(BorderFactory.createRaisedBevelBorder());//设置边框
		getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));//把标签加入LayerPane层中
		JPanel imagePanel = (JPanel) this.getContentPane();
		imagePanel.setOpaque(false);//设置ContentPane不透明
		
		/**
		 * 登陆按钮创建于添加
		 */
		ImageIcon image3 = new ImageIcon(
				"E:/TDDOWNLOAD/LanJie/src/cn/czm0709/3.jpg");
		login = new JButton(image3);
		login.setPreferredSize(new Dimension(image3.getIconWidth(), image3
				.getIconHeight()));
		panel_s = new JPanel();
		panel_s.setOpaque(false);
		panel_s.add(login);
		add(panel_s, BorderLayout.SOUTH);
/**
 * 关闭按钮与最小化按钮添加
 */
		ImageIcon image1 = new ImageIcon(
				"E:/TDDOWNLOAD/LanJie/src/cn/czm0709/1.jpg");
		close = new JButton(image1);
		close.setPreferredSize(new Dimension(image1.getIconWidth(), image1
				.getIconHeight()));
		close.addActionListener(new ActionListener() {   //添加监听器,当点击关闭按钮时退出
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(ABORT);
			}

		});
		
		ImageIcon image4 = new ImageIcon(
				"E:/TDDOWNLOAD/LanJie/src/cn/czm0709/4.jpg");
		mini = new JButton(image4);
		mini.setPreferredSize(new Dimension(image4.getIconWidth(), image4
				.getIconHeight()));
		mini.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {//添加监听器,当点击最小化按钮时最小化
				// TODO Auto-generated method stub
				setExtendedState(1);
			}

		});
		panel_n = new JPanel();
		panel_n.setOpaque(false);
		panel_n.setLayout(new FlowLayout(FlowLayout.RIGHT, 2, 2));//重设流体布局
		panel_n.add(mini);
		panel_n.add(close);
		panel_n.setPreferredSize(new Dimension(400, 150));//设置面板大小
		add(panel_n, BorderLayout.NORTH);

		/**
		 * 头像添加
		 */
		ImageIcon image2 = new ImageIcon(
				"E:/TDDOWNLOAD/LanJie/src/cn/czm0709/2.jpg");
		head = new JLabel(image2);
		head.setPreferredSize(new Dimension(image2.getIconWidth(), image2
				.getIconHeight()));
		panel_w = new JPanel();
		panel_w.setOpaque(false);
		panel_w.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
		panel_w.setPreferredSize(new Dimension(100, 300));
		panel_w.add(head);
		add(panel_w, BorderLayout.WEST);

		/**
		 * 账号密码框添加以及复选框添加
		 */
		panel_c = new JPanel();
		panel_c.setOpaque(false);
		panel_c.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
		account = new JComboBox();
		account.setEditable(true);
		account.addItem("88485740");
		account.setPreferredSize(new Dimension(200, 28));
		panel_c.add(account);
		jl1 = new JLabel("注册账号");
		panel_c.add(jl1);//添加账号输入框

		password = new JPasswordField(18);
		panel_c.add(password);//添加密码框

		jl2 = new JLabel("忘记密码");
		panel_c.add(jl2);//添加标签

		remember = new JCheckBox("记住密码");
		remember.setForeground(Color.BLACK);
		remember.setOpaque(false);
		panel_c.add(remember);//复选框添加

		auto = new JCheckBox("自动登录");
		auto.setOpaque(false);
		auto.setForeground(Color.BLACK);
		panel_c.add(auto);//复选框添加
		add(panel_c, BorderLayout.CENTER);
		
		/**
		 * 增加鼠标监听器,实现窗口拖动
		 */
		addMouseMotionListener(new MouseAdapter(){
			public void mouseMoved(MouseEvent e){
				p = new Point(e.getX() +background.getX(), e.getY() + background.getY());
				if((p.x>355 && p.x<400)&&(p.y>0&&p.y<25)){
					close.setIcon(new ImageIcon("E:/TDDOWNLOAD/LanJie/src/cn/czm0709/6.jpg"));
					panel_n.updateUI();
				}else{
					close.setIcon(new ImageIcon("E:/TDDOWNLOAD/LanJie/src/cn/czm0709/1.jpg"));
					panel_n.updateUI();
				}
			}
			
			public void mouseDragged(MouseEvent e) {
				setLocation(e.getLocationOnScreen().x-p.x, e.getLocationOnScreen().y-p.y);
			}
		});
		

	}
/**
 * 主函数
 * @param args
 */
	public static void main(String[] args) {
		QQ q = new QQ();
		q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		q.setVisible(true);

	}
}

 

2计算机界面
本例中利用GridLayout,读者也可尝试使用FlowLayout

package cn.czm0709;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * 计算机
 * 
 * @author chenzhaomin
 * 
 */
public class Calculator extends JFrame {

	private JTextField display;
	private JPanel buttonPanel;
	private boolean start;
	private double result;
	private String lastCommand;

	public Calculator() {
		/**
		 * 框架设置
		 */
		setTitle("计算机");
		setSize(200, 250);
		setLocationRelativeTo(null); // 居中
		setResizable(false); // 禁止调整大小

/**
 * 显示屏设置
 */
		display = new JTextField("0");
		display.setEditable(false);//设置不可编辑
		display.setFont(new Font("Serif", Font.PLAIN, 20));// 设置字体
		display.setPreferredSize(new Dimension(20, 50));// 设置显示大小

		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(4, 4));//设置网格布局
		
		start = true;
		result = 0;
		lastCommand = "=";
		
		NumActionListener listener1 = new NumActionListener();
		CommandAction listener2 = new CommandAction();
		// 加入按钮与监听器
		addButton(new JButton("7"), listener1);
		addButton(new JButton("8"), listener1);
		addButton(new JButton("9"), listener1);
		addButton(new JButton("+"), listener2);

		addButton(new JButton("4"), listener1);
		addButton(new JButton("5"), listener1);
		addButton(new JButton("6"), listener1);
		addButton(new JButton("-"), listener2);

		addButton(new JButton("1"), listener1);
		addButton(new JButton("2"), listener1);
		addButton(new JButton("3"), listener1);
		addButton(new JButton("*"), listener2);

		addButton(new JButton("0"), listener1);
		addButton(new JButton("."), listener1);
		addButton(new JButton("="), listener2);
		addButton(new JButton("/"), listener2);
		add(buttonPanel, BorderLayout.CENTER);
		add(display, BorderLayout.NORTH);

	}
/**
 * 定义按钮添加方法
 * @param jb
 * @param listener
 */
	public void addButton(JButton jb, ActionListener listener) {

			jb.addActionListener(listener);
			buttonPanel.add(jb);
	}
/**
 * 
 * 定义数字监听器
 *
 */
	private class NumActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String num = e.getActionCommand();
			if (start) {
				display.setText("");
				start = false;
			}
				display.setText(display.getText() + num);
		}

	}
/**
 * 
 *定义运算符监听器
 *
 */
	private class CommandAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			if (start) {
				if (command.equals("-")) {
					display.setText("-");
					start = false;
				} else {
					lastCommand = command;
				}
			} else {
				calculate(Double.parseDouble(display.getText()));
				lastCommand = command;
				start = true;
			}
		}
	}
/**
 * 
 * 定义计算方法
 */
	public void calculate(double x) {
		if (lastCommand.equals("+")) {
			result += x;
		} else if (lastCommand.equals("-")) {
			result -= x;
		} else if (lastCommand.equals("*")) {
			result *= x;
		} else if (lastCommand.equals("/")) {
			result /= x;
		} else if (lastCommand.equals("=")) {
			result = x;
		}
		display.setText("" + result);
	}
	/**
	 * 主函数
	 * @param args
	 */
	public static void main(String[] args){
		Calculator c = new Calculator();
		c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		c.setVisible(true);
	}
}

 



 

  • 大小: 27.1 KB
  • 大小: 11.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics