我想使用可用的布局(例如 GridBagLayout
)为我的 Java 应用程序实现以下网格系统:
我设法创建了一个类似的布局,但没有仅使用 GridBagLayout
的较小按钮,但是当我尝试添加这些较小的按钮时,它不起作用,因为它的单元格大小不同。
我认为解决方案可以向 JFrame 添加多个布局,每个布局代表全局布局的一部分,但我找不到合适的布局。
是否可以使用一个 GridBagLayout 来创建所有较大的方形部分和两个大部分,一个包含左上角的项目,第二个包含以下内容8 个较小的按钮?
谢谢。
最佳答案
我“认为”您所说的是使用复合布局,其中您使用一个或多个子容器为特定区域执行特定布局,并将这些容器组合到具有另一种布局的另一个容器中,也许类似于...
因此,较小的 4x2
网格实际上是另一个 JPanel
,它负责该区域的具体细节(您可能可以使用 GridLayout
)然后将其合并到更大的布局中
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBackground(Color.BLACK);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.75;
gbc.weighty = 0.16;
gbc.gridwidth = 3;
gbc.insets = new Insets(4, 4, 4, 4);
add(makePanel(Color.DARK_GRAY), gbc);
gbc.weightx = 0.25;
gbc.gridx = 3;
gbc.gridwidth = 1;
add(makePanel(Color.ORANGE), gbc);
gbc.gridy++;
add(makePanel(Color.DARK_GRAY), gbc);
gbc.gridx = 0;
gbc.gridwidth = 3;
gbc.weightx = 0.75;
add(makeSmallerPane(), gbc);
gbc = new GridBagConstraints();
gbc.weightx = 0.25;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.weighty = 0.16;
gbc.fill = GridBagConstraints.BOTH;
for (int y = 2; y < 7; y++) {
gbc.gridx = y;
for (int x = 0; x < 4; x++) {
gbc.gridx = x;
Color color = Color.GRAY;
if (x == 3) {
color = Color.DARK_GRAY;
}
add(makePanel(color), gbc);
}
}
}
protected Component makePanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
protected Component makeSmallerPane() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setOpaque(false);
panel.setLayout(new GridBagLayout());;
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.25;
gbc.weighty = 0.5;
gbc.insets = new Insets(0, 0, 0, 4);
int topGap = 0;
int bottomGap = 4;
for (int y = 0; y < 2; y++) {
gbc.gridy = y;
for (int x = 0; x < 4; x++) {
gbc.gridx = x;
switch (x) {
case 0:
gbc.insets = new Insets(topGap, 0, bottomGap, 4);
break;
case 3:
gbc.insets = new Insets(topGap, 4, bottomGap, 0);
break;
default:
gbc.insets = new Insets(topGap, 4, bottomGap, 4);
break;
}
panel.add(makePanel(Color.DARK_GRAY), gbc);
}
topGap = 4;
bottomGap = 0;
}
return panel;
}
}
}
关于java - Swing - JFrame 中的多种布局架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33510071/