9

我无法从主类获取我的 JFrame 来显示另一个类的 JPanel。一切都编译没有错误。

JFrameTest.java:

package jframetest;

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class JFrameTest extends JFrame {

    public JFrameTest() {

        FlowLayout mainLayout = new FlowLayout();
        setSize(320, 480);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(mainLayout);

        JPanel panelMain = new JPanel(mainLayout);

        JButton testButton = new JButton("Test12");
        panelMain.add(testButton);

        JPanelOne panel = new JPanelOne();
        panelMain.add(panel);
        panel.setVisible(true);
        add(panelMain);



    }

    public static void main(String[] arguments) {

        JFrameTest frame = new JFrameTest();
        frame.setVisible(true);


    }
}

JPanelOne.java:

package jframetest;

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JPanel;


public class JPanelOne extends JPanel {

    public JPanelOne() {

        FlowLayout layoutPanel = new FlowLayout();
        JPanel panel = new JPanel(layoutPanel);
        JButton button = new JButton("test");
        panel.add(button);
        panel.setVisible(true);

    }

}

将不同的 JPanel 保留在自己的课程中是一种好习惯吗?(示例:希望 JFrame 包含几个相同大小的 JPanel,这将通过将setVisible()设置为 true/false 来切换)

编辑

感谢您的所有回答。著名的。现在回到我的问题:

既然我知道如何添加在其他类中创建的单个 GUI 元素,我想知道是否可以在其中一个类中使用布局管理器来组织元素(可能在 JPanel 以外的其他容器中),所以我可以将它们添加为以布局组织的组(这就是为什么我要询问在其他类中创建整个 JPanel 的原因)。如图所示:

此示例的 JPanel(第二类)代码将是:

package jframetest;

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import net.miginfocom.swing.MigLayout;


public class JPanelOne extends JPanel {

    private JPanel panelSettingsMain;
    private JLabel labelChooseLanguage, labelWordsCollection;
    private JButton buttonSelectLanguage, buttonSelectCollection,
            buttonStatistics, buttonPrintingOptions, buttonAddNewWordCollection,
            buttonInterfaceCustomization;
    private JSeparator separatorSettingsOne, separatorSettingsTwo,
            separatorSettingsThree, separatorSettingsFour,
            separatorSettingsFive;
    private Color greenRegular, separatorGreenLight, separatorGreenDark;

    public JPanelOne() {

        // creating settings main panel

        // setting up layout managers
        MigLayout layoutSettingsMain = new MigLayout(
                "insets 3 0 0 0");

        // setting up colors
        greenRegular = new Color(30, 165, 145);
        separatorGreenLight = new Color(190, 240, 220);
        separatorGreenDark = new Color(130, 205, 180);

        panelSettingsMain = new JPanel(layoutSettingsMain);
        panelSettingsMain.setBackground(Color.WHITE);

            // setting up choose language label
            labelChooseLanguage = new JLabel("Choose language:");
            panelSettingsMain.add(labelChooseLanguage,
                    "gapleft 10, gaptop 15, width 200");

            // setting up select language button
            buttonSelectLanguage = new JButton("language");
            buttonSelectLanguage.setForeground(greenRegular);
            buttonSelectLanguage.setFocusPainted(false);
            buttonSelectLanguage.setBorder(null);
            buttonSelectLanguage.setContentAreaFilled(false);
            buttonSelectLanguage.setCursor(new java.awt.Cursor(
                    java.awt.Cursor.HAND_CURSOR));
            panelSettingsMain.add(buttonSelectLanguage, "gapbottom 15px, wrap");

            // setting up light separator
            separatorSettingsOne = new JSeparator();
            separatorSettingsOne.setForeground(separatorGreenLight);
            panelSettingsMain.add(separatorSettingsOne,
                    "span 2, width 320, gapbottom 15, wrap");

            // setting up words collection label
            labelWordsCollection = new JLabel("Words collection:");
            panelSettingsMain.add(labelWordsCollection, "gapleft 10");

            // setting up selectcollection button
            buttonSelectCollection = new JButton("collection");
            buttonSelectCollection.setForeground(greenRegular);
            buttonSelectCollection.setFocusPainted(false);
            buttonSelectCollection.setBorder(null);
            buttonSelectCollection.setContentAreaFilled(false);
            buttonSelectCollection.setCursor(new java.awt.Cursor(
                    java.awt.Cursor.HAND_CURSOR));
            panelSettingsMain.add(buttonSelectCollection,
                    "gapbottom 15px, wrap");

            // setting up dark separator
            separatorSettingsTwo = new JSeparator();
            separatorSettingsTwo.setForeground(separatorGreenDark);
            panelSettingsMain.add(separatorSettingsTwo,
                    "span 2, width 320, gapbottom 15px, wrap");

            // setting up show statistics button
            buttonStatistics = new JButton("Show statistics");
            buttonStatistics.setForeground(greenRegular);
            buttonStatistics.setFocusPainted(false);
            buttonStatistics.setBorder(null);
            buttonStatistics.setContentAreaFilled(false);
            buttonStatistics.setCursor(new java.awt.Cursor(
                    java.awt.Cursor.HAND_CURSOR));
            panelSettingsMain.add(buttonStatistics,
                    "gapleft 10, gapbottom 15px, , wrap");

            // setting up dark separator
            separatorSettingsThree = new JSeparator();
            separatorSettingsThree.setForeground(separatorGreenDark);
            panelSettingsMain.add(separatorSettingsThree,
                    "span 2, width 320, gapbottom 15px, wrap");

            // setting up printing options button
            buttonPrintingOptions = new JButton("Printing options");
            buttonPrintingOptions.setForeground(greenRegular);
            buttonPrintingOptions.setFocusPainted(false);
            buttonPrintingOptions.setBorder(null);
            buttonPrintingOptions.setContentAreaFilled(false);
            buttonPrintingOptions.setCursor(new java.awt.Cursor(
                    java.awt.Cursor.HAND_CURSOR));
            panelSettingsMain.add(buttonPrintingOptions,
                    "gapleft 10, gapbottom 15px, wrap");

            // setting up dark separator
            separatorSettingsFour = new JSeparator();
            separatorSettingsFour.setForeground(separatorGreenDark);
            panelSettingsMain.add(separatorSettingsFour,
                    "span 2, width 320, gapbottom 15px, wrap");

            // setting up add new word collection button
            buttonAddNewWordCollection = new JButton("Add new word collection");
            buttonAddNewWordCollection.setForeground(greenRegular);
            buttonAddNewWordCollection.setFocusPainted(false);
            buttonAddNewWordCollection.setBorder(null);
            buttonAddNewWordCollection.setContentAreaFilled(false);
            buttonAddNewWordCollection.setCursor(new java.awt.Cursor(
                    java.awt.Cursor.HAND_CURSOR));
            panelSettingsMain.add(buttonAddNewWordCollection,
                    "gapleft 10, gapbottom 15px, , wrap");

            // setting up dark separator
            separatorSettingsFive = new JSeparator();
            separatorSettingsFive.setForeground(separatorGreenDark);
            panelSettingsMain.add(separatorSettingsFive,
                    "span 2, width 320, gapbottom 10px, wrap");

            // setting up interface customization button
            buttonInterfaceCustomization = new JButton(
                    "Interface customization");
            buttonInterfaceCustomization.setForeground(greenRegular);
            buttonInterfaceCustomization.setFocusPainted(false);
            buttonInterfaceCustomization.setBorder(null);
            buttonInterfaceCustomization.setContentAreaFilled(false);
            buttonInterfaceCustomization.setCursor(new java.awt.Cursor(
                    java.awt.Cursor.HAND_CURSOR));
            panelSettingsMain.add(buttonInterfaceCustomization,
                    "gapleft 10, gapbottom 15px, wrap");

    }

}

我正在考虑通过将 JPanels(如示例中的)设置为可见或不可见来浏览程序 GUI。

  1. 这是一个好方法吗?
  2. 我应该将我的 GUI 分成几个类,还是应该将所有内容合二为一?我在问,因为现在代码中只有一半的 GUI 大约有 400 行长(此时它除了“看”之外什么也做不了)。正如我之前所说 - 我是初学者,它是迄今为止我编写的最长的应用程序之一(我相信它不会那么长!)。

编辑

也许我想多了,所以最终是否可以拥有大的 GUI 类并通过设置它们是否可见来控制不同 GUI 区域的可见性?

编辑

我查看了 Oracle 的 CardLayout 教程,它看起来对我的任务很有帮助(不包括从外部类创建 JPanel,但没关系)。起初我误解了它,并且仅根据选项卡式窗格(我不想在我的项目中实现)来考虑 CardLayout。

4

4 回答 4

6

问题来自于JPanelOne课堂。它继承JPanel,但在构造函数中,您创建一个新的JPanel,然后向其添加一个按钮。如果您改为这样做:

public class JPanelOne extends JPanel {

   public JPanelOne() {
       JButton button = new JButton("test");
       add(button);
   }
}

它应该可以按您的预期工作。

于 2012-08-18T18:43:16.430 回答
6

首先要回答您的问题,您需要在 JFrameTest 构造函数中将面板的实例添加到框架中,如下所示:

add(new JPanelOne());

您还需要将按钮直接添加到 JPanelOne 本身:

public class JPanelOne extends JPanel {   

  public JPanelOne() {   
    JButton button = new JButton("test");   
    add(button);   
  }   
}

其次,我认为这几行代码有问题:

    FlowLayout mainLayout = new FlowLayout(); 
    // snip...
    setLayout(mainLayout); 

    JPanel panelMain = new JPanel(mainLayout); 

每个容器都应该有自己的布局管理器实例。否则你的 GUI 会做一些奇怪的事情:

setLayout(new FlowLayout());
JPanel panelMain = new JPanel(mainLayout);
于 2012-08-18T18:50:17.227 回答
5

借助更多帮助(来自其他站点的用户“Hilek”),我设法从另一个班级获得 JPanel,以便在主班上进行展示。这是代码:

JFrameTest.java:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameTest extends JFrame {

        private JButton testButton;
        private JPanel panelMain;
        private JPanelOne panel;

        public JFrameTest() {

                // setting up JFrame
                setLayout(null);
                setPreferredSize(new Dimension(420, 90));
                setResizable(false);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                // creating main JPanel (white)
                panelMain = new JPanel();
                panelMain.setBackground(Color.WHITE);
                panelMain.setBounds(0, 0, 420, 90);
                panelMain.setPreferredSize(new Dimension(200, 40));
                add(panelMain);

                // creating JButton in the main JPanel (white)
                testButton = new JButton("Button from main class");
                panelMain.add(testButton);

                // creating new JPanelOne object from JPanelOne class containing black JPanel
                panel = new JPanelOne();

                // adding black JPanel to main JPanel (white)
                panelMain.add(panel);

                pack();

        }

        public static void main(String[] arguments) {

                // creating JFrame object and setting it visible
                JFrameTest frame = new JFrameTest();
                frame.setVisible(true);

        }

}

JPanelOne.java:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class JPanelOne extends JPanel
{

        public JPanelOne()
        {
                // setting up black JPanel
                JPanel panel = new JPanel();
                panel.setPreferredSize(new Dimension(220, 40));
                panel.setBackground(Color.BLACK);

                // creating button on external JPanel
                JButton button = new JButton("Button (+JPanel) from external class");

                // adding button to the black JPanel
                panel.add(button);

                // adding blackJPanel
                add(panel);
        }
}

工作示例的打印屏幕:

http://i.stack.imgur.com/qKeBp.jpg

也许有人会发现它对他们的问题有帮助。

于 2012-08-19T10:37:26.333 回答
4

不要调用setSize()你的 JFrame。而是让布局设置其组件和 GUI 的适当大小。将所有组件添加到 GUI 后,调用pack()JFrame,然后调用setVisble(true). 请注意,大多数布局更尊重组件的 preferredSize 而不是其大小。

此外,您无需调用setVisible(true)各个组件(除非您在 GUI 启动并出于某种原因运行后更改它们的可见性)。您还需要阅读有关使用布局管理器的更多信息,并且在您研究过它们之后可能会更少使用 FlowLayout。

编辑
关于您最近的编辑:

也许我想多了,所以最后是否可以拥有大的 GUI 类并通过将它们设置为可见来控制不同 GUI 区域的可见性?

我会回答,有时这很有帮助,例如,如果您想将标准计算器更改为科学计算器,有时最好使用 setVisible(true) 简单地显示一个已创建的 JPanel,其中填充了高级计算按钮。另一方面,如果您想交换 GUI 的“视图”以反映其状态的重大变化,例如文字处理器从编辑模式更改为打印模式,您可以使用 CardLayout 轻松交换 JPanel 来执行此操作。

于 2012-08-18T18:40:37.747 回答