JFC问题??

xiaoxiaoxiaogou_1983 2004-09-06 09:22:37
谁能帮我找找原因 啊 ?怎么点击按钮没变化呢!!应该是变灰色的啊!
原代码:
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);

}
}
}

}
...全文
75 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hexiaofeng 2004-09-08
  • 打赏
  • 举报
回复
你原来的
String metallicLF = "javax.swing.plaf.metal.MetalLookAndFeel";
String motifLF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
String windowLF = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
定义都有问题,。字符串内容不对
hexiaofeng 2004-09-07
  • 打赏
  • 举报
回复
这是我调好的程序,测试成功

package untitled5;

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 = "javax.swing.plaf.metal.MetalLookAndFeel";
String motifLF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
String windowLF = "com.sun.java.swing.plaf.windows.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);

}
}
}

}
mickeylm 2004-09-06
  • 打赏
  • 举报
回复
帮你顶

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧