📜  Java Swing – 带有示例的 JPanel

📅  最后修改于: 2022-05-13 01:55:38.930000             🧑  作者: Mango

Java Swing – 带有示例的 JPanel

JPanel 是Java Swing 包的一部分,是一个可以存储一组组件的容器。 JPanel 的主要任务是组织组件,在 JPanel 中可以设置各种布局,提供更好的组件组织,但是它没有标题栏。

JPanel 的构造函数

  1. JPanel() :创建一个带有流布局的新面板
  2. JPanel(LayoutManager l) : 用指定的 layoutManager 创建一个新的 JPanel
  3. JPanel(boolean isDoubleBuffered) : 创建一个具有指定缓冲策略的新 JPanel
  4. JPanel(LayoutManager l, boolean isDoubleBuffered) : 用指定的 layoutManager 和指定的缓冲策略创建一个新的 JPanel

JPanel的常用功能

  1. add(Component c) : 将组件添加到指定容器
  2. setLayout(LayoutManager l) :将容器的布局设置为指定的布局管理器
  3. updateUI() :使用当前外观的值重置 UI 属性。
  4. setUI(PanelUI ui) :设置呈现此组件的对象的外观。
  5. getUI() :返回呈现此组件的外观对象。
  6. paramString() :返回此 JPanel 的字符串表示形式。
  7. getUIClassID() :返回呈现此组件的外观类的名称。
  8. getAccessibleContext() :获取与此 JPanel 关联的 AccessibleContext。

让我们举一个示例程序,通过附加输出的顺序执行快照来说明 JPanel 类的使用,以证明以下程序集的合理性,如下所示:

例子:

Java
// Java Program to Create a Simple JPanel
// and Adding Components to it
  
// Importing required classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
// Class 1
// Helper class extending JFrame class
class solution extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2;
  
    // Label to display text
    static JLabel l;
  
    // Main class
    public static void main(String[] args)
    {
        // Creating a new frame to store text field and
        // button
        f = new JFrame("panel");
  
        // Creating a label to display text
        l = new JLabel("panel label");
  
        // Creating a new buttons
        b = new JButton("button1");
        b1 = new JButton("button2");
        b2 = new JButton("button3");
  
        // Creating a panel to add buttons
        JPanel p = new JPanel();
  
        // Adding buttons and textfield to panel
        // using add() method
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(l);
  
        // setbackground of panel
        p.setBackground(Color.red);
  
        // Adding panel to frame
        f.add(p);
  
        // Setting the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}


Java
// Java Program to Create a JPanel with a Border Layout
// and Adding Components to It
  
// Importing required classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
// Main class
// Extending JFrame class
class solution extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2, b3;
  
    // Label to display text
    static JLabel l;
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a new frame to store text field and
        // button
        f = new JFrame("panel");
  
        // Creating a label to display text
        l = new JLabel("panel label");
  
        // Creating a new buttons
        b = new JButton("button1");
        b1 = new JButton("button2");
        b2 = new JButton("button3");
        b3 = new JButton("button4");
  
        // Creating a panel to add buttons
        // and a specific layout
        JPanel p = new JPanel(new BorderLayout());
  
        // Adding buttons and textfield to panel
        // using add() method
        p.add(b, BorderLayout.NORTH);
        p.add(b1, BorderLayout.SOUTH);
        p.add(b2, BorderLayout.EAST);
        p.add(b3, BorderLayout.WEST);
        p.add(l, BorderLayout.CENTER);
  
        // setbackground of panel
        p.setBackground(Color.red);
  
        // Adding panel to frame
        f.add(p);
  
        // Setting the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}


Java
// Java Program to Create a JPanel with a Box layout
// and Adding components to it
  
// Importing required classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
// Main class
// Extending JFrame class
class solution extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2, b3;
  
    // Label to display text
    static JLabel l;
  
    // Main drive method
    public static void main(String[] args)
    {
        // Creating a new frame to store text field and
        // button
        f = new JFrame("panel");
  
        // Creating a label to display text
        l = new JLabel("panel label");
  
        // Creating a new buttons
        b = new JButton("button1");
        b1 = new JButton("button2");
        b2 = new JButton("button3");
        b3 = new JButton("button4");
  
        // Creating a panel to add buttons and
        // textfield and a layout
        JPanel p = new JPanel();
  
        // Setting box layout
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
  
        // Adding buttons and textfield to panel
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(l);
  
        // Setting background of panel
        p.setBackground(Color.red);
  
        // Adding panel to frame
        f.add(p);
  
        // Setting the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}


输出:

示例 2:

Java

// Java Program to Create a JPanel with a Border Layout
// and Adding Components to It
  
// Importing required classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
// Main class
// Extending JFrame class
class solution extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2, b3;
  
    // Label to display text
    static JLabel l;
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a new frame to store text field and
        // button
        f = new JFrame("panel");
  
        // Creating a label to display text
        l = new JLabel("panel label");
  
        // Creating a new buttons
        b = new JButton("button1");
        b1 = new JButton("button2");
        b2 = new JButton("button3");
        b3 = new JButton("button4");
  
        // Creating a panel to add buttons
        // and a specific layout
        JPanel p = new JPanel(new BorderLayout());
  
        // Adding buttons and textfield to panel
        // using add() method
        p.add(b, BorderLayout.NORTH);
        p.add(b1, BorderLayout.SOUTH);
        p.add(b2, BorderLayout.EAST);
        p.add(b3, BorderLayout.WEST);
        p.add(l, BorderLayout.CENTER);
  
        // setbackground of panel
        p.setBackground(Color.red);
  
        // Adding panel to frame
        f.add(p);
  
        // Setting the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}

输出:

示例 3:

Java

// Java Program to Create a JPanel with a Box layout
// and Adding components to it
  
// Importing required classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
// Main class
// Extending JFrame class
class solution extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2, b3;
  
    // Label to display text
    static JLabel l;
  
    // Main drive method
    public static void main(String[] args)
    {
        // Creating a new frame to store text field and
        // button
        f = new JFrame("panel");
  
        // Creating a label to display text
        l = new JLabel("panel label");
  
        // Creating a new buttons
        b = new JButton("button1");
        b1 = new JButton("button2");
        b2 = new JButton("button3");
        b3 = new JButton("button4");
  
        // Creating a panel to add buttons and
        // textfield and a layout
        JPanel p = new JPanel();
  
        // Setting box layout
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
  
        // Adding buttons and textfield to panel
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(l);
  
        // Setting background of panel
        p.setBackground(Color.red);
  
        // Adding panel to frame
        f.add(p);
  
        // Setting the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}

输出:

此后,我们成功地在面板中生成按钮。