简体   繁体   English

GridBagLayout-图表和标签的放置

[英]GridBagLayout - placement of chart and labels

I am trying to place a JfreeChart together with some labels on a gridBagLayout. 我试图将JfreeChart和一些标签一起放在gridBagLayout上。 I would like the chart to be on the right hand side, taking all the space of the frame horizontally and vertically, and span 90% of the width of the frame.. The labels should be in equally sized cells, on the left of the graph, each taking 10% of witdth and 33% of height. 我希望图表位于右侧,水平和垂直地占据框架的所有空间,并跨越框架宽度的90%。标签应位于大小相等的单元格中,在左侧图中,每人占智慧的10%,身高的33%。 SO far the code looks like this: 到目前为止,代码如下所示:

 val chartPanel = new ChartPanel(chart, true, true, true, true, true)

  //layout
  setLayout(new GridBagLayout())
  val c = new GridBagConstraints()

  //natural height, maximum width
  c.fill = GridBagConstraints.BOTH
  c.gridx = 1
  c.gridy = 0
  c.weightx = 0.9
  add(chartPanel, c)

  val label = new JLabel("Count: ")

  val c2 = new GridBagConstraints()
  c2.fill = GridBagConstraints.VERTICAL
  c2.gridx = 0
  c2.gridy = 0
  c2.weightx = 0.1
  add(label, c2)

  val label2 = new JLabel("Count2: ")
  val c3 = new GridBagConstraints()
  c3.fill = GridBagConstraints.VERTICAL
  c3.gridx = 0
  c3.gridy = 1
  c3.weightx = 0.1
  add(label2, c3)


  val label3 = new JLabel("Count3: ")
  val c4 = new GridBagConstraints()
  c4.fill = GridBagConstraints.VERTICAL
  c4.gridx = 0
  c4.gridy = 2
  c4.weightx = 0.1
  add(label3, c4)

and the screen i am getting is this: 我得到的屏幕是这样的:

在此处输入图片说明

Furthermore, bizzarly, when I resize the screen I am seeing this: 此外,奇怪的是,当我调整屏幕大小时,我看到的是:

在此处输入图片说明

As if the whole chart got massively shrunk... Any ideas how to solve this? 好像整个图表都缩水了……有什么想法可以解决这个问题吗? Thanks a lot in advance 提前谢谢

您可以使用此frame.setResizable(false);

Whenever a GridBagLayout is shrunk to the point where the layout cannot accommodate components' preferred sizes, it immediately uses each component's minimum size instead. 每当GridBagLayout缩小到无法容纳组件的首选大小的程度时,它就会立即使用每个组件的最小大小。 This has the annoying effect of making everything become instantly tiny. 这具有使所有内容立即变小的烦人的效果。 (It is most commonly observed with JTextFields, whose minimum width is just a few pixels.) (最常见的情况是JTextFields,其最小宽度仅为几个像素。)

Fortunately, it's easy to solve: Just set each component's minimum size to its preferred size. 幸运的是,这很容易解决:只需将每个组件的最小尺寸设置为其首选尺寸。 For instance: 例如:

chartPanel.setMinimumSize(chartPanel.getPreferredSize())

Now, on to your use of weightx. 现在,继续使用weightx。 GridBagLayout weights are used for the extra space in the layout, so your labels should have a weightx of zero. GridBagLayout权重用于布局中的额外空间,因此标签的权重应为零。 That simply means they will never be given any extra space when the layout is larger than it needs to be (typically because the user made the frame larger by resizing it). 这只是意味着,当布局大于所需的大小时,将永远不会给它们任何额外的空间(通常是因为用户通过调整框架的大小来增大框架的大小)。

When all components have a weight of zero, any extra space is simply kept on the edges, causing the entire layout to be centered. 当所有组件的权重均为零时,任何多余的空间都会简单地保留在边缘上,从而使整个布局居中。 That is what you're seeing in the vertical axis of your layout. 那就是在布局的垂直轴上看到的。 Give each row the same positive weighty value, and the rows will stretch to fill the window vertically. 给每行相同的正权weighty值,行将拉伸以垂直填充窗口。

(Side note: Weights are not required to add up to 1, as they are relative to each other. So you could just do weighty = 1 for each chart row, and leave all other rows with a weighty of zero.) (旁注:权重不需要相加,因为权重相对于彼此是1。因此,您可以为每个图表行都设置weighty = 1weighty = 1所有其他行的权weighty为零。)

Why not give the labels 'the width they need' and the chart 'the rest'? 为什么不给标签“他们需要的宽度”和图表“其余”?

That could be done using a single column GridLayout of labels in the LINE_START of a BorderLayout . 这可以通过在BorderLayoutLINE_START中使用标签的单列GridLayout来完成。 Put the chart in the CENTER . 将图表放在CENTER

This typically happens because the available space is smaller then the components preferred size (this happens alot with JTextField ) 通常发生这种情况是因为可用空间小于组件的首选大小(这种情况在JTextField会发生很多)

You could also try returning a minimum size. 您也可以尝试返回最小尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM