滑块的标签没有显示

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

这个问题让我抓狂!当我使用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());

    }
}
java jslider
1个回答
1
投票

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

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

在Finestra班。我用以下代替它们:

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

我最后补充说:

this.pack(); 
© www.soinside.com 2019 - 2024. All rights reserved.