• 如何更改Jframe里Jpanel的大小


    先贴代码:

    package xiya;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    class Fram extends JFrame{
    	public Fram(){
    		setTitle("Test");
    		setSize(800, 600);
    		setResizable(false);
    		setVisible(true);
    		setBackground(Color.WHITE);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		vgaPanel vgaPanel = new vgaPanel();
    		add(vgaPanel, BorderLayout.WEST);
    		
    		regPanel regPanel = new regPanel();
    		add(regPanel, BorderLayout.EAST);
    		
    		JPanel northJPanel = new JPanel();
    		northJPanel.setPreferredSize(new Dimension(800, 0));
    		add(northJPanel, BorderLayout.NORTH);
    		
    		JPanel buttonJPanel = new JPanel();
    		buttonJPanel.setPreferredSize(new Dimension(800, 40));
    		buttonJPanel.add(new JButton("Run"));
    		buttonJPanel.add(new JButton("Step"));
    		buttonJPanel.add(new JButton("Restart"));
    		add(buttonJPanel, BorderLayout.SOUTH);
    	}
    }
    
    class vgaPanel extends JPanel{
    	public vgaPanel() {
    		setLayout(null);
    		//setSize(400, 600);
    		setPreferredSize(new Dimension(640, 300));
    		setBackground(Color.RED);
    	}
    }
    
    class regPanel extends JPanel{
    	public regPanel() {
    		setLayout(null);
    		//setSize(400, 600);
    		setPreferredSize(new Dimension(160, 300));
    		setBackground(Color.GREEN);
    	}
    }
    public class Test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Fram fram = new Fram();
    	}
    
    }
    


    Jframe默认布局为:BorderLayout

    Jpanel默认布局为:FlowLayout

    默认布局会屏蔽掉我们的设置大小(如setSize)

    Jpanel中使用setPreferredSize更改大小,如果想使用自己的布局方式,必须setLayout(null);

    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    软件工程结对作业02
    软件工程个人作业04
    第五周学习进度条
    软件工程中的形式化方法
    需求工程
    软件过程
    软件项目管理
    软件概论概述
    人月神话读后略有感想
    软件工程—理论、方法和实践 第一章:概述
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834888.html
Copyright © 2020-2023  润新知