1

所以我正在尝试制作一个使用两个 JSlider 来调整矩形大小的 Applet。我希望从下到上的高度与垂直 JSlider 的外观相匹配。

我遇到的唯一问题是,要在 Java 中绘制,绘图来自 x 和 y,它们都位于 0,向左(西)移动 x,并且 y 向南移动。调整宽度很简单,因为我只是使用带有单个参数的方法来增加或减少宽度。有了高度,向上移动就可以了(我将 y 设置为 340,因为这是我想留在的基础)。当我向下移动 JSlider 时,它会向下移动,但不会达到以前的高度。我真的很想让矩形看起来像 JSlider 的移动方式一样移动。

主 Applet 页面(JSlider 所在的位置):

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class ResizeRectangle extends JApplet {
    private JSlider sliderHeight;
    private JSlider sliderWidth;
    private JLabel title;
    private Contents content;
    private int oldValue = 0;
    public void init(){

        setLayout(new BorderLayout());

        title = new JLabel("Rectangle Resizer");
        content = new Contents();

        content.setBackground(Color.WHITE);

        //Slider for increasing the height
        sliderHeight = new JSlider(SwingConstants.VERTICAL, 10, 100, 10);
        sliderHeight.setMajorTickSpacing(10);
        sliderHeight.setPaintTicks(true);

        sliderHeight.addChangeListener(
                new ChangeListener(){
                    public void stateChanged(ChangeEvent e){
                        //If sliderHeight is greater than oldValue pass true for increase
                        if(sliderHeight.getValue() > oldValue){
                            content.setHeight(sliderHeight.getValue(), true);
                        }
                        //Else if sliderHeight is less than oldValue pass false for increase
                        else if(sliderHeight.getValue() < oldValue){
                            content.setHeight(sliderHeight.getValue(), false);
                        }
                        //Sets the value of sliderHeight to the value of oldValue to compare later
                        oldValue = sliderHeight.getValue();
                    }
                }
        );
        //Slider for increasing the widht
        sliderWidth = new JSlider(SwingConstants.HORIZONTAL, 10, 100, 10);
        sliderWidth.setMajorTickSpacing(10);
        sliderWidth.setPaintTicks(true);

        sliderWidth.addChangeListener(
                new ChangeListener(){
                    public void stateChanged(ChangeEvent e){
                        content.setWidth(sliderWidth.getValue());
                    }
                }
        );

        content.setBackground(Color.WHITE);
        add(title, BorderLayout.NORTH);
        add(content, BorderLayout.CENTER);
        add(sliderWidth, BorderLayout.SOUTH);
        add(sliderHeight, BorderLayout.EAST);

    }
}

这是设置高度(以及宽度)的代码:

import java.awt.Component;
import java.awt.Graphics;



public class Contents extends Component {

    private int height = 10;
    private int width = 10;
    //Initialize y to 340.
    private int y = 340;

    //Draw the rectangle
    public void paint(Graphics g){
        g.fillRect(5, y, width, height);
    }

    public void setHeight(int h, boolean increase){
        //If the slider is increasing, decrease y and increase height to give it the
        // look of rising 
        if(increase){
            y = y - h;
            height = height + h;
        }
        //else do the opposite
        else{
            y = y + h;
            height = height - h;
        }
        //For debugging purposes
        System.out.println("h = "+h);
        System.out.println("y = "+y);
        System.out.println("height = "+height+"\n\n");
        //Repaint the rectangle
        repaint();

    }
    //Set the width and repaint
    public void setWidth(int w){
        width = w;
        repaint();
    }
}

这是我学习 Java 的第三个月,如果有任何反馈,我将不胜感激。先感谢您。

4

1 回答 1

1

您的代码存在逻辑问题。用于更改更改侦听器内部高度的调用是

content.setHeight(sliderHeight.getValue(), ...);

乍一看,这看起来很合理。然而,该方法setHeight并没有按照名称所暗示的那样做(即设置高度),而是通过作为第一个参数给出的量来增加或减少高度:

if(increase){
    y = y - h;
    height = height + h;
} else {
    y = y + h;
    height = height - h;
}

因此,如果滑块从 10 变为 20 并返回到 10,则值会发生以下变化:

// initial values
y = 340
height = 10     

// change from 10 to 20, thus h=20 and increase as arguments for the method
y -> 340 - 20 = 320
height -> 10 + 20 = 30


// change from 20 back to 10, i.e. h=10 and decrease
y -> 320 + 10 = 330
height -> 30 - 10 = 20

该方法应该做的确实是设置高度,即

y = 340 - h;
height = h;
于 2012-03-30T17:13:21.523 回答