问题
I have a simple problem here in Java Swing. I simplified my code to the following snippet. I am not sure how I can minimize the gap size between the horizontal JSeparator with the next JTextField, as current code generates a huge gap between the two.
GroupLayout layout = new GroupLayout(jPanel1);
jPanel1.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(button)
))
.addComponent(jSeparator)
.addComponent(jTextField)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(button)
.addComponent(jSeparator)
.addComponent(jTextField)
);
And also in general, how can I control the gap size to any integer represented value, instead of using the addPreferredGap
?
Thank you.
Okay, this is the window generated from the code posted above:
You can see the space between the JSeparator and JTextField is very wide.
回答1:
Absent your sscce, the problem appears to be in the code you don't show. The parent container's layout or pack()
may be involved. Note that the default layout of JFrame
is BorderLayout
; the default position is CENTER
. Here's an sscce with which to compare your code.
Addendum: Commenting that the parent of your GroupLayout
panel is another JPanel
, you asked the following,
Do you know how to make this work in my situation?
Yes, give the enclosing JPanel
a suitable layout, e.g. GridLayout
as shown below. The latter behaves much like the BorderLayout.CENTER
of the JFrame
in this regard.
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
/** @see http://stackoverflow.com/questions/6769722 */
public class GroupPanel extends JPanel {
private final JButton button = new JButton("Start");
private final JSeparator jSeparator = new JSeparator();
private final JTextField jTextField = new JTextField(10);
public GroupPanel() {
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createParallelGroup()
.addComponent(button)
.addComponent(jSeparator)
.addComponent(jTextField)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(button, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jSeparator, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
);
}
private static void display() {
JFrame f = new JFrame("GroupPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(1, 0));
f.add(new GroupPanel());
f.add(new GroupPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
}
回答2:
in the vertical layout, add the separator in the following way:
addComponent(separator, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
来源:https://stackoverflow.com/questions/6769722/gap-size-after-placing-jseparator-in-java-swing