我的JSlider和下面的文本之间的巨大空间

问题描述 投票:0回答:1

我目前正在使用Java的媒体播放器,利用VLCJ的力量,我正在实现均衡器调整窗口。在它们下面将有11个垂直滑块,带有JLabel,指示hZ频段和该频段的dB电平。但是,滑块与JLabel之间保持着巨大的差距。我试着仅将两个JLabel堆叠在一起,根本没有间隙。我的代码如下。 (返回均衡器的东西尚未实现。我只想在开始添加功能之前就可以使用基本的UI)

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import uk.co.caprica.vlcj.player.Equalizer;

public class VideoEQFrame {

    public VideoEQFrame() {
        //constructor
    }

    public Equalizer show() {
        JFrame frame = new JFrame("Effects");

        JPanel panel = new JPanel();

        JPanel sliders= new JPanel();

        JPanel gainObjects = new JPanel(new GridLayout(2, 0, 2, 0));
        JSlider gainS = new JSlider(JSlider.VERTICAL, -12, 12, 0);

        gainS.setMajorTickSpacing(2);
        gainS.setPaintTicks(true);
        gainS.setToolTipText("Adjust the gain");
        JLabel gainL = new JLabel("Text");
        gainObjects.add(gainS);
        gainObjects.add(gainL);
        sliders.add(gainObjects);

        panel.add(sliders);

        frame.add(panel);
        frame.setSize(new Dimension(600, 300));
        //frame.setResizable(false);
        frame.setVisible(true);

        Equalizer eq = new Equalizer(0);
        return eq;
    }

}

java swing jpanel jlabel jslider
1个回答
0
投票

您正在使用GridLayout放置滑块和文本标签。这意味着它们将占据相同的高度。因此,由于滑块的高度较大,因此标签的高度也将调整为此高度。尝试像LayoutManager这样使用另一个BorderLayout,如下所示:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

public class VideoEQFrame {

    public VideoEQFrame() {
        //constructor
    }

    public void show() {
        JFrame frame = new JFrame("Effects");

        JPanel panel = new JPanel();

        JPanel sliders= new JPanel();

        JPanel gainObjects = new JPanel(new BorderLayout());
        JSlider gainS = new JSlider(JSlider.VERTICAL, -12, 12, 0);

        gainS.setMajorTickSpacing(2);
        gainS.setPaintTicks(true);
        gainS.setToolTipText("Adjust the gain");
        JLabel gainL = new JLabel("Text");
        gainObjects.add(gainS, BorderLayout.CENTER);
        gainObjects.add(gainL, BorderLayout.PAGE_END);
        sliders.add(gainObjects);

        panel.add(sliders);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        //frame.setResizable(false);
        frame.setVisible(true);

//        Equalizer eq = new Equalizer(0);
//        return eq;
    }

    public static void main(final String[] args) {
        new VideoEQFrame().show();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.