1

BorderLayout を使用して、左 (西) と右 (東) に 1 つずつ、2 つの異なる JButton を配置し、中央に水平 JSeparator を配置しました。私がやりたいことは、セパレーターを現在のように上ではなく、中央に y 揃えすることです。セパレーターで次の方法を使用しようとしました

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 を使用しました。

4

1 に答える 1

3

これは、JSeparatorUIデリゲートがコンポーネントをペイントする方法を決定する方法に関係しています。テストに基づいて、セパレーターの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);
            }
        });
    }
}
于 2014-07-07T01:32:16.037 に答える