JSlider没有以最小尺寸显示

时间:2014-07-12 08:42:55

标签: java swing jslider

我有一个非常简单的JFrame,其北面板中只有一个JSlider。我想通过以下方法以最小尺寸显示JSlider。

slider.putClientProperty("JComponent.sizeVariant", "mini");

但它仍然以正常大小显示。以下技巧也不起作用。

SwingUtilities.updateComponentTreeUI(this);

以下是完整的计划。

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

public class TestSliderSize extends JFrame {

    public TestSliderSize() {
        setLookNFeel();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screenSize.width / 2, screenSize.height / 2);
        JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 0, 50, 10);
        slider.putClientProperty("JComponent.sizeVariant", "mini");
        slider.setMajorTickSpacing(10);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);

            //SwingUtilities.updateComponentTreeUI(this);
        getContentPane().setLayout(new BorderLayout());
        JPanel centerPanel = new JPanel(new BorderLayout());

        JPanel northPanel = new JPanel(new FlowLayout());
        Dimension d1 = centerPanel.getSize();
        northPanel.setPreferredSize(new Dimension(screenSize.width / 2, 100));
        northPanel.add(slider);
        northPanel.setBackground(Color.BLACK);
        centerPanel.add(northPanel, BorderLayout.NORTH);

        JPanel innerPanel = new JPanel();
        innerPanel.setBackground(Color.ORANGE);
        centerPanel.add(innerPanel, BorderLayout.CENTER);

        SwingUtilities.updateComponentTreeUI(this);

        getContentPane().add(centerPanel, BorderLayout.CENTER);
        this.setVisible(true);

    }

    public static void main(String[] args) {

        new TestSliderSize();
    }

    private void setLookNFeel() {
        try {              //   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e1) {
        }
    }
}

现在我需要帮助以最小尺寸显示滑块。

1 个答案:

答案 0 :(得分:2)

此处的关键是pack()封闭的Window,以便组件采用其preferred sizesinnerPanel已被赋予任意大小以说明效果。另请参阅Initial Threads

image

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

public class TestSliderSize extends JFrame {

    public TestSliderSize() {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 0, 50, 10);
        slider.putClientProperty("JComponent.sizeVariant", "mini");
        slider.setMajorTickSpacing(10);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);

        JPanel centerPanel = new JPanel(new BorderLayout());
        JPanel northPanel = new JPanel();
        northPanel.add(slider);
        northPanel.setBackground(Color.BLACK);
        centerPanel.add(northPanel, BorderLayout.NORTH);
        JPanel innerPanel = new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 100); // arbitrary size
            }
        };
        innerPanel.setBackground(Color.ORANGE);
        centerPanel.add(innerPanel, BorderLayout.CENTER);
        this.add(centerPanel, BorderLayout.CENTER);

        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestSliderSize();
            }
        });
    }
}