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();
}
}