我正在尝试创建一个JPanel
使用FlowLayout
. 该面板位于container
using 中BorderLayout
。然后container
使用 将其添加到卡片中CardLayout
。我希望使用FLowLayout
ot 的面板是面板左下角的一个小框BorderLayout
。我该怎么做?我已经使用过settingsPanel.setPreferredSize(new Dimension(10, 425));
,但无论我设置什么尺寸,宽度仍然跨越整个面板,尽管高度会发生变化。我将如何改变宽度?
代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Main {
//Settings Panel variables
JLabel settingsTitle = new JLabel("Settings");
public Main()
{
JPanel mainCard = new JPanel(new BorderLayout(8,8));
JPanel settingsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
settingsTitle.setFont(new Font("TimesRoman", Font.PLAIN, 35));
settingsPanel.add(settingsTitle);
final CardLayout layout = new CardLayout();
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
mainCard.add(settingsPanel, BorderLayout.SOUTH);
settingsPanel.setPreferredSize(new Dimension(10, 425));
settingsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
layout.show(cards, "1");
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.setSize(1280, 720);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});
}
}
例子: