学习笔记1之用swing在面板上创建按钮几种方法的对比

方式1:

package actionPerform;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonTest {

	public static void main(String[] args) {
		
		EventQueue.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				ButtonFrame2 buttonFrame = new ButtonFrame2();
				buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				buttonFrame.setVisible(true);
				
			}
		});

	}

}
/**
 * 创建框架
 * @Description:
 * @author liuyunqiang
 * @date:   2018年9月5日 下午8:34:28
 */
class ButtonFrame extends JFrame{

	private static final long serialVersionUID = 1L;
	private JPanel buttonPanel;
	public ButtonFrame(){
		setTitle("ButtonTest");
		setSize(300, 200);
		//创建按钮
		JButton yButton = new JButton("Yellow");
		JButton bButton = new JButton("Blue");
		JButton rButton = new JButton("Red");
		//将按钮加入面板
		buttonPanel = new JPanel();
		buttonPanel.add(yButton);
		buttonPanel.add(bButton);
		buttonPanel.add(rButton);
		//将面板加入框架
		add(buttonPanel);
		
		//实例化监听器
		ColorAction yAction = new ColorAction(Color.YELLOW);
		ColorAction bAction = new ColorAction(Color.BLUE);
		ColorAction rAction = new ColorAction(Color.RED);
		//将按钮加入监听器
		yButton.addActionListener(yAction);
		bButton.addActionListener(bAction);
		rButton.addActionListener(rAction);
	}
	/**
	 * 创建监听器
	 * @Description:
	 * @author liuyunqiang
	 * @date:   2018年9月5日 下午8:33:26
	 */
	private class ColorAction implements ActionListener{

		private Color backgroundColor;
		public ColorAction(Color c){
			backgroundColor = c;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			buttonPanel.setBackground(backgroundColor);
			
		}
		
	}
}

result:点击按钮图版颜色随之变化;

步骤:

1、创建框架;

2、创建按钮;

3、将按钮加入面板中;

4、将面板加入框架中;

总结:相当于图层叠加;

方法2、用匿名内部类优化,

package actionPerform;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonTest2 {

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				ButtonFrame2 buttonFrame = new ButtonFrame2();
				buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				buttonFrame.setVisible(true);

			}
		});

	}

}

/**
 * 创建框架 @Description:
 * 
 * @author liuyunqiang
 * @date: 2018年9月5日 下午8:34:28
 */
class ButtonFrame2 extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel buttonPanel;
	public ButtonFrame2() {
		setTitle("ButtonTest");
		setSize(300, 200);
        //此处为创建1个面板,如果将面板放入makeButton中,就是创建三个面板,,最终叠加只会
        //保留最后一个按钮
		buttonPanel = new JPanel();
		makeButton("Yellow", Color.YELLOW);
		makeButton("Blue", Color.BLUE);
		makeButton("Red", Color.RED);
	}

	private void makeButton(String name, Color backgroundColor) {
		JButton button = new JButton(name);
		buttonPanel.add(button);
		add(buttonPanel);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Object source = e.getSource();
				System.out.println();
				buttonPanel.setBackground(backgroundColor);
			}

		});
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值