Domanda

Sto provando un programma che utilizza un jslider per regolare la larghezza di un cerchio e il valore del cursore funziona, ma non è in realtà modifica della variabile 'larghezza'.Per favore aiuto!!!Ecco cosa ho finora:

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);  
    }

}
.

L'altra 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
.

È stato utile?

Soluzione

Due (principali) Popup Popup.

Prima di tutto, nel gestore di eventi, stai ridendo il telaio del cursore, che non ti aiuterà, quindi il tuo setWidth dovresti aggiungere una richiesta repaint, ad esempio.

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

In secondo luogo, tutti si chiamano super.paintComponents (nota il s alla fine) anziché super.paintComponent, ad esempio ...

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

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

}//end paintComponent
.

Ciò ti farà una quantità significativa di addolorazione con strani artefatti di vernice ...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top