将Jpanel添加到Jframe NetBeans

编程入门 行业动态 更新时间:2024-10-27 11:26:41
本文介绍了将Jpanel添加到Jframe NetBeans的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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. First I was doing manual coding. It takes time.

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

In manual coding I create single JFrame with Menu bar and and items, on all action performed I wrote codes for all Jpanels. It made the file and code huge. and confusing too.

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

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 )

我已经设置了动作监听器

I had made action listener

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

编写此代码. 我没有道理 朋友,作为Java的新手,我需要学习

wrote this code. I doesn't make sense. Friends, As a new to java, What i need to study is

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

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.

某种程度上,类似于HTML中的iframe 谢谢大家.

In sort, something like Iframe in HTML Thank you all.

compilr/abelkbil/openlib/OpenLibMainGUI.java

compilr/abelkbil/openlib/addBook.java

compilr/abelkbil/openlib/bookAdded.java

推荐答案

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

CardLayout, is exactly what you need for this situation. 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.

这里是一个示例,您可以看一下:

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.

更多推荐

将Jpanel添加到Jframe NetBeans

本文发布于:2023-08-06 03:33:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Jpanel   Jframe   NetBeans

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!