关闭

Welcome to JAVA!之Swing common features

标签: java博客java学习面向对象swing
953人阅读 评论(0) 收藏 举报
分类:

12.8 Display a frame that contains six labels.Set the background of the labels to white.Set the foreground of the labels to black,blue,cyan,green,magenta,and orange,respectively,Set the border of each label to a line border with the yellow color,Set the font of each label to TimesRoman,bold,and 20 pixels.Set the text and tool tip text of each label to the name of its foreground color.

代码如下:

package com.ytu.gui;

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

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class SwingCommFeature extends JFrame {
	public SwingCommFeature(){
		super();
		setSize(600,300);
		setLocationRelativeTo(null);
		setLayout(new GridLayout(2,3,5,10));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		JLabel[] jlbs=new JLabel[6];
		LineBorder border = new LineBorder(Color.yellow,2);
		for (int i=0;i<jlbs.length;i++){
			
			jlbs[i]=new JLabel();
			jlbs[i].setBorder(border);
			jlbs[i].setBackground(Color.white);
			jlbs[i].setFont(new Font("TimesRoman",Font.BOLD,30));
			add(jlbs[i]);
			
		}
		jlbs[1].setText("black");
		jlbs[1].setForeground(Color.black);
		jlbs[1].setToolTipText("black");
		jlbs[2].setText("blue");
		jlbs[2].setForeground(Color.blue);
		jlbs[2].setToolTipText("blue");
		jlbs[3].setText("cyan");
		jlbs[3].setForeground(Color.cyan);
		jlbs[3].setToolTipText("cyan");
		jlbs[4].setText("green");
		jlbs[4].setForeground(Color.green);
		jlbs[4].setToolTipText("green");
		jlbs[5].setText("magenta");
		jlbs[5].setForeground(Color.magenta);
		jlbs[5].setToolTipText("magenta");
		jlbs[0].setText("orange");
		jlbs[0].setForeground(Color.orange);
		jlbs[0].setToolTipText("orange");
		setVisible(true);
	}
	
	public static void main(String[] args)
	{
		new SwingCommFeature();
	}
}
运行结果:


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:435659次
    • 积分:10809
    • 等级:
    • 排名:第1549名
    • 原创:559篇
    • 转载:3篇
    • 译文:0篇
    • 评论:399条
    最新评论