使用JScrollBar调整形状尺寸

时间:2013-11-17 23:23:47

标签: java swing jscrollbar

我希望能够使用JScrollBar更改形状的位置和/或大小。我仍然相当新兴摆动组件,但已经想出如何更改字符串的位置,但我不知道如何使用从paintComponent绘制的形状来解决它。我有这个作为我的调整听众:

scrollBar.addAdjustmentListener(new AdjustmentListener() {

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            double value = scrollBar.getValue(); //returns current value of scrollbar 0-100
            double maximumValue = scrollBar.getMaximum();
            double newX = (value * messagePanel.getWidth() / maximumValue);
            messagePanel.setXCoordinate((int)newX);



        }//end adjustment Value Changed

    });//end adjustment listener 

消息的x和y坐标默认为20,但我不知道如何为椭圆或矩形(均为2D)调整全部。我不知道是否有其他算法或如何去做。如果需要,我可以提供更多代码。请帮忙!

JSlider测试代码:

public class event implements ChangeListener{

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

    }//end stateChanged
}//end class event 

我的形状如下:

    private Shape circle; 
int width = 300;

public DrawShape() {
    this.circle = new Ellipse2D.Float(100, 20, width, 300); 
}

public void setWidth(int width) {
    this.width = width;
}

值会改变,但它不会改变'width'变量,我哪里错了?

1 个答案:

答案 0 :(得分:1)

  

所以我正在测试一个JSlider但是无法让它真正改变它   宽度由于某种原因。

在更改圆的宽度后,我没有看到您在任何地方调用repaint()方法。试试这个工作示例作为起点,仔细阅读Performing Custom Painting教程。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Demo {

    private Shape circle;
    private int width = 100;
    private int height = 100;

    private void initGUI(){

        final JPanel drawPane = new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                circle = new Ellipse2D.Float(100, 20, width, height);
                Graphics2D graphics = (Graphics2D)g.create();
                graphics.draw(circle);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400,300);
            }
        };

        JSlider widthSlider = new JSlider(width, width * 2, width);
        widthSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider)e.getSource();
                width = slider.getValue();
                drawPane.repaint();
            }
        });

        JSlider heightSlider = new JSlider(height, height * 2, height);
        heightSlider.setOrientation(JSlider.VERTICAL);
        heightSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider)e.getSource();
                height = slider.getValue();
                drawPane.repaint();
            }
        });

        JPanel content = new JPanel(new BorderLayout());
        content.add(widthSlider, BorderLayout.NORTH);
        content.add(drawPane, BorderLayout.CENTER);
        content.add(heightSlider, BorderLayout.WEST);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setContentPane(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }

}

截图

enter image description here