如何在 JPanel 上覆盖组件、调整组件大小以及使其居中?
我在这里花了一段时间阅读和实验,并提出了一些方法,但还没有使它们中的任何一个完全工作,所以我想知道更有经验的 Swing 程序员会做什么。
我的应用程序的主窗口包含 JPanel 的自定义子类型,用于显示根据数学函数计算的图像。这可能需要一些时间来计算,因此当发生这种情况时,我会在面板中间显示一条文本消息和一个叠加的进度条。我希望在调整面板大小时,文本和进度条都能适当调整大小,可能使用面板宽度的恒定分数。
目前,我有另一个包含文本和进度栏的 JPanel 子类型,我将此“进度面板”添加到主面板,并将其设置为在需要时可见。主面板不包含任何其他组件,但有自己的paintComponent()方法来显示从另一个类获得的BufferedImage。目前的情况存在一些问题:
- 进度面板在应有的时候并不总是可见,可能是因为代码中没有任何地方明确确保它绘制在图像前面。
- 进度面板始终占据应用程序面板的整个宽度。它在主面板中使用 BoxLayout 垂直居中,并在进度面板之前和之后使用一些垂直粘合,但我还没有设法获得相同的技巧来水平工作,无论如何,进度面板的首选水平尺寸是不一定是对的。一种几乎可行的方法是在进度面板周围使用带有水平和垂直支柱的 BorderLayout,但进度面板的大小仍然不正确,并且在调整主面板大小时需要更改支柱。
那么你会怎么做?
- 在主面板中使用不同的布局管理器?
- 以某种方式使用 LayeredPane 或 OverlayLayout 吗?
- 向包含图像面板和进度面板的包含层次结构添加另一个层?
- 以上的某种组合,还是其他什么?
调整文本大小怎么样?调整面板大小时是否需要显式创建新字体,或者有什么方法可以更自动地执行此操作? -
I've spent a while reading and experimenting here, and come up with a few approaches, but not got any of them to work completely yet, so I would like to know what more experienced Swing programmers would do.
The main window of my application contains a custom subtype of JPanel, which is used to display an image calculated from a mathematical function. This can take some time to calculate, so while this happens I display a text message and a progress bar superimposed on the middle of the panel. I would like both the text and the progress bar to resize appropriately when the panel is resized, probably using a constant fraction of the width of the panel.
At the moment I have another JPanel subtype that contains the text and the progress bar, and I add this "progress panel" to the main panel, and set it to be visible when required. The main panel does not contain any other components, but has its own paintComponent() method to display a BufferedImage obtained from another class. There are a few problems with this as it stands:
- The progress panel is not always visible when it should be, perhaps because there is nowhere in the code that explicitly ensures it is painted in front of the image.
- The progress panel always takes up the full width of the application panel. It is centred vertically using a BoxLayout in the main panel with some vertical glue before and after the progress panel, but I haven't managed to get the same trick to work horizontally, and in any case the preferred horizontal size of the progress panel is not necessarily right. One approach that almost works is to use a BorderLayout with horizontal and vertical struts around the progress panel, but the size of the progress panel is still not right, and the struts need to be changed when the main panel is resized.
So what would you do?
- Use a different layout manager in the main panel?
- Use a LayeredPane or OverlayLayout somehow?
- Add a further layer to the containment hierarchy containing the image panel and the progress panel?
- Some combination of the above, or something else?
What about resizing the text. Do I need to explicitly create a new Font when the panel is resized, or is there any way to do this more automatically?
-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JProgressBar
的首选大小 由 UI 委托指定,BasicProgressBarUI
。下面的示例说明了各种布局管理器的效果。FlowLayout
仅使用UIManager
默认值ProgressBar.horizontalSize
,而GridLayout
和BorderLayout.CENTER
代码> 填充可用空间。BoxLayout
,带有侧翼胶水,随着框架大小的调整而按比例调整。从
SwingWorker
的process()
方法更新 GUI 应该是安全的。您可以更改图层甚至删除组件,但我会对任何过于复杂的内容保持警惕。附录:这是相关的默认值。
代码:
The preferred size of
JProgressBar
is specified by the UI delegate,BasicProgressBarUI
. The example below illustrates the effect of various layout managers.FlowLayout
simply uses theUIManager
default,ProgressBar.horizontalSize
, whileGridLayout
andBorderLayout.CENTER
fill the available space.BoxLayout
, with flanking glue, adjusts proportionally as the frame is resized.Updating the GUI from the
process()
method of yourSwingWorker
should be safe. You can change layers or even remove components, but I'd be wary of anything overly complicated.Addendum: Here's the relevant default.
Code:
我知道这是一个布局问题,但如果您有有用的临时结果,SwingWorker 可能会使问题变得更简单。我有时会从这个 示例开始。
I know this is a layout question, but
SwingWorker
might make the problem simpler if you have useful interim results. I sometimes start with this example.