问题就是在不同地方使用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"); Bo优艾设计网_设计圈rderLayout 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); } }
精彩评论