62,628
社区成员
发帖
与我相关
我的任务
分享import java.awt.Image;
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;
public class Demo {
private static JButton button=new JButton();
private static Image bomb=Toolkit.getDefaultToolkit().getImage("src/image/1.jpg");
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(200, 200, 200, 200);
jf.setLayout(null);
button.setBounds(50, 50, 23, 23);
jf.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
button.setEnabled(false);
button.setDisabledIcon(new ImageIcon(bomb));
}
});
jf.setVisible(true);
}
}
public class Demo {
private static JButton button = new JButton();
private static Image bomb1 = Toolkit.getDefaultToolkit().getImage("src/images/logo1.jpg");
private static Image bomb2 = Toolkit.getDefaultToolkit().getImage("src/images/logo2.jpg");
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(200, 200, 200, 200);
jf.setLayout(null);
button.setBounds(50, 50, 80, 30);
button.setIcon(new ImageIcon(bomb1));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
button.setEnabled(false);
button.setDisabledIcon(new ImageIcon(bomb2));
button.updateUI();
}
});
jf.add(button);
jf.setVisible(true);
}
}
[/quote]
谢谢,问题解决了,请问可以解释一下原理吗?
public class Demo {
private static JButton button = new JButton();
private static Image bomb1 = Toolkit.getDefaultToolkit().getImage("src/images/logo1.jpg");
private static Image bomb2 = Toolkit.getDefaultToolkit().getImage("src/images/logo2.jpg");
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(200, 200, 200, 200);
jf.setLayout(null);
button.setBounds(50, 50, 80, 30);
button.setIcon(new ImageIcon(bomb1));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
button.setEnabled(false);
button.setDisabledIcon(new ImageIcon(bomb2));
button.updateUI();
}
});
jf.add(button);
jf.setVisible(true);
}
}
import javax.swing.*;
public class Demo extends JFrame{
// 定义组件
private JPanel jpl;
private JLabel bg;
public Demo(){
// 选择图片文件
bg = new JLabel(new ImageIcon("测试/src/image/1.jpg"));
// 初始化
jpl = new JPanel();
jpl = new JPanel();
jpl.add(bg);
this.add(jpl);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出关闭JFrame
this.setSize(400, 300);//设置窗口大小
this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Demo();
}
}