将Jpanel添加到Jframe NetBeans

Abe*_*ojo 2 java user-interface swing netbeans jpanel

在此输入图像描述

大家好,我正在开发我的大学迷你项目.这是一个图书馆管理系统,我应该使用Net-beans IDE在Java中进行.首先我做了手动编码.这需要时间.

在手动编码中,我使用菜单栏和项目创建单个JFrame,在执行的所有操作中,我为所有Jpanels编写代码.它使文件和代码变得庞大.而且也很困惑.

现在我需要你的帮助.我创建了一个带有菜单A JPanel的主JFrame - 添加另一个Jpanel - 添加书籍成功(演示!,对于ADD书中发生的操作)

我做了动作听众

addBook addbk = new addBook();
this.getContentPane().add(addbk);
Run Code Online (Sandbox Code Playgroud)

写了这段代码.我没有意义.朋友们,作为java的新手,我需要学习的是什么

1.)如何执行cal和显示外部Jpanel执行的操作2.)如果在外部JPanel中发生任何事件,如何向另一个根JFrame显示另一个JPanel.

在排序中,像HTML中的iframe一样谢谢大家.

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

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

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

nIc*_*cOw 5

CardLayout,正是这种情况所需要的.并且学习Java命名约定并坚持使用它们,因为您是初学者,因此从一开始就走在正确的轨道上总是很好.

这是一个例子,你可以看看:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,你也可以看看这个例子.