Java 使用flowLayout将控制栏按钮向左摆动

Java 使用flowLayout将控制栏按钮向左摆动,java,swing,jframe,Java,Swing,Jframe,(来源:) 你能看到在关闭按钮的最右边有1号线或2号线吗?要获得更好的质量,请选择视频: (来源:) 我真的不确定是什么原因造成的。我使用FlowLayout作为包含所有按钮的组件,使用gridLayout作为主要组件的布局,使用BorderLayout作为JFrame的布局 我试着调试,但没有成功 我是新来的布局,他们是可怕的,但有时很难 这是我的视图类,负责窗口的整个标题部分: public class ControllBar extends JPanel implements ViewC


(来源:)

你能看到在关闭按钮的最右边有1号线或2号线吗?要获得更好的质量,请选择视频:


(来源:)

我真的不确定是什么原因造成的。我使用FlowLayout作为包含所有按钮的组件,使用gridLayout作为主要组件的布局,使用BorderLayout作为JFrame的布局

我试着调试,但没有成功

我是新来的布局,他们是可怕的,但有时很难

这是我的视图类,负责窗口的整个标题部分:

public class ControllBar extends JPanel implements ViewComponent {

    private static final long serialVersionUID = 8031230498064473495L;
    
    private ControllbarController c;
    
    private JPanel close;
    private JPanel minimize;
    private JPanel down;
    
    public ControllBar(ControllbarController c) {
        this.c = c;
    }
    
    @Override
    public void init() throws Exception {
        super.setLayout(new GridLayout(1, 0));
        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        buttonsPanel.setBackground(new Color(0, 0, 0, 0));
        
        JLabel text = new JLabel(c.getFrame().getTitle(), JLabel.LEFT);
        text.setForeground(Color.WHITE);
        text.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
        
        super.add(text);

        
        this.down = this.createToolButton(new Down(this, c), new ImageIcon("assets/down.png"));
        this.minimize = this.createToolButton(new Minimize(this, c), new ImageIcon("assets/minimize.png"));
        this.close = this.createToolButton(new Close(this, c), new ImageIcon("assets/close.png"));

        buttonsPanel.add(down);
        buttonsPanel.add(minimize);
        buttonsPanel.add(close);
        
        super.add(buttonsPanel);
        super.setBackground(new Color(0x8F8F8F));
        super.setSize(c.getFrame().getWidth(), 30);
    }
    
    private JPanel createToolButton(MouseListener listener, ImageIcon icon) {
        JPanel button = new JPanel();
        button.setLayout(new GridBagLayout());
        button.setPreferredSize(new Dimension(30, 30));
        button.setBackground(new Color(0x8F8F8F));
        button.add(new JLabel(icon));   
        
        button.addMouseListener(listener);
        return button;
    }
}
这是我的视窗课:

public class Window extends JFrame {

    private static final long serialVersionUID = 1L;
    
    public Window() {
        super(Screen.getTitle());
        super.setSize(new Dimension(Screen.getWidth(), Screen.getHeight()));
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setUndecorated(true);
        super.setLocationRelativeTo(null);
        super.setLayout(new BorderLayout());

    }
    
    public void init(Collection<Controller<?>> list) {
        for (Controller<?> controller : list) {
            super.add(controller.getView(), controller.getPosition());
        }
    }
    
    public void setShowState(boolean b) {
        super.setVisible(b);
    }
}

为什么?

除非调用,否则框架的这一部分应该与操作系统上的其他框架一样。。要更快地获得更好的帮助,请发布(最小、完整、可验证的示例)。编辑、添加问题要更快地获得更好的帮助,请发布(最小、完整、可验证的示例)。
    JLabel text = new JLabel(c.getFrame().getTitle(), JLabel.LEFT);
    text.setForeground(Color.WHITE);
    text.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
    
    super.add(text);