我需要JPanel吗?

huangapple go评论87阅读模式
英文:

Do I need JPanel always?

问题

我现在正在编写一个简单的GUI代码,用于启动游戏窗口。我只需要在窗口上添加“是否要开始游戏”消息和开始按钮。但是我对JFrameJPanel有一些混淆的概念。实际上,我以为我需要将JPanel添加到JFrame以添加其他组件,比如JLabelJButton等等。但我意识到我实际上不需要JPanel。我可以简单地使用add(button)add(label)将组件添加到JFrame中。那么为什么我需要JPanel呢?我认为JFrame不需要JPanel,但JPanel需要JFrame。我理解得对吗?

英文:

I am now writing code simple GUI that's for start the game window. I only need Do you want to start game message and start button on the window. But I have a confusing concepts for the JFrame and JPanel. Actually, I thought I need to add JPanel to JFrame to add the other components such as JLabel, JButton,...etc. But I realized I don't actually need JPanel. I can just add the components simply use add(button), add(label) to JFrame. So why I need JPanel. And I think JFrame doesn't need JPanel but JPanel need JFrame. Am I understand correctly?

答案1

得分: 1

不,不总是这样。一个简单的图形用户界面可以通过将组件直接添加到JFrame来实现。但是为了获得更大的灵活性,您通常会使用JPanels。例如,要在GUI的不同部分使用不同的布局,将某些组件分组等等。

JFrame由JRootPane支持,其中的一部分是contentPane。

当您向JFrame添加组件时,实际上是将它们添加到内容面板,例如:frame.getContentPane().add(Component)

JFrame是Swing GUI应用程序的常见起始场景,而JPanel旨在放置在另一个场景(容器)中。由于内容面板和JPanel都继承自相同的类(Container),因此在添加组件方面,您可以以类似的方式使用它们。

英文:

No, not always. A simple graphical user interface may be implemented by just adding components "directly" to a JFrame. But in order to get more flexibility, you would always use JPanels. For example, to employ different layouts in different parts of the GUI, to group certain components together, etc.

A JFrame is backed by a JRootPane, a part of which is a contentPane.

我需要JPanel吗?

(image from Oracle Javadoc)

When you add components to a JFrame, you are really adding them to the content pane, e.g.: frame.getContentPane().add(Component).

A JFrame is a common starting scene of a Swing GUI application, while a JPanel is intended to be put in another scene (container). Since both content pane and a JPanel inherit from the same class (Container) you may use them in a similar manner, as far as adding components to them goes.

答案2

得分: 1

不需要。除非您需要一个Swing GUI,那么是的。

另一个答案回复类似这样的话:“不,您可以直接将组件添加到框架中”。他们忽略了的是添加到JFrame的组件会自动添加到内容窗格。内容窗格就是一个JPanel

话虽如此:

  1. 我(以及许多其他人)建议设计一个基于主内容面板的应用程序,然后根据需要将该面板添加到顶级容器中。顶级容器可以是JFrameJWindowJDialogJOptionPane等。
  2. 是什么促使了这个问题?JPanel是一个非常“轻量级”的容器(在多个方面都是如此)。一个GUI可以包含上千个组件而不会因此而负担沉重。当然,这是一个罕见的需求,但只是说...根据需要使用面板,不用担心它。
英文:

> Do I need JPanel always?

No. Well, unless you need a Swing GUI. Then yes.

Another answer replied words to the effect. "No, you can add components direct to a frame" What they missed was that components added to a JFrame are added to the content pane (automatically). The content pane is a JPanel.

Having said that:

  1. I (and many others) would recommend designing an app based around a main content panel, then adding that panel to a top-level container as needed. The top level container might be a JFrame, JWindow, JDialog, JOptionPane ..
  2. What prompted the question? A JPanel is a very 'light weight' container (in more ways than one). A GUI can contain 1000s and not be burdened by doing so. Of course, that's a rare requirement, but just saying .. use panels as needed and don't worry about it.

huangapple
  • 本文由 发表于 2020年8月5日 15:03:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63260002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定