简体   繁体   English

将 JPanel 添加到 Netbeans 中的 JFrame

[英]Adding a JPanel to a JFrame in Netbeans

I created JFrame class in Netbeans and using generator I have add jPanel .我在 Netbeans 中创建了JFrame类,并使用生成器添加了jPanel I have also class, which extends JPanel .我还有类,它扩展了JPanel Now i want to create object of this class, and add him on the place where is my Panel in JFrame , but I can't find the right way, because all what I'm trying give no results现在我想创建这个类的对象,并将他添加到JFrame我的 Panel 所在的位置,但我找不到正确的方法,因为我尝试的所有内容都没有给出任何结果

public static void main(String args[]) {
    
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            Frame f  = new Frame();
            jPanel1  = new MyPanel();
            f.pack();
            f.setVisible(true);
        }
    });

Ok, so maybe I will show more precise example what am I talking about:好的,所以也许我会展示更精确的例子我在说什么:

first File:第一个文件:

class MyPanel extends javax.swing.JPanel {
public MyPanel() {
        initComponents();
        
    }
}

Another file, with Frame:另一个文件,带框架:

public class Frame extends javax.swing.JFrame {
public Frame() {
        initComponents();
}
public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Frame f  = new Frame();
                MyPanel p = new MyPanel();
                jPanel1 = p;
            }
        });
    }
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;

And I want to set on the place of jPanel1 - object MyPanel我想在 jPanel1 的地方设置 - 对象 MyPanel

Your problem: Your MyPanel instance is not added to the main frame.您的问题:您的MyPanel实例未添加到主框架中。

Solution: Add your MyPanel instance to the main frame's content pane.解决方案:将您的MyPanel实例添加到主框架的内容窗格。

Something like:就像是:

JFrame f  = new JFrame();
MyPanel jPanel1  = new MyPanel();
f.getContentPane().add(jPanel1  );
f.pack();
f.setVisible(true);

Above answers are correct - you need to actually add this MyPanel to the contentPane of the Frame.以上答案是正确的 - 您需要将此MyPanel实际添加到 Frame 的 contentPane 中。

Additionally you should probably use Swing instead of plain AWT.此外,您可能应该使用 Swing 而不是普通的 AWT。 The code would look like this:代码如下所示:

SwingUtilities.invokeLater(new Runnable() {

        @Override //annotation if you are using Java >= 1.5
        public void run() {
            JFrame f = new JFrame();
            MyPanel jPanel1 = new MyPanel();
            f.getContentPane().add(jPanel1);
            f.pack();
            f.setVisible(true);
        }
    });

You need to add the panel to the frame:您需要将面板添加到框架中:

f.add(jPanel1)

The following standalone code works for me :以下独立代码对我有用:

    public static void main(String[] args)
    {

        Frame f  = new Frame();
        Panel jPanel1  = new Panel();
        jPanel1.add(new Button("Hello"));
        f.add(jPanel1  );
        f.pack();
        f.setVisible(true);
    }

JFrame jframe = new JFrame(); JFrame jframe = new JFrame();

JPanel jpanel = new JPanel(); JPanel jpanel = new JPanel();

jframe.add(jpanel); jframe.add(jpanel);

Eg:例如:

   public Myclass extends JFrame{

   public Myclass(){

    this.setSize(300,300);

    this.add(new Mypanel());

     }

        class Mypanel extends JPanel{

                 // Add what u want to add in panel
              }


   }

Maybe the problem is that you have an empty JPanel, I mean, you have to use frame.add(MyPanel instance) but also you have to use (ie in MyPanel initComponents) add(jButton) or something like that..也许问题是你有一个空的 JPanel,我的意思是,你必须使用 frame.add(MyPanel instance) 但你也必须使用(即在 MyPanel initComponents 中)add(jButton) 或类似的东西..

you can also try seting the layout您也可以尝试设置布局

setLayout(new GridLayout(1, 1)); setLayout(new GridLayout(1, 1)); //also in the initComponents method. //也在 initComponents 方法中。

Can you show both initComponents();你能同时显示 initComponents(); methods?方法?


EDIT:编辑:

if you're using the Netbeans editor如果您使用的是 Netbeans 编辑器

Right click the Palette of components右键单击组件面板

go to Palette Manager转到调色板管理器

Add from project (if it's an open project) or jar, whatever you want.从项目(如果它是一个打开的项目)或 jar 添加,无论你想要什么。 You can browse your class there and look for your MyPanel file.您可以在那里浏览您的课程并查找您的 MyPanel 文件。

Then you will also select in which folder or menu must this component appear,然后您还将选择此组件必须出现在哪个文件夹或菜单中,

and then you should be able to drag it like a common JPanel.然后你应该可以像普通的 JPanel 一样拖动它。

Here is a great tutorial which teaches every thing about panel.这是一个很棒的教程,它教授有关面板的所有内容。 It teaches you how to create a JFrame which contains JPanel.它教你如何创建一个包含 JPanel 的 JFrame。

Go to the Video tutorial转到视频教程

If you want to find out interesting videos visit this channel如果您想了解有趣的视频,请访问此频道

Full list of Video tutorials视频教程的完整列表

None of you people even understood this question.你们甚至没有人理解这个问题。 The person has their own JPanel, created previously, not in the netbeans widget designer.这个人有自己的 JPanel,之前创建的,而不是在 netbeans 小部件设计器中。 They are asking how you add your own custom JPanel that you already built some other time ... into a JFrame, or Frame, or whatever it is the netbeans designer uses when you create a "Frame" in the designer.他们问你如何将自己的自定义 JPanel 添加到 JFrame 或 Frame 中,或者在设计器中创建“框架”时 netbeans 设计器使用的任何其他时间已经构建的自定义 JPanel。

The question is, how do you add your own previously created JPanel that extends JPanel into the designer environment.问题是,如何添加自己之前创建的 JPanel,将 JPanel 扩展到设计器环境中。 And no, it's not straightforward at all.不,这根本不是直截了当的。 You can insert your own "custom creation code" and assign one of the designers JPanels with your own statement "= new MyExistingCustomJPanel();"您可以插入您自己的“自定义创建代码”,并使用您自己的语句“= new MyExistingCustomJPanel();”分配设计器 JPanel 之一。 and the constructor of your MyExistingCustomJPanel will indeed run, but it never paints the gui when you run it, because your subclass has been downcast to a regular old JPanel in the assignment.并且您的 MyExistingCustomJPanel 的构造函数确实会运行,但是当您运行它时它永远不会绘制 gui,因为您的子类在分配中已向下转换为常规的旧 JPanel。

So, how does one insert their own JPanel into a project that wants to use it as an object in the Netbeans Form designer environment.那么,如何将自己的 JPanel 插入到想要将其用作 Netbeans Form 设计器环境中的对象的项目中。

Not so easy.没那么容易。 Never have found a clear way to get this going with my own JPanels.从来没有找到一种明确的方法来使用我自己的 JPanel 来实现这一点。 Just some cheats that are really ugly.只是一些非常丑陋的作弊。

All the answers here provided, excluded that the OP was trying to use their own JPanel in the Netbeans designer's "Frame" that it provides.这里提供的所有答案都排除了 OP 试图在它提供的 Netbeans 设计器的“框架”中使用他们自己的 JPanel。 Not straight forward at all, and nobody even understood the question.一点也不直截了当,甚至没有人理解这个问题。

I did, because I've struggled with this repeatedly.我做到了,因为我一直在努力解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM