所以我试图创建一个使用两个JSliders来调整矩形大小的Applet。我希望从底部到顶部的高度与Vertical JSlider的外观相匹配。
我遇到的唯一问题是用Java绘制图纸来自x& y,它们都位于0,向左移动x(向西),y向南移动。调整宽度的大小很简单,因为我只使用带有单个参数的方法来增加或减少宽度。随着高度,向上移动是很好的(我已经设置为340,因为这是我想留下的基础)。当我向下移动JSlider时,它向下移动但不会达到它曾经的高度。我真的很想看看矩形的移动方式与JSlider的移动方式相同。
主Applet页面(JSliders所在的位置):
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的第三个月,我非常感谢任何反馈。提前谢谢。
答案 0 :(得分: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;