【问题标题】:Add Jpanel to Jframe NetBeans将 Jpanel 添加到 Jframe NetBeans
【发布时间】:2012-08-30 08:29:13
【问题描述】:

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

在手动编码中,我使用菜单栏和项目创建单个 JFrame,在执行的所有操作中,我为所有 Jpanel 编写代码。它使文件和代码变得巨大。也很混乱。

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

我已经做了动作监听器

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

写了这段代码。 我没有道理。 朋友们,作为一个java新手,我需要学习的是

1.) 如何校准并显示外部 Jpanel 执行的操作 2.) 如果外部 JPanel 中发生任何事件,如何将另一个 JPanel 显示到同一个根 JFrame。

在排序上,类似于 HTML 中的 iframe 谢谢大家。

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

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

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

【问题讨论】:

  • 为了有意义,只需将其设置为可见。

标签: java swing user-interface netbeans jpanel


【解决方案1】:

CardLayout,正是您在这种情况下所需要的。并且学习Java Naming Conventions并坚持下去,因为你是初学者,所以从一开始就走在正确的轨道上总是好的。

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

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));
    }
}

否则你也可以看看这个example

【讨论】:

  • 或者使用validate()repaint()作为JFrame
  • @AbelJojo :这是一个 example 完全适合您的情况,尽管它使用 revalidate()/repaint() 而不是 CardLayout
  • @GagandeepBali 先生,你能不能来java聊天框chat.stackoverflow.com/rooms/139/java
  • @AbelJojo :我真的很抱歉,但在这个问题上,NetBeans 让我无法理解。我尝试了书中的每一个角度,但无法让它发挥作用,似乎手工编码比使用一个 IDE 更容易和有效:-)
猜你喜欢
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多