将JPanel从另一个类添加到JFrame中的JPanel(begginer java程序员)

前端之家收集整理的这篇文章主要介绍了将JPanel从另一个类添加到JFrame中的JPanel(begginer java程序员)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
请指出我正确的方向与这个任务.我不能从主类获取我的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);

    }

}

将不同的JPanels保留在自己的类中是否是一个好习惯? (例如:想要使JFrame包含几个相同大小的JPanels,这将通过将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,wrap");

            // setting up dark separator
            separatorSettingsTwo = new JSeparator();
            separatorSettingsTwo.setForeground(separatorGreenDark);
            panelSettingsMain.add(separatorSettingsTwo,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,wrap");

            // setting up dark separator
            separatorSettingsThree = new JSeparator();
            separatorSettingsThree.setForeground(separatorGreenDark);
            panelSettingsMain.add(separatorSettingsThree,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,wrap");

            // setting up dark separator
            separatorSettingsFour = new JSeparator();
            separatorSettingsFour.setForeground(separatorGreenDark);
            panelSettingsMain.add(separatorSettingsFour,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,wrap");

            // setting up dark separator
            separatorSettingsFive = new JSeparator();
            separatorSettingsFive.setForeground(separatorGreenDark);
            panelSettingsMain.add(separatorSettingsFive,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,wrap");

    }

}

我正在考虑通过将JPanels(例如在示例中)设置为可见或不可见来导航程序GUI.

这是好办法吗?
>我应该将我的GUI分成几个课程,还是把所有内容都放在一起?我在问,因为现在只有一半的代码中的GUI大约有400行长(并且它不能做任何事情,而只是“看”)在这一点上).正如我之前说过的 – 我是初学者,也是我迄今为止写的最长的应用程序之一(我相信这还不是很久)!

编辑

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

编辑

我已经看过Oracle中的CardLayout教程,看起来它对我的任务很有用(不包括从外部类创建JPanels,但是没关系).我首先误解了它,只是在标签窗格(我不想在我的项目中实现)方面考虑CardLayout.

感谢大家的耐心,有用的提示和答案!

解决方法

首先要回答你的问题,你需要在你的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);

猜你在找的Java相关文章