我打算设计一个包含一个外部JPanel的gui程序,其中包含许多具有文本和按钮的重复JPanel。但是,我无法确定哪个布局适合这个任务。我希望它是这样的:如何选择嵌套JPanel的最佳布局?
我只是复制和粘贴,将你喜欢的图片中看到以编程的复发第一的JPanel。 为了得到这样的结果,我应该使用哪种布局?
我打算设计一个包含一个外部JPanel的gui程序,其中包含许多具有文本和按钮的重复JPanel。但是,我无法确定哪个布局适合这个任务。我希望它是这样的:如何选择嵌套JPanel的最佳布局?
我只是复制和粘贴,将你喜欢的图片中看到以编程的复发第一的JPanel。 为了得到这样的结果,我应该使用哪种布局?
在我的头上,它看起来是这样的:
JScrollPane
>JPanel (outerPane)
>JPanel (innerPane [many])
在此基础上,我们需要考虑其布局管理器是outerPane
要使用和innerPane
小号...
为了提供innerPane
S之间的间距我会去GridLayout (rows, columns, hgap, vgap)
像:
GridLayout(0, 1, 5, 5);
现在每个innerPane
我们可以去GridLayout
,GridBagLayout
或FlowLayout
,让我们看看会与每发生什么:
如果我们使用FlowLayout
的组件将不会在“网格”或“表”之类的布局,所以,这是一个没有没有。这是它会是什么样子:
Altough他们看起来像我们所需要的,我不知道如果每个标签GOI NG随着时间的推移至少调整时,它改变与否,但你可以尝试...
GridLayout
将使我们的JButton
s到取电池的整个空间,而不会好看( ),这里是一个图像与之前和调整的GUI后,显示我的意思(裁剪到之后不使用大量的空间):如果你的GUI不会调整你可以走这条路,如果可以的话,那你应该换个布局。
GridBagLayout
是我在这种情况下的最爱,因为每个标签会留在自己的栏和按钮将无法填补所有可用空间,我们的GUI看起来更像你张贴的形象:在上面的例子中,我使用的CustomBorder
类以提供innerPane
S和outerPane
,同时还提供AA彩色边框之间间距以及表示垂直JScrollPane
送花儿给人秒。
产生这些产出的代码是:
package sof;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.geom.Rectangle2D;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.AbstractBorder;
public class NestedPanels {
private JFrame frame;
private JPanel outerPane;
private JPanel innerPane;
private GridBagConstraints gbc;
private JScrollPane scrollPane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new NestedPanels()::createAndShowGui);
}
private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
outerPane = new JPanel();
outerPane.setLayout(new GridLayout(0, 1, 5, 5));
for (int i = 0; i < 5; i++) {
innerPane = new JPanel();
innerPane.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(10, 10, 10, 10);
innerPane.add(new JLabel("Recurring JLabel1"), gbc);
gbc.gridx++;
innerPane.add(new JLabel("Recurring JLabel2"), gbc);
gbc.gridx++;
innerPane.add(new JLabel("Recurring JLabel3"), gbc);
gbc.gridx++;
innerPane.add(new JButton("Recurring JButton1"), gbc);
innerPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
outerPane.add(innerPane);
}
outerPane.setBorder(new CustomBorder(5, Color.BLACK));
scrollPane = new JScrollPane(outerPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@SuppressWarnings("serial")
class CustomBorder extends AbstractBorder {
private int gap;
private Color color;
public CustomBorder(int gap, Color color) {
this.gap = gap;
this.color = color;
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(gap, gap, gap, gap);
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(color);
g2d.draw(new Rectangle2D.Double(x, y, width - 1, height - 1));
}
}
}
播放与边框样式以获得所需的一个,我画与GUI上-1像素的边界,如果我不” t将其只会显示左侧和顶部边框...
JTable
,但我留给你
看起来外面板应该使用垂直的“BoxLayout”,内部的面板可以使用水平的“BoxLayout”或“GridLayout”。看看[教程](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)。要得到明确的答案,您需要指定调整行为。 – user1803551
@ user1803551感谢您的链接,但是您指的是“指定调整大小行为”的含义 – gingerdd
调整框架大小时,您希望如何在组件之间分配空间? – user1803551