lemon
6 months ago
6 changed files with 34 additions and 76 deletions
@ -1,46 +0,0 @@
|
||||
package com.fr.design.condition; |
||||
|
||||
import javax.swing.*; |
||||
import java.awt.*; |
||||
|
||||
public class FlowLayoutExample { |
||||
public static void main(String[] args) { |
||||
JFrame frame = new JFrame("FlowLayout Example"); |
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
||||
frame.setSize(400, 300); |
||||
|
||||
// 创建一个主 JPanel 使用 BorderLayout
|
||||
JPanel mainPanel = new JPanel(new BorderLayout()); |
||||
|
||||
// 创建一个居中面板 使用 FlowLayout 并设置为左对齐
|
||||
JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
||||
|
||||
// 添加一些组件到 rowPanel
|
||||
rowPanel.add(new JButton("Button 1")); |
||||
rowPanel.add(new JButton("Button 2")); |
||||
rowPanel.add(new JButton("Button 3")); |
||||
|
||||
// 创建一个带有 GridBagLayout 的面板,用于垂直居中 rowPanel
|
||||
JPanel centerPanel = new JPanel(new GridBagLayout()); |
||||
|
||||
// 设置 GridBagConstraints 以在垂直方向居中 rowPanel
|
||||
GridBagConstraints gbc = new GridBagConstraints(); |
||||
gbc.gridx = 0; |
||||
gbc.gridy = 0; |
||||
gbc.weightx = 1; |
||||
gbc.weighty = 1; |
||||
gbc.anchor = GridBagConstraints.NORTH; |
||||
gbc.fill = GridBagConstraints.HORIZONTAL; |
||||
|
||||
centerPanel.add(rowPanel, gbc); |
||||
|
||||
// 将居中面板添加到主面板的中间
|
||||
mainPanel.add(centerPanel, BorderLayout.CENTER); |
||||
centerPanel.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black)); |
||||
|
||||
// 将主面板添加到 JFrame
|
||||
frame.add(mainPanel); |
||||
|
||||
frame.setVisible(true); |
||||
} |
||||
} |
Loading…
Reference in new issue