JFrame 中的两个 JPanel 并排,其中一个 JPanel 具有固定宽度 [英] Two JPanels side by side in a JFrame with one JPanel having a fixed width
问题描述
为了方便起见,我在下面创建了一个我想要的图:
To make things easier, I've created a figure below of what I want:
基本上,左侧面板显示图像,右侧面板显示颜色条.我已经有两个面板类可以按照我想要的方式显示图像和颜色条,但现在我想将它们并排放置在 JFrame
中.现在,我不确定最好的实现是什么,所以如果有人有任何概念上不同的想法,那么他们将不胜感激.
Basically, the panel on the left displays an image and the panel on the right will display a colorbar. I've already got two panel classes that display the image and colorbar how I want, but now I want to put them side by side inside a JFrame
. Now, I'm not exactly sure what the best implementation would be for this so if anyone has any conceptually different ideas then they would be appreciated.
我已经尝试过 GridLayout
,但它似乎以百分比来定位事物.关于此问题的一个棘手问题是,我希望右侧的面板具有固定宽度,因为我不希望颜色条在调整框架大小时改变宽度.
I've already tried a GridLayout
, but it seemed to position things in terms of percentages. The one tricky thing about this is that I'd like the panel on the right to be a fixed width because I don't want the colorbar to change widths as the frame is resized.
推荐答案
好的,JB Nizet 很容易回答,但这是我使用的代码:
Ok, well JB Nizet answered it pretty easily, but here is the code I used:
public class Data2DFrame extends JFrame{
public Data2DFrame(Data2D data2D,double min,double max,Data2DColorMap data2DColorMap) {
// Set image if data is nonempty
if (data2D.height > 0 && data2D.width > 0) {
// Set data panel
Data2DPanel data2DPanel = new Data2DPanel(data2D,min,max,data2DColorMap);
data2DPanel.toggleKeyBoard(); // Allow keyboard interaction
data2DPanel.toggleMouse(); // Allow mouse interaction
// data2DPanel.setPreferredSize(new Dimension(data2D.width,data2D.height));
// Set colorbar
Data2DColorBar data2DColorBar = new Data2DColorBar(min,max,data2DColorMap);
// data2DColorBar.setPreferredSize(new Dimension(100,data2D.height));
// Arrange
this.setLayout(new BorderLayout());
this.add(data2DPanel,BorderLayout.CENTER);
this.add(data2DColorBar,BorderLayout.LINE_END);
this.pack();
}
}
}
这篇关于JFrame 中的两个 JPanel 并排,其中一个 JPanel 具有固定宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!