| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 908 人关注过本帖
标题:外观不能改变
取消只看楼主 加入收藏
tgnian
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-13
收藏
 问题点数:0 回复次数:2 
外观不能改变
为什么在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());        } }  }
搜索更多相关主题的帖子: 外观 
2007-11-25 02:56
tgnian
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-13
收藏
得分:0 
为什么在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());
            
            
        }
    }
  
}

2007-11-25 03:00
tgnian
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-13
收藏
得分:0 
谢谢了

2007-11-28 01:03
快速回复:外观不能改变
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026151 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved