Pergunta

Estou testando um programa que usa um JSlider para ajustar a largura de um círculo, e o valor do controle deslizante está funcionando, mas na verdade não está alterando a variável 'largura'.Por favor ajude!!!Aqui está o que tenho até agora:

public class SliderTest extends JFrame{

    private static DrawShape circle = new DrawShape();
    JSlider slider;
    JLabel label;


    public SliderTest()  {

        setLayout(new FlowLayout());
        slider = new JSlider(JSlider.HORIZONTAL, 150, 450, 300);//orientation, min val, max value, starting val
        slider.setMajorTickSpacing(50);//every 5 integers will be a new tick position 
        slider.setPaintTicks(true);
        add(slider);

        label = new JLabel("Current value 300");
        add(label);

        event e = new event();
        slider.addChangeListener(e);;


    }//end cons

    public class event implements ChangeListener{

        public void stateChanged(ChangeEvent e) {
             JSlider slider = (JSlider)e.getSource();
             int value = slider.getValue();
            label.setText("Current Value " + value);

            circle.setWidth(value);
            repaint();

        }//end stateChanged
    }//end class event 




    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setTitle("Circle");
        frame.add(circle);
        frame.setSize(500,400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true); 

        JFrame frame1 = new SliderTest ();
        frame1.setTitle("Toolbar");
        frame1.setSize(300,200);
        frame1.setLocation(200,100);
        frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame1.setVisible(true);  
    }

}

A outra classe:

public class DrawShape extends JPanel{

    private float width = 300;
    private Ellipse2D circle = new Ellipse2D.Float(100, 20, 300, 300); 

    public DrawShape() {

    }

    public DrawShape(float width) {
        circle.setFrame(100, 20, width, 300);
    }

    public void setWidth(int w) {
        this.width = w;
        circle.setFrame(100, 20, w, 300);
        revalidate();
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        size.width = circle.getBounds().width;
        return size;
    }

    public void paintComponent (Graphics g) {
         super.paintComponents(g);
         Graphics2D graphics = (Graphics2D)g;

         graphics.setColor(Color.black);
         graphics.fill(circle);


    }//end paintComponent

}//end class
Foi útil?

Solução

Dois (principais) problemas aparecem.

Primeiramente, no seu manipulador de eventos, você está repintando o quadro do controle deslizante, o que não vai ajudar, então é seu setWidth você deve adicionar um repaint solicitar, por exemplo.

public void setWidth(int w) {
    System.out.println("setWidth " + w);
    this.width = w;
    circle.setFrame(100, 20, w, 300);
    revalidate();
    repaint();
}

Em segundo lugar, todos vocês estão ligando super.paintComponents (Note o s no final) em vez de super.paintComponent, por exemplo...

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(Color.black);
    g2d.fill(circle);

}//end paintComponent

Isso causará uma quantidade significativa de sofrimento com estranhos artefatos de pintura...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top