📜  Java Swing-JPanel

📅  最后修改于: 2020-09-30 06:43:39             🧑  作者: Mango

Java JPanel

JPanel是最简单的容器类。它提供了一个空间,应用程序可以在其中附加任何其他组件。它继承了JComponents类。

它没有标题栏。

JPanel类声明

public class JPanel extends JComponent implements Accessible

常用的构造函数:

Constructor Description
JPanel() It is used to create a new JPanel with a double buffer and a flow layout.
JPanel(boolean isDoubleBuffered) It is used to create a new JPanel with FlowLayout and the specified buffering strategy.
JPanel(LayoutManager layout) It is used to create a new JPanel with the specified layout manager.

Java JPanel示例

import java.awt.*;
import javax.swing.*;
public class PanelExample {
 PanelExample()
    {
    JFrame f= new JFrame("Panel Example");  
    JPanel panel=new JPanel();
    panel.setBounds(40,80,200,200);  
    panel.setBackground(Color.gray);
    JButton b1=new JButton("Button 1");   
    b1.setBounds(50,100,80,30);  
    b1.setBackground(Color.yellow); 
    JButton b2=new JButton("Button 2"); 
    b2.setBounds(100,100,80,30);  
    b2.setBackground(Color.green); 
    panel.add(b1); panel.add(b2);
    f.add(panel);
                f.setSize(400,400);  
                f.setLayout(null);  
                f.setVisible(true);  
    }
    public static void main(String args[])
    {
    new PanelExample();
    }
}

输出: