使用 Java 的 Swing 将面板添加到容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4680493/
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-10-30 07:29:25  来源:igfitidea点击:

Adding panel to container with Java's swing

javaswing

提问by

I have made two panels and then added in third panel. How can I add a panel to show up on screen?

我制作了两个面板,然后添加到第三个面板中。如何添加面板以显示在屏幕上?

Here is my current code:

这是我当前的代码:

import javax.swing.*;
import java.awt.*;

public class LibraryFront {

public static void main(String[] args)
{

    JFrame f1 = new JFrame();
    JPanel cards;
    final String BUTTONPANEL = "Card with JButtons";
    final String TEXTPANEL = "Card with JTextField";

    JPanel card1 = new JPanel();
    JPanel card2 = new JPanel();

    // Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    cards.add(card1, BUTTONPANEL);
    cards.add(card2, TEXTPANEL);

    Container c = getContentPane(); //this gives error
}

采纳答案by brian_d

EDIT:
I noticed now that you took the code snippet (part) from http://download.oracle.com/javase/tutorial/uiswing/layout/card.htmland placed it directly into your main method. That code is just part of the full program implementation at CardLayoutDemo.java. You need to take a look at that code.

编辑:
我现在注意到您从http://download.oracle.com/javase/tutorial/uiswing/layout/card.html 获取代码片段(部分)并将其直接放入您的主要方法中。该代码只是CardLayoutDemo.java 中完整程序实现的一部分。你需要看看那个代码。

There are a lot of little errors with your code. Here is a working implementation

你的代码有很多小错误。这是一个工作实现

//it is good practice to only import the packages you need
//so that you know exactly what you are dealing with
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LibraryFront {

    public static void main(String[] args) {
        //f1 is the JFrame
        //f1.getContentPane() would return the Container
        //but you do not actually need to add directly to it
        JFrame f1 = new JFrame();
        JPanel cards;
        final String BUTTONPANEL = "Card with JButtons";
        final String TEXTPANEL = "Card with JTextField";

        JPanel card1 = new JPanel();
        JPanel card2 = new JPanel();

        // Create the panel that contains the "cards".
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);

        //adjust background colors just so you can see what is happening
        cards.setBackground(Color.GREEN);
        card1.setBackground(Color.RED);
        card2.setBackground(Color.BLUE);

        //set the layout to BorderLayout
        // add the cards JPanel to the center
        f1.setLayout(new BorderLayout());
        f1.add(cards, BorderLayout.CENTER);
        f1.setSize(400, 300);
        f1.setTitle("Test Frame");
        f1.setVisible(true);
    }
}

回答by fmucar

LibraryFront is not the container. JFramef1 is, f1.getContentPane()should work and you also need to add panels to JFrame and set it to visible if not visible.

LibraryFront 不是容器。JFramef1 是,f1.getContentPane()应该可以工作,您还需要将面板添加到 JFrame 并将其设置为可见(如果不可见)。

回答by Jesper

Try:

尝试:

Container c = f1.getContentPane();

Call the method on the JFrame, not on your own LibraryFrontclass.

在 上调用该方法JFrame,而不是在您自己的LibraryFront类上。

回答by jzd

It gives you an error because your class LibraryFrontdoes not contain a getContentPane()method. Instead call that method on the frame.

它给你一个错误,因为你的类LibraryFront不包含getContentPane()方法。而是在框架上调用该方法。

Do something like:

做类似的事情:

f1.getContentPane().add(cards);