zoukankan      html  css  js  c++  java
  • JFrame 刷新问题

    先看下面的一段代码:

    import java.awt.Container;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class Icon extends JFrame {
        private ImageIcon icon;
        private JLabel label1;
        private JPanel panel;
        public Icon()
        {
            Container container=getContentPane();
            setSize(1000,1000);
            setVisible(true);
            icon=createImageIcon("image/yanzi.png","燕姿");
            //label1=new JLabel( "icon display",icon,JLabel.CENTER);
            label1=new JLabel(icon);
            label1.setBounds(0, 0,300,500);
            this.add(label1);
            
            setLayout(null);
            setTitle("icon");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
        }
        protected  ImageIcon createImageIcon(String path, String description) {
            // TODO Auto-generated method stub
            URL imgURL=getClass().getResource(path);
        
            
            if(imgURL!=null)
                return new ImageIcon(imgURL,description);
            else
                System.out.println("couldn't find file "+path);
            return null;
        }
        public static void main(String[] args)
        {
            Icon icon=new Icon();
        }    
        
    }
    运行后显示的是一个空白的JFrame,只有当改变jframe大小时,图片才显示,因为构造函数里setVisible(true)是出现在添加组件的前面,jframe显示时是没有label的,改变大小时就会刷新jframe显示图片。可以改成如下:
      先添加组件在显示。setVisible(true);放在添加组件后。
    或者在添加组件后立即重绘,repaint();
    还可以 重新布局

    setVisible(true);
    添加组件
    invalidate();

       validate();

    Class Container 方法

    validate 
    public   void   validate() 验证此容器及其所有子组件。   
    使用   validate   方法会使容器再次布置其子组件。已经布置容器后,在修改此容器的子组件的时候(在容器中添加或移除组件,或者更改与布局相关的信息),应该调用上述方法。 




    当画出JFrame的以后,有时候需要响应某些监听事件,向JFrame里添加组件。这时候如果不做任何处理的话,JFrame是不会刷新的(除非窗口最小化,大小改变),所以需要我们强制刷新,用的方法是updateUI(),下面是一个实验程序,响应单选框选中,加入一个JButton,注意这里我使用的是JFrame,如果换成Frame的话,则无效果
    public class Text extends JFrame{
    JCheckBox a = new JCheckBox("1");
    JFrame f = new JFrame("text");
    JPanel p= new JPanel(new GridLayout(4,1));
    
    public void lantchFrame(){
       f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
         System.exit(0);
        }
       });
       p.add(a);
       f.add(p);
       a.addActionListener(new MyListener());
       f.pack();
       f.setSize(320,240);
       f.setVisible(true);
    }
    class MyListener implements ActionListener {
       public void actionPerformed(ActionEvent e) {
        if(a.isSelected()){
         JButton jb3 = new JButton("jb3");
         p.add(jb3);
        p.updateUI();
        }
       }
    }
    public static void main(String [] args){
       new Text().lantchFrame();
    }
    }
    JPanel updateUI() 
              Resets the UI property with a value from the current look and feel.
    
    
  • 相关阅读:
    .NET XmlNavigator with Namespace
    编程要素
    【FOJ】1962 新击鼓传花游戏
    【POJ】1389 Area of Simple Polygons
    【POJ】2482 Stars in Your Window
    【HDU】3265 Posters
    【HDU】1199 Color the Ball
    【HDU】3642 Get The Treasury
    【HDU】4027 Can you answer these queries?
    【HDU】1542 Atlantis
  • 原文地址:https://www.cnblogs.com/youxin/p/2495882.html
Copyright © 2011-2022 走看看