package com.corejava.L82;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class PlafTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PlafFrame frame = new PlafFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class PlafFrame extends JFrame {
public PlafFrame() {
setTitle("观感测试");
setSize(300, 200); PlafPanel panel = new PlafPanel();
add(panel);
}
}class PlafPanel extends JPanel {
public PlafPanel() {
UIManager.LookAndFeelInfo[] infos = UIManager
.getInstalledLookAndFeels(); for (UIManager.LookAndFeelInfo[] info : infos)
makeButton(info.getName(),info.getClassName()); } void makeButton(String name, final String plafName) {
JButton button = new JButton(name);
add(button); button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
UIManager.setLookAndFeel(plafName);
SwingUtilities.updateComponentTreeUI(PlafPanel.this);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}以上红色部分出现问题:
infos--Type mismatch: cannot convert from element type UIManager.LookAndFeelInfo to
UIManager.LookAndFeelInfo[]
info.getName()--Cannot invoke getName() on the array type UIManager.LookAndFeelInfo[]
info.getClassName()--Cannot invoke getClassName() on the array type UIManager.LookAndFeelInfo[]
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class PlafTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PlafFrame frame = new PlafFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class PlafFrame extends JFrame {
public PlafFrame() {
setTitle("观感测试");
setSize(300, 200); PlafPanel panel = new PlafPanel();
add(panel);
}
}class PlafPanel extends JPanel {
public PlafPanel() {
UIManager.LookAndFeelInfo[] infos = UIManager
.getInstalledLookAndFeels(); for (UIManager.LookAndFeelInfo[] info : infos)
makeButton(info.getName(),info.getClassName()); } void makeButton(String name, final String plafName) {
JButton button = new JButton(name);
add(button); button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
UIManager.setLookAndFeel(plafName);
SwingUtilities.updateComponentTreeUI(PlafPanel.this);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}以上红色部分出现问题:
infos--Type mismatch: cannot convert from element type UIManager.LookAndFeelInfo to
UIManager.LookAndFeelInfo[]
info.getName()--Cannot invoke getName() on the array type UIManager.LookAndFeelInfo[]
info.getClassName()--Cannot invoke getClassName() on the array type UIManager.LookAndFeelInfo[]
把红色部分做如下修改:
for (UIManager.LookAndFeelInfo info : infos)
makeButton(info.getName(),info.getClassName()); }
即去掉[].
for循环语法问题:
for(数组元素类型 name:数组名称)
{
可以通过"name"直接访问每一个数组元素;
}
makeButton(info.getName(),info.getClassName()); }
这段代码是在infos中查找LookAndFeelInfo[]类型的对象,异常说不能把infos中的元素转换为UIManager.LookAndFeelInfo[] 对象你的意思应该是下面这样:
for (UIManager.LookAndFeelInfo info : infos)
makeButton(info.getName(),info.getClassName()); }
取掉那个红色的中括号即可