码迷,mamicode.com
首页 > 编程语言 > 详细

【JAVA语言程序设计基础篇】--Swing GUI组件的公共特性

时间:2016-08-03 13:43:37      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

</pre><pre name="code" class="html">package chapter12;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.LineBorder;//线框
import javax.swing.border.TitledBorder;//标题框

@SuppressWarnings("serial")
public class TestSwingCommonFeatures extends JFrame{
	public TestSwingCommonFeatures(){
		
		JPanel p1 = new JPanel (new FlowLayout(FlowLayout.LEFT,2,2));
		JButton jbtleft = new JButton("left");
		JButton jbtcenter = new JButton("CENTER");
		JButton jbtright = new JButton("right");
		
		jbtleft.setBackground(Color.white);
		jbtcenter.setForeground(Color.green);
		jbtright.setToolTipText("this is the right button");
		
		p1.add(jbtleft);
		p1.add(jbtcenter);
		p1.add(jbtright);
		p1.setBorder(new TitledBorder("three buttons"));
		//-------------------------------------//
		Font largeFont =new Font ("TimesRoman",Font.BOLD,20);//字体
		
		JPanel p2 = new JPanel (new FlowLayout(FlowLayout.LEFT,5,5));
		JButton jbtred = new JButton ("red");
		JButton jbtorange = new JButton("orange");
		jbtred.setFont(largeFont);
		jbtred.setBackground(Color.red);
		jbtorange.setForeground(Color.orange);
		jbtorange.setToolTipText("this is a orange button");
		
		p2.add(jbtred);
		p2.add(jbtorange);
		p2.setBorder(new LineBorder(Color.green,4));//被下一行覆盖
		p2.setBorder(new TitledBorder("a"));
		p2.setLayout(new GridLayout(1,2,1,10));
		
		
		setLayout(new FlowLayout(FlowLayout.LEFT,30,30));
		setLayout(new GridLayout(1,2,5,10));//下一行有一个新的setlayout 这一样的setlayout被覆盖,就不显示了
		add(p1);
		add(p2);
	}
	
	public static void main(String[] args) {
		JFrame frame = new TestSwingCommonFeatures();
		frame.setTitle("test swing common features");
		frame.setSize(500, 150);
		frame.setLocationRelativeTo(null);//放中间
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}

展示

Font 字体

Color 颜色

Border 框



技术分享

【JAVA语言程序设计基础篇】--Swing GUI组件的公共特性

标签:

原文地址:http://blog.csdn.net/qq_24653023/article/details/52102692

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