Java Swing – 带有示例的 JPanel
JPanel 是Java Swing 包的一部分,是一个可以存储一组组件的容器。 JPanel 的主要任务是组织组件,在 JPanel 中可以设置各种布局,提供更好的组件组织,但是它没有标题栏。
JPanel 的构造函数
- JPanel() :创建一个带有流布局的新面板
- JPanel(LayoutManager l) : 用指定的 layoutManager 创建一个新的 JPanel
- JPanel(boolean isDoubleBuffered) : 创建一个具有指定缓冲策略的新 JPanel
- JPanel(LayoutManager l, boolean isDoubleBuffered) : 用指定的 layoutManager 和指定的缓冲策略创建一个新的 JPanel
JPanel的常用功能
- add(Component c) : 将组件添加到指定容器
- setLayout(LayoutManager l) :将容器的布局设置为指定的布局管理器
- updateUI() :使用当前外观的值重置 UI 属性。
- setUI(PanelUI ui) :设置呈现此组件的对象的外观。
- getUI() :返回呈现此组件的外观对象。
- paramString() :返回此 JPanel 的字符串表示形式。
- getUIClassID() :返回呈现此组件的外观类的名称。
- 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();
}
}
输出:
此后,我们成功地在面板中生成按钮。
Note: In the previous Program, border layout and Box Layout are used. Different other layouts can be used to organize the components in a definite pattern, such as card layout, grid layout, etc.