java 制作 JPanel 弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28115745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:01:18  来源:igfitidea点击:

Making a JPanel Popup

javanetbeansjframejpanel

提问by Henry Fantacussi

I'm developing an app for computer, and i have a JFrame with a lot of JPanel on it, and when i click on a button, i want another JPanel to popup.

我正在为计算机开发一个应用程序,我有一个带有很多 JPanel 的 JFrame,当我点击一个按钮时,我想要另一个 JPanel 弹出。

Example: When i click on this button http://i62.tinypic.com/c2fzr.jpgenter image description here

示例:当我点击这个按钮 http://i62.tinypic.com/c2fzr.jpg在此处输入图片说明

I want this window to popup http://i62.tinypic.com/2qi0in7.jpgenter image description here

我希望这个窗口弹出 http://i62.tinypic.com/2qi0in7.jpg在此处输入图片说明

I already tried making a popup menu, but i don't want a menu, i want a window, and i can't seen to find out how to do it :( It's probably easy, but i don't have enough knowledge in java

我已经尝试制作一个弹出菜单,但我不想要一个菜单​​,我想要一个窗口,但我看不到如何去做:( 这可能很容易,但我没有足够的知识爪哇

Any help? thanks guys!

有什么帮助吗?多谢你们!

回答by Vulovic Vukasin

Ok so,for this you will need 2 JFrames. First one is where the buttons and everything is and the second one is the one that will popup. You will have 3 classes: Main, classWhere1stJframeis, ClassWhere2ndJframeis.

好的,为此您将需要 2 个 JFrame。第一个是按钮和所有东西所在的位置,第二个是将弹出的那个。您将拥有 3 个类:Main, classWhere1stJframeis, ClassWhere2ndJframeis

This is main:

这是main

package proba;

import javax.swing.JFrame;

public class mejn {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Frame1 frejm = new Frame1();
        frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frejm.setVisible(true);
        frejm.setSize(250, 300);
    }
}

This is Frame1:

这是Frame1

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame1 extends JFrame {
    JFrame Frame = new JFrame();
    JButton Button1 = new JButton();

    public Frame1()
    {
        super("The title");

        Frame = new JFrame();
        Button1 = new JButton();
        Frame.add(Button1);

        thehandler handler = new thehandler();
        Button1.addActionListener(handler);
    }


    private class thehandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if(event.getSource()==Button1)
            {
                Frejm2 frejm = new Frejm2();
                frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frejm.setVisible(true);
            }
        }
    }
}

This is Frame2:

这是Frame2

import javax.swing.JFrame;

public class Frejm2 extends JFrame {
    JFrame Frame2 = new JFrame();

    public Frejm2()
    {
        super("Title");
    }
}

回答by David Coler

that is not just a panel you want to pop up that would be considered a whole other frame. I would suggest making a different JFrame class that when the button is clicked instantiates the other frame.

这不仅仅是您想要弹出的面板,它会被视为整个其他框架。我建议创建一个不同的 JFrame 类,当单击按钮时实例化另一个框架。