/** */
/**
* @(#)c10_1.java
*
*
* @author
* @version 1.00 2008/4/8
*/
import java.awt. * ;
import javax.swing. * ;
import java.awt.event. * ;
public class c10_1 extends JApplet implements ActionListener ... {
Container cp = getContentPane();
Icon pa= new ImageIcon("g:/JavaDocs/a1.gif");
Icon pb= new ImageIcon("g:/JavaDocs/a2.gif");
Icon pc= new ImageIcon("g:/JavaDocs/a3.gif");
JButton bt=new JButton();
Icon pd=new ImageIcon("g:/JavaDocs/a4.gif");
JLabel lb=new JLabel("HELLO",pd,JLabel.CENTER);
/** *//** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() ...{
// TODO start asynchronous download of heavy resources
bt.setRolloverEnabled(true);
bt.setText("Click");
bt.setHorizontalTextPosition(JButton.CENTER);
bt.setVerticalTextPosition(JButton.BOTTOM);
lb.setHorizontalTextPosition(JLabel.CENTER);
lb.setVerticalTextPosition(JLabel.BOTTOM);
cp.add(lb,BorderLayout.NORTH);
cp.add(bt,BorderLayout.SOUTH);
bt.setIcon(pa);
bt.setRolloverIcon(pb);
bt.setPressedIcon(pc);
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()=="Click")
bt.setText("点击");
else
bt.setText("Click");}
}
}
* @(#)c10_1.java
*
*
* @author
* @version 1.00 2008/4/8
*/
import java.awt. * ;
import javax.swing. * ;
import java.awt.event. * ;
public class c10_1 extends JApplet implements ActionListener ... {
Container cp = getContentPane();
Icon pa= new ImageIcon("g:/JavaDocs/a1.gif");
Icon pb= new ImageIcon("g:/JavaDocs/a2.gif");
Icon pc= new ImageIcon("g:/JavaDocs/a3.gif");
JButton bt=new JButton();
Icon pd=new ImageIcon("g:/JavaDocs/a4.gif");
JLabel lb=new JLabel("HELLO",pd,JLabel.CENTER);
/** *//** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() ...{
// TODO start asynchronous download of heavy resources
bt.setRolloverEnabled(true);
bt.setText("Click");
bt.setHorizontalTextPosition(JButton.CENTER);
bt.setVerticalTextPosition(JButton.BOTTOM);
lb.setHorizontalTextPosition(JLabel.CENTER);
lb.setVerticalTextPosition(JLabel.BOTTOM);
cp.add(lb,BorderLayout.NORTH);
cp.add(bt,BorderLayout.SOUTH);
bt.setIcon(pa);
bt.setRolloverIcon(pb);
bt.setPressedIcon(pc);
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()=="Click")
bt.setText("点击");
else
bt.setText("Click");}
}
}
关于ImageIcon(string filename)...貌似要指定绝对路径。。因为我开始没指定绝对路径就算把图片文件和.class放在同一个文件夹也无法正常显示。。
支持的图片有GIF.JPG.JPEG还有从1.3版本开始支持PNG等。不支持ico。