📜  Java摇摆 |带有示例的 Popup 和 PopupFactory

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

Java摇摆 |带有示例的 Popup 和 PopupFactory

Popup 和 PopupFactory 是Java Swing 库的一部分。当我们想要向用户显示该特定包含层次结构中所有其他组件顶部的组件时,将使用弹出窗口。 PopupFactory 是用于创建弹出窗口的类。弹出窗口的生命周期非常短,通常是具有有限子组件的轻量级组件。
该类的构造函数是:

  1. PopupFactory():为弹出工厂创建一个对象

常用方法

methodexplanation
getPopup(Component o, Component c, int x, int y)Creates a Popup for the Component o containing the Component c at a location x, y on the owner component.
hide()Hides and disposes of the Popup.
show()Makes the Popup visible.

下面的程序将说明弹出窗口的使用:

  1. 创建弹出窗口并将其显示在父框架上的Java程序:我们通过创建弹出窗口工厂并使用返回弹出窗口的函数getpopup() 来创建弹出窗口 p。我们将此弹出窗口添加到标题为“pop”的框架 f 我们将创建一个标签并将 t 添加到容器面板 p2 并使用 setBackground()函数设置面板 p2 的背景。我们将容器面板 p2 添加到弹出窗口中。我们还将创建一个名为“click”的按钮 b,添加一个动作监听器,并在按下按钮时显示弹出窗口。按钮 b 被添加到面板中,并且面板被添加到框架中。使用 setSize(400,400) 将帧设置为大小 400,400。最后,使用 show()函数显示帧。
Java
// Java Program to create a popup and display
// it on a parent frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class pop extends JFrame implements ActionListener {
    // popup
    Popup p;
 
    // constructor
    pop()
    {
        // create a frame
        JFrame f = new JFrame("pop");
 
        // create a label
        JLabel l = new JLabel("This is a popup");
 
        f.setSize(400, 400);
 
        PopupFactory pf = new PopupFactory();
 
        // create a panel
        JPanel p2 = new JPanel();
 
        // set Background of panel
        p2.setBackground(Color.red);
 
        p2.add(l);
 
        // create a popup
        p = pf.getPopup(f, p2, 180, 100);
 
        // create a button
        JButton b = new JButton("click");
 
        // add action listener
        b.addActionListener(this);
 
        // create a panel
        JPanel p1 = new JPanel();
 
        p1.add(b);
        f.add(p1);
        f.show();
    }
 
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
        p.show();
    }
    // main class
    public static void main(String args[])
    {
        pop p = new pop();
    }
}


Java
// Java Program to create a popup (add a panel) and
// display it on a parent frame and also
// add action listener to the popup
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class pop1 extends JFrame implements ActionListener {
    // popup
    Popup po;
 
    // frame
    JFrame f;
 
    // panel
    JPanel p;
 
    // popupfactory
    PopupFactory pf;
 
    // constructor
    pop1()
    {
        // create a frame
        f = new JFrame("pop");
 
        f.setSize(400, 400);
 
        pf = new PopupFactory();
 
        // create a label
        JLabel l = new JLabel("This  is a popup menu");
 
        // create a new button
        JButton b19 = new JButton("OK");
 
        // add action listener
        b19.addActionListener(this);
 
        try {
            // set windows look and feel
            UIManager.setLookAndFeel(UIManager.
                  getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
        }
 
        // create a panel
        p = new JPanel();
 
        p.setBackground(Color.blue);
 
        // create a font
        Font fo = new Font("BOLD", 1, 14);
 
        l.setFont(fo);
 
        // add contents to panel
        p.add(l);
        p.add(b19);
 
        p.setLayout(new GridLayout(2, 1));
 
        // create a popup
        po = pf.getPopup(f, p, 180, 100);
 
        // create a button
        JButton b = new JButton("click");
 
        // add action listener
        b.addActionListener(this);
 
        // create a panel
        JPanel p1 = new JPanel();
 
        p1.add(b);
        f.add(p1);
        f.show();
    }
 
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String d = e.getActionCommand();
        // if ok button is pressed hide the popup
        if (d.equals("OK")) {
            po.hide();
 
            // create a popup
            po = pf.getPopup(f, p, 180, 100);
        }
        else
            po.show();
    }
    // main class
    public static void main(String args[])
    {
        pop1 p = new pop1();
    }
}


  1. 输出

  1. Java程序创建弹出窗口(添加面板)并将其显示在父框架上,并向弹出窗口添加动作侦听器:我们通过创建弹出窗口工厂并使用返回弹出窗口的函数getpopup() 创建弹出窗口 p。我们将外观设置为系统外观。我们将此弹出窗口添加到标题为“pop”的框架 f,我们将创建一个标签和一个标题为“OK”的按钮 b19(我们将标签的字体设置为指定的字体对象)并将其添加到容器面板 p2 和使用 setBackground()函数设置面板 p2 的背景。我们将容器面板 p2 添加到弹出窗口中。我们还将创建一个名为“click”的按钮 b,添加一个动作监听器,并在按下按钮时显示弹出窗口。按钮 b 被添加到面板中,并且面板被添加到框架中。使用 setSize(400,400) 将帧设置为大小 400,400。最后,使用 show()函数显示框架。

Java

// Java Program to create a popup (add a panel) and
// display it on a parent frame and also
// add action listener to the popup
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class pop1 extends JFrame implements ActionListener {
    // popup
    Popup po;
 
    // frame
    JFrame f;
 
    // panel
    JPanel p;
 
    // popupfactory
    PopupFactory pf;
 
    // constructor
    pop1()
    {
        // create a frame
        f = new JFrame("pop");
 
        f.setSize(400, 400);
 
        pf = new PopupFactory();
 
        // create a label
        JLabel l = new JLabel("This  is a popup menu");
 
        // create a new button
        JButton b19 = new JButton("OK");
 
        // add action listener
        b19.addActionListener(this);
 
        try {
            // set windows look and feel
            UIManager.setLookAndFeel(UIManager.
                  getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
        }
 
        // create a panel
        p = new JPanel();
 
        p.setBackground(Color.blue);
 
        // create a font
        Font fo = new Font("BOLD", 1, 14);
 
        l.setFont(fo);
 
        // add contents to panel
        p.add(l);
        p.add(b19);
 
        p.setLayout(new GridLayout(2, 1));
 
        // create a popup
        po = pf.getPopup(f, p, 180, 100);
 
        // create a button
        JButton b = new JButton("click");
 
        // add action listener
        b.addActionListener(this);
 
        // create a panel
        JPanel p1 = new JPanel();
 
        p1.add(b);
        f.add(p1);
        f.show();
    }
 
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String d = e.getActionCommand();
        // if ok button is pressed hide the popup
        if (d.equals("OK")) {
            po.hide();
 
            // create a popup
            po = pf.getPopup(f, p, 180, 100);
        }
        else
            po.show();
    }
    // main class
    public static void main(String args[])
    {
        pop1 p = new pop1();
    }
}
  1. 输出

  • https://docs.oracle.com/javase/7/docs/api/javax/swing/Popup.html
  • https://docs.oracle.com/javase/7/docs/api/javax/swing/PopupFactory.html