graphic java_Java graphic(color and font)

q2ILG.png

See comments in code for details.

import java.awt.*;

import javax.swing.*;

public class First {

public static void main(String[] args) {

Color purpleColor = new Color(120, 78, 140);

Color pinkColor = new Color(120, 13, 14);

String a ="HELLO";

// CENTER_BASELINE should not be included in a Font style!

//Font f1 = new Font(a, Font.BOLD + Font.ITALIC + Font.CENTER_BASELINE, 25);

Font f1 = new Font(a, Font.BOLD + Font.ITALIC, 25);

JFrame f = new JFrame();

// default layout for a frame is BorderLayout

// a component added to BL with no constraint will end up in CENTER

// a component in center will be stretched to available size

f.setLayout(new GridBagLayout());

f.setSize(300,200);

JButton b = new JButton(a);

// Must set the font!

b.setFont(f1);

// will be (rightly) ignored by layout. Use setMargin(..)

//b.setSize(400,200);

b.setMargin(new Insets(20,20,20,20));

// Many calls relevant to the content pane of a frame were configured

// to pass directly through to it. The BG color was NOT one of them!

//f.setBackground(pinkColor);

f.getContentPane().setBackground(pinkColor);

b.setBackground(purpleColor);

// AFAIR, necessary for some PLAFs

b.setContentAreaFilled(false);

b.setOpaque(true);

f.add(b);

// Good practice

f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

f.setVisible(true);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值