How to set size of a JSlider?

自作多情 提交于 2019-12-10 18:52:56

问题


I have searched the web for a solution to this problem and didn't find anything that worked.

I have a vertical JSlider inside a JPanel that uses GridBagLayout and a GridBagConstraints for positioning the objects on the panel.

Currently I have the following code:

    gbc.gridy = 1;
    add(button1,gbc);

    gbc.gridy = 2;
    add(button2,gbc);

    gbc.gridy = 3;
    add(slider,gbc);

The objects are positioned vertically along the panel.

The slider always appears in the same size (length). Tried to use setPreferredSize - didn't work. Tried to use gridheight in order to have to slider cross two rows - didn't work either. Tried to change the actual min and max values of the slider, didn't help.

Shouldn't GridBagLayout respect preferred size?

EDIT: Also tried creating anoter JPanel inside the main JPanel, set it's layout to FlowLayout, BoxLayout or GridLayout, and add the slider to it. Didn't change a thing. Very weird.

Any ideas?


回答1:


Use a different layout manager (or put the slider in a nested JPanel with a different layout manager), that will stretch the JSlider. In the example I use BorderLayout

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;

public class DifferentSizeSlider extends JPanel{
    public DifferentSizeSlider() {
        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(new JSlider());

        JPanel bottomPanel = new JPanel(new GridLayout(1, 2));
        bottomPanel.add(new JSlider());
        bottomPanel.add(new JPanel());

        setLayout(new GridLayout(2, 1));
        add(topPanel);
        add(bottomPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JOptionPane.showMessageDialog(null, new DifferentSizeSlider());
            }
        });
    }
}

EDIT with your code example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;

public class DifferentSizeSlider extends JPanel{
    public DifferentSizeSlider() {
        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.setPreferredSize(new Dimension(400, 50));
        topPanel.add(new JSlider());

        JButton jbtOne = new JButton("Button");
        JButton jbtTwo = new JButton("Button");

        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(new GridBagLayout());

        gbc.gridy = 0;
        add(jbtOne,gbc);  

        gbc.gridy = 1;
        add(jbtTwo,gbc);

        gbc.gridy = 2;
        add(topPanel, gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JOptionPane.showMessageDialog(null, new DifferentSizeSlider());
            }
        });
    }
}



回答2:


To change the size of a swing component you use the setPrefferedSize() method

setPreferredSize(new Dimension(300, 80));

You may have to use a separate panel with a flowLayout for the slider then add that panel to the . Some layout managers may not take the preferred size.



来源:https://stackoverflow.com/questions/21835430/how-to-set-size-of-a-jslider

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!