简体   繁体   English

将Jpanel添加到Jframe NetBeans

[英]Add Jpanel to Jframe NetBeans

在此处输入图片说明

Hi all, I was in a development of my college mini project. 大家好,我正在开发我的大学迷你项目。 It was a Library Management system , which i should do in Java swing using Net-beans IDE. 这是一个图书馆管理系统,我应该使用Net-beans IDE在Java swing中进行。 First I was doing manual coding. 首先,我正在做手动编码。 It takes time. 这需要时间。

In manual coding I create single JFrame with Menu bar and and items, on all action performed I wrote codes for all Jpanels. 在手动编码中,我创建了带有菜单栏和项目的单个JFrame,在执行的所有操作中,我为所有Jpanel编写了代码。 It made the file and code huge. 它使文件和代码变得巨大。 and confusing too. 也令人困惑。

Now I need your help. 现在,我需要您的帮助。 I have created a Main JFrame with Menu A JPanel - ADD Book another Jpanel - On add book success (demo! , for Actions happening in ADD Book ) 我用菜单A JPanel创建了一个主JFrame-添加了另一个Jpanel-添加成功后(演示!,用于添加Book中发生的动作)

I had made action listener 我做了动作监听器

addBook addbk = new addBook();
this.getContentPane().add(addbk);

wrote this code. 写下了这段代码。 I doesn't make sense. 我没有道理 Friends, As a new to java, What i need to study is 朋友们,作为Java的新手,我需要学习的是

1.) How cal and Show an external Jpanel an action performed 2.) How to show another JPanel to same root JFrame if any event has occurred in external JPanel. 1.)如何校准并向外部JPanel显示动作。2.)如果外部JPanel中发生任何事件,如何向同一根JFrame显示另一个JPanel。

In sort, something like Iframe in HTML Thank you all. 在某种程度上,类似HTML中的Iframe的谢谢大家。

http://compilr.com/abelkbil/openlib/OpenLibMainGUI.java http://compilr.com/abelkbil/openlib/OpenLibMainGUI.java

http://compilr.com/abelkbil/openlib/addBook.java http://compilr.com/abelkbil/openlib/addBook.java

http://compilr.com/abelkbil/openlib/bookAdded.java http://compilr.com/abelkbil/openlib/bookAdded.java

CardLayout , is exactly what you need for this situation. CardLayout正是这种情况所需的。 And do learn Java Naming Conventions and stick to them, as you are a beginner, so to be on the right track from the start is always GOOD. 而且,作为初学者,请务必学习并遵守Java命名约定 ,因此从一开始就走在正确的道路上永远是好的。

Here is one example, that you can look at : 这是一个示例,您可以看一下:

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

public class CardLayoutExample
{
    private JPanel contentPane;
    private MyPanel panel1;
    private MyPanel panel2;
    private MyPanel panel3;
    private JComboBox choiceBox;
    private String[] choices = {
                                "Panel 1",
                                "Panel 2",
                                "Panel 3"
                               };

    private void displayGUI()
    {
        JFrame frame = new JFrame("Card Layout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new CardLayout());

        choiceBox = new JComboBox(choices);        

        panel1 = new MyPanel(contentPane
                , Color.RED.darker().darker(), this);
        panel2 = new MyPanel(contentPane
                , Color.GREEN.darker().darker(), this);
        panel3 = new MyPanel(contentPane
                , Color.DARK_GRAY, this);   

        contentPane.add(panel1, "Panel 1"); 
        contentPane.add(panel2, "Panel 2");
        contentPane.add(panel3, "Panel 3");         

        frame.getContentPane().add(choiceBox, BorderLayout.PAGE_START);
        frame.getContentPane().add(contentPane, BorderLayout.CENTER);       
        frame.pack();   
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public JComboBox getChoiceBox()
    {
        return choiceBox;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new CardLayoutExample().displayGUI();
            }
        });
    }
}

class MyPanel extends JPanel 
{

    private JButton jcomp1;
    private JPanel contentPane;
    private Color backgroundColour;
    private JComboBox choiceBox;

    public MyPanel(JPanel panel, Color c, CardLayoutExample cle) 
    {   
        contentPane = panel;
        backgroundColour = c;
        choiceBox = cle.getChoiceBox();

        setOpaque(true);
        setBackground(backgroundColour);

        //construct components
        jcomp1 = new JButton ("Show New Panel");
        jcomp1.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String changeToPanel = (String) choiceBox.getSelectedItem();
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.show(contentPane, changeToPanel);
            }
        });

        add(jcomp1);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 500));
    }
}

Else you can have a look at this example also. 否则,您也可以查看此示例

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

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