码迷,mamicode.com
首页 > 其他好文 > 详细

修改面板的背景颜色

时间:2015-06-16 16:09:50      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

package button;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class buttonFrame extends JFrame{
	private JPanel buttonPanel;
	private static final int D_width=300;
	private static final int D_height=200;
	public static void main(String args[]){
		JFrame frame = new buttonFrame();
		frame.setTitle("buttonFrame");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public buttonFrame(){
		setSize(D_width,D_height);
		
		JButton yellowButton = new JButton("Yellow");
		JButton blueButton = new JButton("Blue");
		JButton redButton = new JButton("Red");
		
		buttonPanel = new JPanel();
		
		buttonPanel.add(yellowButton);
		buttonPanel.add(blueButton);
		buttonPanel.add(redButton);
		
		add(buttonPanel);
		
		ColorAction yellowAction = new ColorAction(Color.YELLOW);
		ColorAction blueAction = new ColorAction(Color.BLUE);
		ColorAction redAction = new ColorAction(Color.RED);
		
		yellowButton.addActionListener(yellowAction);
		blueButton.addActionListener(blueAction);
		redButton.addActionListener(redAction);
		
	}
	
	private class ColorAction implements ActionListener{
		private Color backgroundColor;
		public ColorAction(Color c){
			backgroundColor=c;
		}
		public void actionPerformed(ActionEvent event){
			buttonPanel.setBackground(backgroundColor);
		}
	}
}

  

修改面板的背景颜色

标签:

原文地址:http://www.cnblogs.com/hugh0510/p/4580825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!