You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Swing中GridLayout布局按钮尺寸与位置异常问题求助

Hey there! Let's work through these Swing layout headaches together—they're totally normal when you're trying to line up components just right. Here's how to fix both of your windows:

1. Fixing the AdminToken Window Layout

Your current GridLayout(5,1) is making every component take up an entire row, which is why your labels and delete buttons aren't side-by-side. The fix is to wrap each label-button pair in a small container panel, then add those panels to your main GridLayout.

Here's a revised code snippet for this window:

// Initialize the main layout with vertical rows (5 rows, 1 column) and add spacing
setLayout(new GridLayout(5, 1, 10, 10)); // The last two numbers are horizontal/vertical gaps

// Create a row for each token
for (int i = 1; i <= 5; i++) {
    // Use a FlowLayout panel to hold the label and button side-by-side
    JPanel tokenRowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
    
    JLabel tokenInfoLabel = new JLabel("Token " + i + ": abc123xyz"); // Replace with your actual token data
    JButton deleteButton = new JButton("Löschen");
    
    // Add components to the row panel
    tokenRowPanel.add(tokenInfoLabel);
    tokenRowPanel.add(deleteButton);
    
    // Add the row panel to the main window
    add(tokenRowPanel);
}

This way, each row is a self-contained panel where the label and button sit next to each other, and the main GridLayout stacks these rows vertically.

2. Fixing the TsServerOverview Window Issues

You've got two problems here: unmodifiable button sizes, and server info not aligning with action buttons. Let's tackle both at once with nested panels:

Step 1: Make Buttons Resizable & Align with Server Info

Instead of adding components directly to the main window, create a panel for each server row. Use BorderLayout to split the row into left (server info) and right (action buttons) sections, and set preferred sizes for your buttons if you need consistent dimensions.

Here's how to implement this:

// Main layout: vertical stack of server rows (0 = auto-adjust number of rows)
setLayout(new GridLayout(0, 1, 15, 15));

// Example for one server row (repeat for each server)
JPanel serverRowPanel = new JPanel(new BorderLayout(10, 0)); // Gap between info and buttons

// Panel for server information (add all your server details here)
JPanel serverInfoPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
serverInfoPanel.add(new JLabel("Server: TeamSpeak Server 1"));
serverInfoPanel.add(new JLabel("Status: Online"));
serverInfoPanel.add(new JLabel("Port: 9987"));

// Panel for action buttons
JPanel actionButtonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton consoleBtn = new JButton("Konsole öffnen");
JButton startBtn = new JButton("Starten");
JButton stopBtn = new JButton("Stoppen");

// Set consistent button dimensions (adjust as needed)
Dimension buttonSize = new Dimension(130, 35);
consoleBtn.setPreferredSize(buttonSize);
startBtn.setPreferredSize(buttonSize);
stopBtn.setPreferredSize(buttonSize);

// Add buttons to their panel
actionButtonsPanel.add(consoleBtn);
actionButtonsPanel.add(startBtn);
actionButtonsPanel.add(stopBtn);

// Assemble the row
serverRowPanel.add(serverInfoPanel, BorderLayout.WEST);
serverRowPanel.add(actionButtonsPanel, BorderLayout.EAST);

// Add the row to the main window
add(serverRowPanel);

Why This Works:

  • The BorderLayout ensures server info stays left-aligned and buttons stay right-aligned in the same row.
  • Setting setPreferredSize() gives you control over button dimensions (if you don't want Swing's auto-sized buttons).
  • Using nested panels keeps your layout organized and easy to adjust later.

A quick pro tip: Avoid using absolute layout (null layout) in Swing—it makes your app break on different screen sizes and is a nightmare to maintain. Stick to combining layout managers like we did here!

内容的提问来源于stack exchange,提问作者FileInputStream

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:10:53