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.
    
    
  • 相关阅读:
    FAQ: c++ 函数名后添加 const void function1(int &id) const
    FAQ: C++中定义类的对象:用new和不用new有何区别?
    How to create a project with existing folder of files in Visual Studio?
    How to export a template in Visual Studio?
    t4 multiple output sample
    fastjson1.2.48以下版本存在重大漏洞
    一秒完成springboot与logback配置
    统计greenplum_postgresql数据占用存储情况
    上传文件不落地转Base64字符串
    三个标签完成springboot定时任务配置
  • 原文地址:https://www.cnblogs.com/youxin/p/2495882.html
Copyright © 2011-2022 走看看