我的课本中有这个练习:
在本练习中,您将探索一种可视化 Rectangle 对象的简单方法。JFrame 类的 setBounds 方法将框架窗口移动到给定的矩形。完成以下程序,直观地展示 Rectangle 类的 translate 方法:
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TranslateDemo
{
public static void main(String[] args)
{
// Construct a frame and show it
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Your work goes here: Construct a rectangle and set the frame bounds
JOptionPane.showMessageDialog(frame, "Click OK to continue");
// Your work goes here: Move the rectangle and set the frame bounds again
}
}****
我试过这个,但它不工作:
**import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TranslateDemo
{
public static void main(String[] args)
{
// Construct a frame and show it
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Your work goes here: Construct a rectangle and set the frame bounds
Rectangle box = new Rectangle(10, 20, 30, 40);
System.out.println("");
System.out.println(box);
System.out.println("");
JFrame f=new JFrame();
f.setBounds(50, 50, 200 ,200);
JOptionPane.showMessageDialog(frame, "Click OK to continue");
// Your work goes here: Move the rectangle and set the frame bounds again
box.translate(0,30);
System.out.println(box);
JOptionPane.showMessageDialog(frame, "Click OK to continue");
}
}**
你能帮我么 ?