我试图制作我的大学网站的用户界面,但我遇到了一个问题,按钮周围有一个奇怪的边框

现在看起来是这样的:

https://i.stack.imgur.com/Q1neJ.png

它应该是这样的:

https://i.stack.imgur.com/Ir0De.png

public static void middleBar(JFrame frame){
        JPanel middle = new JPanel();
        middle.setLayout(new GridLayout(4,6,20,20));
        String[] items = {"UniversityERP","Award","Blackboard","CCT","CertificateIssuance","CourseEvaluationSurvey","DoctoralPortal",
        "Fastrack","HostelManagement","IDCardManagement","MobileAppCMS","OnCampusJob","StudentOutboundMobility","StudentAttendanceRecording","StudentAttendanceManagement",
        "StudentClearance","StudentPaymentCenter"};
        for(String item:items){
            ImageIcon icon = new ImageIcon("Images/"+item+".png");
            JButton button = new JButton(icon);
            Dimension d = new Dimension(icon.getIconWidth(),icon.getIconHeight());
            button.setPreferredSize(d);
            middle.add(button);
        }
        JPanel middleBar = new JPanel(new BorderLayout());
        middleBar.add(middle,BorderLayout.NORTH);
        frame.add(middleBar,BorderLayout.CENTER);
    }

我怎么把它移走?

我try 使用setPferedSize方法,但不起作用

推荐答案

正如gthan op所提到的,用户应该将边距设置为零.

But just fyi what is a margin? 边距很容易理解,但它是来自边界的(任何内容)之间的差距.您可以使用它在Insets中使用的button.setMargin(new Insets(top, left, bottom, right))来设置边距,它定义了该偏移.下面是它是如何被给予in the docs

java.awt.Insets.Insets(int top, int left, int bottom, int right)


Creates and initializes a new Insets object with the specified top, left, bottom, and right insets.
Parameters:
top - the inset from the top.
left - the inset from the left.
bottom - the inset from the bottom.
right - the inset from the right.

Let's see it in an example

cool lil button

我有一个很酷的小纽扣.我还没有修改页边距.

cool lil button num 2

在这里,我将边距插入修改为top=1、Bottom=10、Right=10和Left=10.

cool lil buttons lil bro

这是所有插图都是零的情况,当你使用像这样的图像时,这一点更加明显

surprised bro

然而,这并不能完全消除边界,而是在外部留下一条小界线,这是border of the button,我们用button.setMargin(...);go 掉的是缺口,而不是边界.既然我们不需要它,我们就可以把它移走.

button.setBorder(null);

或者使用另一种 Select

Border emptyBorder = BorderFactory.createEmptyBorder();
button.setBorder(emptyBorder);

cool shi6

事情应该是这样结束的.

这将删除边框,并使图像覆盖整个按钮,按钮将完全正常工作.

然而,我会建议你创建一个自定义的按钮或至少按钮的用户界面,这将是更容易和可靠的创建新的按钮相比,粘贴屏幕截图的按钮,特别是动画按钮,所有都需要更多的努力.但如果它起作用,你就让它保持(trust me i know from my past experiences ;))

Java相关问答推荐

在FML中删除关键帧动画

Java 22模式匹配不适用于记录模式匹配.给出汇编问题

Java 21虚拟线程会解决转向react 式单线程框架的主要原因吗?

Spring boot:Bean和动态扩展器

CSS应用于一个端点,但不应用于Thymeleaf中的另一个端点

Java WireMock定义存根在Cucumber并行执行的多线程测试中失败

';com.itextpdf.ext.html.WebColors已弃用

Chunk(Int)已弃用并标记为要删除

为什么Java Annotation接口覆盖对象类中的方法

在Spring Boot中使用哪个Java类来存储创建时间戳?

Javadoc在方法摘要中省略方法

用OSQL创建索引

Java中不兼容的泛型类型

Quarkus:运行时出现EnumConstantNotPresentException

为什么我不能建立输入/输出流?Java ServerSocket

处理4.3问题:javax.xml.ind包不存在(&Q;).您可能在学习GitHub教程时遗漏了库.&Q

基于距离的APACHE POI公式判断

当我将鼠标悬停在javafxTextArea上时,如何更改鼠标光标?

转换为JSON字符串时,日期按天递减-Java

可以';不要在Intellij IDEA中使用最新的Java版本(JDK 21)