gpt4 book ai didi

java - Swing:创建一个有 3 个边框的 CompoundBorder?

转载 作者:行者123 更新时间:2023-12-01 08:55:52 27 4
gpt4 key购买 nike

我正在创建一个 JFrame 对象,其中有一些 JPanel 并排放置。

我希望 JPanel 具有 15 像素的边距、 eclipse 刻边框和 15 像素的填充。起初我认为这会像 HTML 框模型一样非常直观,所以我尝试在 CompoundBorder 中创建 CompoundBorder,但这行不通。

这是我的代码:

import java.awt.Dimension;
import javax.swing.*;

public class StackOverFlowExample extends JFrame {

public static void main() {
stackOverFlowExample window = new stackOverFlowExample();
window.setVisible(true);
}

public StackOverFlowExample() {
// create buttons
JButton foo = new JButton("foo");
JButton bar = new JButton("bar");
JButton foo2 = new JButton("foo2");
JButton bar2 = new JButton("bar2");

// create panels and add buttons to them
JPanel left = new JPanel();
left.setBorder(BorderFactory.createEtchedBorder());
left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
left.add(foo);
left.add(bar);
JPanel right = new JPanel();
right.setBorder(BorderFactory.createEtchedBorder());
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
right.add(foo2);
right.add(bar2);

// add panels to frame
this.getContentPane().setLayout(new BoxLayout(
getContentPane(), BoxLayout.LINE_AXIS));
this.getContentPane().add(left);
this.getContentPane().add(right);

// finalize layout
this.setPreferredSize(new Dimension(150,150));
this.pack();
this.setVisible(true);
}
}

我知道我可以只使用 GridBagConstraints 或 JButton.setMargin() 创建填充,然后使用 CompoundBorder 创建带有空边框的 eclipse 刻边框。如果我不想让我的代码因这些技术而显得困惑怎么办?

最佳答案

我不确定你可能遇到什么问题,因为你没有提供你尝试过的例子,但基本过程是......

  • 创建内边框要求(EtchedBorder包裹一个EmptyBorder),例如,new CompoundBorder(emptyBorder, etchedBorder)
  • 创建外边框要求(EmptyBorder包裹内复合边框),例如,new CompoundBorder(inner, emptyBorder);
  • 将此外边框应用于组件...

举个例子...

Border

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

public class Test1 {

public static void main(String[] args) {
new Test1();
}

public Test1() {
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() {

setLayout(new GridBagLayout());

EmptyBorder emptyBorder = new EmptyBorder(15, 15, 15, 15);
EtchedBorder etchedBorder = new EtchedBorder();

CompoundBorder inner = new CompoundBorder(emptyBorder, etchedBorder);
CompoundBorder outter = new CompoundBorder(inner, emptyBorder);

JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(outter);

panel.add(new JButton("Hello"));

add(panel);

}

}

}

关于java - Swing:创建一个有 3 个边框的 CompoundBorder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25227777/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com