而不是创建一个LineBorder
, use a CompoundBorder
:
button2.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.RED, 1),
BorderFactory.createEmptyBorder(
button1.getBorder().getBorderInsets(button1).top,
button1.getBorder().getBorderInsets(button1).left,
button1.getBorder().getBorderInsets(button1).bottom,
button1.getBorder().getBorderInsets(button1).right)));
我拿了BorderInsets
为了button1
使它们具有相同的尺寸。
我的回答基于@MadProgrammer 的回答这个问题 https://stackoverflow.com/a/30239657/2180785
顺便说一句,不要延长JFrame
,创建它的一个实例,如果您确实需要扩展一个组件,可以是JPanel
: 扩展 JFrame 与在程序内创建它 https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program
也不要忘记打电话
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
On your JFrame
实例,以便您的程序在关闭时终止。
而且您也错过了将您的程序放在 EDT 上,请参阅更多内容这个答案 https://stackoverflow.com/a/41559787/2180785