两半Java JButton(+/-)

时间:2018-07-16 21:07:11

标签: java swing jbutton

对不起,但我什至不知道该如何表达这个问题,因为我缺乏术语。基本上,我想知道如何创建它:

enter image description here

或者这个:

enter image description here

在Java中,多个JButton占据一个空间,它们之间有一条细线。有人可以给我一些指示吗?我已经搜索了一段时间,但无法弄清楚。谢谢!

2 个答案:

答案 0 :(得分:2)

您想要的东西没有直接支持。

您可以将JPanel与水平BoxLayout一起使用。

然后使用Border从每个按钮中删除setBorder(null)

然后您可以在面板中添加Border,以使按钮看起来像共享相同的Border

然后您可以在每个按钮之间添加一个JSeparator

答案 1 :(得分:2)

一个人可以创建一组按JToolBar分组的按钮(或其他窗口小部件)。有setBorderPainted()setFloatable()等方法可自定义工具栏属性。这是一个基本示例:

import javax.swing.*;
public class MyApp {
    public static void main(String[] args) {
        displayGui();
    }
    private static void displayGui() {
        JToolBar toolBar = new JToolBar();
        JButton saveButton = new JButton("Save");
        JButton deleteButton = new JButton("Delete");
        toolBar.add(saveButton);
        toolBar.add(deleteButton);
        JFrame frame = new JFrame("My App");
        frame.add(toolBar);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(100, 100);
        frame.setLocation(100, 100);
        frame.setVisible(true);
    }
}

编辑: 工具栏示例(仅图像):

enter image description here

相关问题