因此,我正在为更大的项目测试JSlider,但无法使其正常工作。该滑块应调整圆的大小,并且不起作用。我以为我可能在创建圆上遇到问题,并且我正在尝试使用setFrame,并且给出了一个错误消息,说它是“未定义”。谁能看到原因?因为它应该接受float或double作为参数。或者,如果您知道为什么它不调整形状的大小也会有所帮助的话,这就是我所拥有的:

public class DrawShape extends JPanel{
    private float width = 300;
    private Shape circle = new Ellipse2D.Float(100, 20, width, 300);

    public DrawShape() {

    }

    public DrawShape(float width) {
        this.width = width;
    }

    public void setWidth(int w) {
        this.width = w;
            circle.setFrame(100, 20, width, 300);//This is where the error is
    }

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

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


    }//end paintComponent

}//end class


主类:

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

}

最佳答案

Shape没有setFrame方法。 RectangularShape确实...

代替

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


您可以尝试使用...

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


代替...

您的public DrawShape(float width) {构造函数也是错误的,因为它实际上不执行任何操作。

您还应该考虑覆盖getPreferredSize方法,以便它可以将形状的宽度作为首选大小的一部分返回。

我不确定您是否真的需要维护width引用,因为您可以直接从circle确定...恕我直言

例如

我还没有测试过...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;


public class DrawShape extends JPanel {

    private final 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) {
        circle.setFrame(100, 20, w, 300);
        revalidate();
    }

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

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

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

    }//end paintComponent

}//end class

09-10 22:33