JavaSwing图形界面编程之JButton Image效果

来源:互联网 发布:淘宝助理官方版下载 编辑:程序博客网 时间:2024/05/17 23:50
package three.day.applet;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
public class JButtonImageDiffDemo01 extends JApplet implements ActionListener
{

Icon icon1=new ImageIcon("cross.gif");
Icon icon2=new ImageIcon("2.gif");
Icon icon3=new ImageIcon("not.gif");
Icon icon4=new ImageIcon("1.jpg");
JLabel lb=new JLabel("Java",icon4,JLabel.CENTER);
JButton bt;

@Override
public void init() {
Container cp=getContentPane();
bt=new JButton();
bt.setRolloverEnabled(true);
bt.setText("OK");
bt.setHorizontalTextPosition(JLabel.CENTER);
bt.setVerticalTextPosition(JLabel.BOTTOM);
cp.setLayout(new BorderLayout());
cp.add(lb,BorderLayout.NORTH);
cp.add(bt,BorderLayout.SOUTH);
bt.setIcon(icon1);
bt.setRolloverIcon(icon2);
bt.setPressedIcon(icon3);
bt.addActionListener(this);
}


public void actionPerformed(ActionEvent e)
{


if(e.getSource()==bt)
{
if(lb.getText()=="Hello")
lb.setText("你好");
else 
lb.setText("Hello");
if(bt.getText()=="ok")
bt.setText("确定");
else {
bt.setText("ok");
}
}
}
}
原创粉丝点击