JSlider没有出现?

时间:2018-05-02 03:44:00

标签: java swing

我有一个JSlider但是当我运行代码时它没有出现。我还创建了一个按钮,但是那个按钮显示了。另外,你知道如何将两者的位置改为左下角吗?谢谢!

这是我的代码

public class Board extends JPanel implements ActionListener, 
                                             KeyListener 
{
private CurlingStone[] stones;
private int stonenumber;

private Timer timer;
private int delay = 15;

private Button button;

private JSlider slider;

public Board()
{
    stones = new CurlingStone[8];
    for(int i = 0; i < 8; i++)
    {
        if(i%2 == 0)
            stones[i] = new CurlingStone(Color.yellow);
        else
            stones[i] =  new CurlingStone(Color.red);
    }
    stonenumber = 0;
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);

此按钮出现

    button = new Button("Launch");
    add(button);
    button.addActionListener(this);

但滑块没有,所以在这里我不确定是什么问题

    slider = new JSlider(1, 10, 1);
    slider.setMinorTickSpacing(1);
    slider.setPaintLabels(true);
    slider.setPaintTicks(true);
    add(slider);
    timer = new Timer(delay, this);
}

public void paint(Graphics g)
{   
    g.setColor(Color.white);
    g.fillRect(0, 0, 400, 725);

    g.setColor(Color.gray);
    g.fillRect(0, 0, 400, 50);

    g.setColor(Color.blue);
    g.fillOval(95, 95, 210, 210);

    g.setColor(Color.white);
    g.fillOval(125, 125, 150, 150);

    g.setColor(Color.red);
    g.fillOval(155, 155, 90, 90);

    g.setColor(Color.white);
    g.fillOval(185, 185, 30, 30);

    for(int i = 0; i <= stonenumber; i++)
    {
        stones[i].draw((Graphics2D) g);
    }

    if(stones[stonenumber].stoneX == 185 && 
       stones[stonenumber].stoneY == 600)
    {
        g.setColor(Color.green);
        ((Graphics2D)g).setStroke(new BasicStroke(4));
        g.drawLine(200, 600, 200 + stones[stonenumber].dirX*10, 530);
    }

    this.requestFocus();
    g.dispose();
}

0 个答案:

没有答案