如何设置JSlider的大小? [英] How to set size of a JSlider?
问题描述
我在网上搜索了此问题的解决方案,但没有找到任何有效的方法.
I have searched the web for a solution to this problem and didn't find anything that worked.
我在 JPanel 中有一个垂直 JSlider,它使用 GridBagLayout 和 GridBagConstraints 来定位面板上的对象.
I have a vertical JSlider inside a JPanel that uses GridBagLayout and a GridBagConstraints for positioning the objects on the panel.
目前我有以下代码:
gbc.gridy = 1;
add(button1,gbc);
gbc.gridy = 2;
add(button2,gbc);
gbc.gridy = 3;
add(slider,gbc);
对象沿面板垂直放置.
滑块始终以相同的大小(长度)显示.尝试使用 setPreferredSize
- 没有用.试图使用 gridheight
以便必须跨两行滑块 - 也不起作用.尝试更改滑块的实际最小值和最大值,但没有帮助.
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.
GridBagLayout 不应该尊重首选大小吗?
还尝试在主 JPanel 内创建另一个 JPanel,将其布局设置为 FlowLayout、BoxLayout 或 GridLayout,并将滑块添加到其中.没有改变任何事情.很奇怪.
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.
有什么想法吗?
推荐答案
使用不同的布局管理器(或将滑块放在具有不同布局管理器的嵌套 JPanel
中),这将 拉伸 JSlider
.在示例中,我使用 BorderLayout
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());
}
});
}
}
<小时>
编辑与您的代码示例
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());
}
});
}
}
这篇关于如何设置JSlider的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!