簡體   English   中英

無法在BorderLayout中垂直對齊水平JSeparator

[英]Unable to vertically align horizontal JSeparator in BorderLayout

使用BorderLayout我放了兩個不同的JButton,一個在左邊(西邊),一個在右邊(東邊),一個水平JSeparator在中間。 我想要做的是將分隔符與中心對齊,而不是像現在一樣對齊頂部。 我已經嘗試在分隔符上使用以下方法

setAlignmentY(CENTER_ALIGNMENT);

但它絕對沒有效果。 我錯過了什么? 如果不可能,沒有使用外部庫有沒有其他方法可以做到這一點?

這就是我得到的:

問題的形象

這就是我想要實現的目標:

解決方案的圖像

這是我正在使用的示例代碼(為了清楚起見,添加了頂部和底部的JPanels):

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

public class SeparatorTest extends JFrame{

    JButton btn1 = new JButton("button1");
    JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
    JButton btn2 = new JButton("button2");

    public SeparatorTest() {
        getContentPane().add(BorderLayout.NORTH, new JPanel());
        getContentPane().add(BorderLayout.WEST, btn1);
        getContentPane().add(BorderLayout.CENTER, sep);
        getContentPane().add(BorderLayout.EAST, btn2);
        getContentPane().add(BorderLayout.SOUTH, new JPanel());

        setSize(300, 85);
    }

    public static void main(String[] args){
        new SeparatorTest().setVisible(true);
    }
}

編輯1:我不介意布局,只要它看起來一樣,我在這里使用BorderLayout由於它的簡單性。

這將有該怎么辦JSeparator UI委托決定如何繪制組件,其中,根據你的測試,似乎要永遠畫開始在分隔y的位置0

相反,您可能需要將JSeparator包裝在另一個面板中,該面板可以使用滿足您需求的不同布局管理器,例如GridBagLayout (當然您可以使用GridBagLayout開始,但我的目標是盡可能小的更改) ;))

紐扣

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SeparatorTest extends JFrame {

    JButton btn1 = new JButton("button1");
    JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
    JButton btn2 = new JButton("button2");

    public SeparatorTest() {

        JPanel pane = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        pane.add(sep, gbc);

        getContentPane().add(BorderLayout.NORTH, new JPanel());
        getContentPane().add(BorderLayout.WEST, btn1);
        getContentPane().add(BorderLayout.CENTER, pane);
        getContentPane().add(BorderLayout.EAST, btn2);
        getContentPane().add(BorderLayout.SOUTH, new JPanel());

        setSize(300, 85);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                SeparatorTest frame = new SeparatorTest();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM