JFC问题??
谁能帮我找找原因 啊 ?怎么点击按钮没变化呢!!应该是变灰色的啊!
原代码:
import java.awt.*;
import javax.swing.*;
//import sun.java2d.loops.CustomComponent;
import java.awt.event.*;
public class TJButton extends JApplet{//结果不对 啊郁闷!!!
JButton button1,button2,button3;
Icon metalIcon=new ImageIcon("metal.gif");
Icon motifIcon=new ImageIcon("motif.gif");
Icon windowIcon=new ImageIcon("window.gif");
String metallicLF="javac.swing.plaf.metal.MetalLookAndFeel";
String motifLF="con.sun.java.swing.plaf.MotifLookAndFeel";
String windowLF="com.sun.java.swing.plaf.WindowsLookAndFeel";
public void init(){
GridLayout grid =new GridLayout(1,3,5,5);
this.getContentPane().setLayout(grid);
this.getContentPane().setBackground(Color.lightGray);
button1=new JButton("Java L&F",metalIcon);
button1.setVerticalTextPosition(SwingConstants.BOTTOM);
button1.setHorizontalTextPosition(SwingConstants.CENTER);
button1.setMnemonic('J');
button1.setEnabled(false);
button1.setActionCommand(metallicLF);
CustomListener c1=new CustomListener(this);
button1.addActionListener(c1);
this.getContentPane().add(button1);
button2=new JButton("Motif L&F",motifIcon);
button2.setVerticalTextPosition(SwingConstants.BOTTOM);
button2.setHorizontalTextPosition(SwingConstants.CENTER);
button2.setMnemonic('M');
button2.setActionCommand(motifLF);
CustomListener c2=new CustomListener(this);
button2.addActionListener(c2);
this.getContentPane().add(button2);
button3=new JButton("Window L&F",windowIcon);
button3.setVerticalTextPosition(SwingConstants.BOTTOM);
button3.setHorizontalTextPosition(SwingConstants.CENTER);
button3.setMnemonic('W');
button3.setActionCommand(windowLF);
CustomListener c3=new CustomListener(this);
button3.addActionListener(c3);
this.getContentPane().add(button3);
}
class CustomListener implements ActionListener{
TJButton testApplet=null;
public CustomListener(TJButton testApplet){
this.testApplet=testApplet;
}
public void actionPerformed(ActionEvent evt){
String LF=evt.getActionCommand();
try{
UIManager.setLookAndFeel(LF);
SwingUtilities.updateComponentTreeUI(testApplet);
JButton button=(JButton)evt.getSource();
button.setEnabled(false);
updateStatus(button);
}catch(Exception e){
System.err.println("Look and Feel not set for "+LF+"!");
}
}
public void updateStatus(JButton button){
if(button ==button1){
button2.setEnabled(true);
button3.setEnabled(true);
}
else if (button ==button2){
button1.setEnabled(true);
button3.setEnabled(true);
}
else if (button ==button3){
button1.setEnabled(true);
button2.setEnabled(true);
}
}
}
}