Java 改变按钮外观、形状,最主要的是如何在上面加图片
重写protectedvoidpaintComponent(Graphicsg)方法时在按钮上加图片...
重写protected void paintComponent(Graphics g)方法时在按钮上加图片
展开
1个回答
展开全部
/**下面中因为初始时就调用了 paintComponent方法 ,所以你一运行就会看到图片.
你会重绘时才看到,就自己编码设置下..
*/
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
public class ChangeButton extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChangeButton frame = new ChangeButton();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChangeButton() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
BPanel panel = new BPanel();
contentPane.add(panel, BorderLayout.CENTER);
}
}
class BPanel extends JPanel{
JButton button = new JButton("New button");
public BPanel(){
this.setLayout(null);
button.setBounds(131, 80, 160, 160);
this.add(button);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
button.setIcon(new ImageIcon("json.jpg"));
//下面几种还有其它的设置效果请你参照 JDK_API文档吧...
// button.setIconTextGap(3);设置文字与图标之间的间隔
//button.setBorder(border);//设置按键的边框
//button.setBackground(bg);//
//button.setForeground(fg);//
//button.setAlignmentY(alignmentY);//
}
}
你会重绘时才看到,就自己编码设置下..
*/
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
public class ChangeButton extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChangeButton frame = new ChangeButton();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChangeButton() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
BPanel panel = new BPanel();
contentPane.add(panel, BorderLayout.CENTER);
}
}
class BPanel extends JPanel{
JButton button = new JButton("New button");
public BPanel(){
this.setLayout(null);
button.setBounds(131, 80, 160, 160);
this.add(button);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
button.setIcon(new ImageIcon("json.jpg"));
//下面几种还有其它的设置效果请你参照 JDK_API文档吧...
// button.setIconTextGap(3);设置文字与图标之间的间隔
//button.setBorder(border);//设置按键的边框
//button.setBackground(bg);//
//button.setForeground(fg);//
//button.setAlignmentY(alignmentY);//
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询