|
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class setbutton extends JApplet
{
Image mm,jj;
JFrame frame1;
JButton mmbn,jjbn;
JLabel label1;
JPanel panel1;
ImageIcon jjcon,mmcon;
public void init()
{
frame1=new JFrame("按钮测试");
frame1.setLayout(new BorderLayout());
mm=getImage(getCodeBase(),"mm.jpg");
mmcon=new ImageIcon(mm);
jj=getImage(getCodeBase(),"jj.jpg");
jjcon=new ImageIcon(jj);
label1=new JLabel();
frame1.add(label1,BorderLayout.CENTER);
panel1=(JPanel)getContentPane();
panel1.setLayout(new GridLayout(1,2));
mmbn=new JButton("我是MM^_^");
jjbn=new JButton("我是JJ^_^");
panel1.add(mmbn);
panel1.add(jjbn);
mmbn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
label1.setIcon(mmcon);
}
});
jjbn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
label1.setIcon(jjcon);
}
});
frame1.add(panel1,BorderLayout.SOUTH);
frame1.pack();
frame1.show();
}
}
I:\JAVA>appletviewer setbutton.html
java.lang.Error: Do not use javax.swing.JFrame.setLayout() use javax.swing.JFram
e.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.setLayout(JFrame.java:531)
at setbutton.init(setbutton.java:20)
at sun.applet.AppletPanel.run(AppletPanel.java:353)
at java.lang.Thread.run(Thread.java:534)
|
|