这个是用图片的
package sample;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private JPanel jPanel = null;
/**
* This is the default constructor
*/
public Frame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(513, 309);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJPanel(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(34, 27, 150, 44));
jButton.setText("愛我老婆");
jButton.setBorder(null);
//下面这句设置背景颜色必须要否则无法实现背景透明
jButton.setBackground(Color.BLACK);
jButton.setOpaque(false);
jButton.setForeground(Color.red);
jButton.setFont(new Font("",Font.BOLD,24));
}
return jButton;
}
class getpane extends JPanel {
private static final long serialVersionUID = 1L;
int width = 0, hight = 0;
String imgpath = "";
public getpane(int width, int hight, String imgpath) {
this.width = width;
this.hight = hight;
this.imgpath = imgpath;
this.setOpaque(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon = new ImageIcon(imgpath);
Image img = icon.getImage();
g.drawImage(img, 0, 0, width, hight, this);
}
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
String imgpath = "src/game/image/good02.jpg";
jPanel = new getpane(this.getWidth(), this.getHeight(), imgpath);
// jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(13, 8, 472, 251));
jPanel.add(getJButton(), null);
}
return jPanel;
}
}
2010年6月04日 12:13