滑块的标签未显示

时间:2018-09-03 07:03:38

标签: java jslider

这个问题使我发疯! 当我使用setLabelTable和setPaintLabels显示JSlider的标签时,不再显示Jslider并炸毁布局。你能帮我吗?

这是我的课程“ sliderPlayer”的代码:

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

public class sliderPlayer extends JPanel
{
static final int MIN = 2;
static final int MAX = 300;
static final int INIT = 15;   

// constructor
public sliderPlayer() 
    {
    super(new GridLayout(1,1));
    JSlider slid = new JSlider(JSlider.HORIZONTAL,MIN,MAX,INIT);
    slid.setMajorTickSpacing(MAX/5);
    slid.setMinorTickSpacing(MAX/10);
    slid.setPaintTicks(true);
    slid.setLabelTable(slid.createStandardLabels(10));
    slid.setPaintLabels(true);
    add(slid);
    }       
}

如果我删除以下行:

slid.setLabelTable(slid.createStandardLabels(10));
slid.setPaintLabels(true);

它运作良好,但当然不会画标签!

这是使用滑块的代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.*;

public class Finestra extends JFrame
{
public static  int LARGHEZZA;
public static  int ALTEZZA ;
public GridLayout mainLayout;
public JPanel tavolo;
public JPanel panelSlidePlayer;
public Finestra()
    {
    super("il D I L E M M A del PRIGIONIERO");
    this.setVisible(true);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int W=(int)screenSize.getWidth();
    int H=(int)screenSize.getHeight();
    LARGHEZZA=W-W/5;
    ALTEZZA=H-H/5;
    Rectangle rect=new Rectangle(W/10, H/10,LARGHEZZA, ALTEZZA);
    this.setBounds(rect);
    mainLayout= new GridLayout(1,1); // Layout principale
    this.setLayout(mainLayout);
    tavolo= new JPanel(new GridLayout(2,2));

    getContentPane().add(tavolo);
    getContentPane().add(new sliderPlayer());

    }
}

1 个答案:

答案 0 :(得分:1)

经过测试,尝试,研究,我解决了问题!  我已删除说明:

Rectangle rect=new Rectangle(W/10, H/10,LARGHEZZA, ALTEZZA); 
this.setBounds(rect); 

在Finestra班上。我将它们替换为:

this.setPreferredSize(new Dimension(LARGHEZZA,ALTEZZA)); 

最后我加了:

this.pack();