回 帖 发 新 帖 刷新版面

主题:[转帖]外观变化疑问

为什么在Metal  Motif  Windows之间选择,外观完全一样,毫无变化
照书本例子,书本里也给出三种外观
为什么我试的不行呢
import java.awt.*;
import javax.swing.*;

import java.awt.event.*;

public class LookandFeel extends JFrame
{
    JRadioButton b1=new JRadioButton("Metal"),
    b2=new JRadioButton("Motif"),
    b3=new JRadioButton("Windows");
    
    public LookandFeel()
    {
        super("Swing application");
        
        Container contentPane=getContentPane();
        contentPane.add(new Panel(),BorderLayout.CENTER);
    }
    
    public static void main(String args[])
    {
        final JFrame f=new LookandFeel();
        
        f.setBounds(100,100, 300, 300);
        f.setVisible(true);
        f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
    
    class Panel extends JPanel implements ActionListener
    {
        public Panel()
        {
            add(new JButton("JButton"));
            add(new JTextField("JTextField"));
            add(new JCheckBox("JCheckBox"));
            add(new JRadioButton("JRadioButton"));
            add(new JLabel("JLabel"));
            add(new JList(new String[]{
                    "JList Item 1","JList Item 2","JList Item 3"}));
            add(new JScrollBar(SwingConstants.HORIZONTAL));
            
            ButtonGroup group=new ButtonGroup();
            group.add(b1);
            group.add(b2);
            group.add(b3);
            
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            
            add(b1);
            add(b2);
            add(b3);
            
        }
        
        public void actionPerformed(ActionEvent e)
        {
            JRadioButton src=(JRadioButton)e.getSource();
            
            try
            {
                if((JRadioButton)e.getSource()==b1)
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookandFeel");
                else if((JRadioButton)e.getSource()==b2)
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookandFeel");
                else if((JRadioButton)e.getSource()==b3)
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookandFeel");
            } catch (Exception ex)
            {
                
            }
            SwingUtilities.updateComponentTreeUI(getContentPane());
            
            
        }
    }
  
}

回复列表 (共1个回复)

沙发

把下面的代码加上去 对照发现你的外观名称的and应该改成And就可以了

UIManager.LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
   for (int i = 0; i < laf.length; i++) {
     System.out.println(laf[i].getClassName());
   }

我来回复

您尚未登录,请登录后再回复。点此登录或注册