java swing setVisible放置顺序不同导致显示效果不同 财富值4

2016-10-15 23:20发布

问题就是在不同地方使用setVisible最终效果不同,下面有代码和效果图

package mvc;  import javax.swing.JFrame; import javax.swing.*; import java.awt.*;  /**  * Created by Smith on 2016/10/15.  */ public class MvcTest {     static final int WIDTH = 500;     static final int HEIGHT = 600;      public static void main(String[] args)     {         JFrame jf = new JFrame("Hello");         jf.setSize(WIDTH,HEIGHT);         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         jf.setVisible(true);         JPanel jp = new JPanel();         jf.setContentPane(jp);         JButton btn1 = new JButton("WEST");         BorderLayout lay = new BorderLayout();         jf.setLayout(lay);         jp.add(btn1,"West");         //jf.setVisible(true);     } }

生成效果:

接下来是正常结果的代码

package mvc;  import javax.swing.JFrame; import javax.swing.*; import java.awt.*;  /**  * Created by Smith on 2016/10/15.  */ public class MvcTest {     static final int WIDTH = 500;     static final int HEIGHT = 600;      public static void main(String[] args)     {         JFrame jf = new JFrame("Hello");         jf.setSize(WIDTH,HEIGHT);         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         //jf.setVisible(true);         JPanel jp = new JPanel();         jf.setContentPane(jp);         JButton btn1 = new JButton("WEST");         BorderLayout lay = new BorderLayout();         jf.setLayout(lay);         jp.add(btn1,"West");         jf.setVisible(true);     } }

友情提示: 问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
0条回答

一周热门 更多>