JSlider没有调整形状大小

时间:2013-11-18 04:27:26

标签: java swing jframe jslider

我正在测试一个程序,该程序使用JSlider来调整圆的宽度,并且滑块的值正在工作,但它实际上并没有改变'width'变量。请帮忙!!!以下是我到目前为止的情况:

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

}

另一堂课:

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

1 个答案:

答案 0 :(得分:2)

弹出两个(主要)问题。

首先,在您的事件处理程序中,您正在重新绘制滑块框架,这对您没有帮助,因此您的setWidth应该添加repaint请求,例如。

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

其次,你们都呼叫super.paintComponents(注意最后的s而不是super.paintComponent,例如......

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

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

}//end paintComponent

这会给你带来大量悲伤的奇怪的油漆文物......