[英]using setbounds un java
我的教科書中有這個練習:
在本練習中,您將探索一種可視化Rectangle對象的簡單方法。 JFrame類的setBounds方法將框架窗口移動到給定的矩形。 完成以下程序,以直觀地顯示Rectangle類的translation方法:
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");
}
}**
你能幫我么 ?
讓我們從創建兩個JFrame
實例開始。
// First instance
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
// Second instance
JFrame f=new JFrame();
f.setBounds(50, 50, 200 ,200);
第一個是屏幕上可見的內容,第二個則不是。
也許像...
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);
f.setBounds(box);
會更好地工作。
您對box
所做的任何更改都不會更改框架,因為JFrame
使用Rectangle
的屬性設置其屬性並且不維護對原始Rectangle
的引用,相反,您將需要再次調用setBounds
來更新框架
但是作為一般規則,您不應該設置框架本身的大小,而是依靠pack
,但是由於這是一種練習,因此我可以忽略它;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.