要自定義 BorderLayout 的樣式,您可以使用以下方法:
import javax.swing.*;
import java.awt.*;
public class CustomBorderLayout extends BorderLayout {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 在這里添加自定義樣式代碼
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
JPanel northPanel = new JPanel();
northPanel.setBackground(Color.BLUE);
this.add(northPanel, BorderLayout.NORTH);
JPanel southPanel = new JPanel();
southPanel.setBackground(Color.GREEN);
this.add(southPanel, BorderLayout.SOUTH);
JPanel eastPanel = new JPanel();
eastPanel.setBackground(Color.YELLOW);
this.add(eastPanel, BorderLayout.EAST);
JPanel westPanel = new JPanel();
westPanel.setBackground(Color.CYAN);
this.add(westPanel, BorderLayout.WEST);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.MAGENTA);
this.add(centerPanel, BorderLayout.CENTER);
這將使得 BorderLayout 的每個區域具有不同的背景顏色。您可以根據需要自定義這些樣式。