| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 906 人关注过本帖
标题:外观不能改变
只看楼主 加入收藏
tgnian
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-13
收藏
 问题点数:0 回复次数:5 
外观不能改变
为什么在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
jdk2006
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2007-5-12
收藏
得分:0 
这个例子我也做了。我想应该用到了Toolkit 这个类

2007-11-25 03:18
csight
Rank: 1
等 级:新手上路
威 望:1
帖 子:293
专家分:0
注 册:2006-6-11
收藏
得分:0 
原帖由 tgnian 于 2007-11-25 03:00 发表 [url=http://bbs.][/url] 为什么在Metal Motif Windows之间选择,外观完全一样,毫无变化照书本例子??,书本里也给出三种外观为什么我试的不行呢 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class LookandFeel ...


因为lz拼写错了;
MetalLookandFeel -> MetalLookAndFeel
MotifLookandFeel- > MotifLookAndFeel
WindowsLookandFeel -> WindowsLookAndFeel

头可断,发型不可乱;血可流,皮鞋不可不擦油;
2007-11-25 03:34
tgnian
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-13
收藏
得分:0 
谢谢了

2007-11-28 01:03
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
又是一个细节问题哦

可惜不是你,陪我到最后
2007-11-28 09:45
快速回复:外观不能改变
数据加载中...
 
   



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

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