I am trying to place a JfreeChart together with some labels on a 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. 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. 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.)
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. GridBagLayout weights are used for the extra space in the layout, so your labels should have a weightx of zero. 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.
(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.)
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
. Put the chart in the CENTER
.
This typically happens because the available space is smaller then the components preferred size (this happens alot with JTextField
)
You could also try returning a minimum size.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.