java 添加图片背景_java 为背景添加图片

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

class MyButton extends JButton

{

// private static final long serialVersionUID = 1965063150601339314L;

public MyButton(String text)

{

super(text);

setOpaque(false);

setContentAreaFilled(false); // 这一句非常重要, 否则父类还会绘制按钮的区域.

}

@Override

protected void paintComponent(Graphics g)

{

int width = this.getWidth();

int height = this.getHeight();

Graphics2D g2d = (Graphics2D) g;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿

g2d.setColor(new Color(109,171,226));

g2d.fillRoundRect(0,0, width,height,20,20);

super.paintComponent(g); // 最后调用这个方法, 让父类绘制在按钮上绘制文字.

}

}

public class HomeScreen extends JFrame

{

JPanel imagePanel;

ImageIcon icon;

ImageIcon background;

JLabel topLabel;

JButton buttonEnter,buttonToday,buttonWeek,ButtonLogout,ButtonHistory;

public HomeScreen()

{

topLabel=new JLabel();

topLabel.setIcon(new ImageIcon("background.png"));

add(topLabel);

topLabel.setLayout(null);

//setLocationRelativeTo(null);

buttonEnter = new MyButton("请假信息录入");

buttonEnter.setFocusable(false);

buttonEnter.setBorderPainted(false);

buttonEnter.setSize(200,40);

buttonEnter.setLocation(300, 150);

buttonEnter.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.out.println("请假信息录入");

}

});

topLabel.add(buttonEnter);

buttonToday = new MyButton("本日请假记录");

buttonToday.setFocusable(false);

buttonToday.setBorderPainted(false);

buttonToday.setSize(200,40);

buttonToday.setLocation(300, 210);

buttonToday.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.out.println("本日请假记录");

}

});

topLabel.add(buttonToday);

buttonWeek = new MyButton("本周请假记录");

buttonWeek.setFocusable(false);

buttonWeek.setBorderPainted(false);

buttonWeek.setSize(200,40);

buttonWeek.setLocation(300, 270);

buttonWeek.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.out.println("本周请假记录");

}

});

topLabel.add(buttonWeek);

ButtonHistory = new MyButton("历史请假记录");

ButtonHistory.setFocusable(false);

ButtonHistory.setBorderPainted(false);

ButtonHistory.setSize(200,40);

ButtonHistory.setLocation(300, 330);

ButtonHistory.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.out.println("历史请假记录");

}

});

topLabel.add(ButtonHistory);

ButtonLogout = new MyButton("注销请假记录");

ButtonLogout.setFocusable(false);

ButtonLogout.setBorderPainted(false);

ButtonLogout.setSize(200,40);

ButtonLogout.setLocation(300, 390);

ButtonLogout.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

System.out.println("注销请假记录");

}

});

topLabel.add(ButtonLogout);

this.setTitle("添加背景图片");

icon=new ImageIcon("icon.png");

this.setIconImage(icon.getImage());

this.setSize(800,600);

Dimension screenSize= Toolkit.getDefaultToolkit().getScreenSize();

int x=(screenSize.width-this.getWidth())/2;

int y=(screenSize.height-this.getHeight())/2;

this.setLocation(x,y);

setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args)

{

new HomeScreen();

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值