Welcome to JAVA!之Swing common features

来源:互联网 发布:淘宝平台运营思路 编辑:程序博客网 时间:2024/06/02 05:48

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
原创粉丝点击