setFrame,JSlider的错误

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

所以我正在为一个更大的项目测试一个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);  
    }

}
java swing paintcomponent shapes jslider
1个回答
1
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.