Hello,
I am doing exercises in my java book and right now I am stuck on an example. This example is supposed to show me how to use the setBounds method of the JFrame class to move a frame window to a given rectangle. I tried playing around with the code and can't seem to figure out what I am doing wrong. I have looked at the API as well which made me try to set parameters in setBounds, but I am still not seeing anything appear on the frame when I run.
Here are the two different codes I have tried. Can anyone direct me what to look at or give me a hint without giving me the answer. I would like to figure this out on my own, but need direction. Thank you.
import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JOptionPane; public class TranslateDemo { public static void main(String[] args) { // Constructs a frame and show it JFrame frame = new JFrame(); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); Rectangle box = new Rectangle (5, 5, 5, 5); box.setBounds(box); box.getBounds(); JOptionPane.showMessageDialog(frame, "Click OK to continue"); box.translate(10, 10); box.setBounds(box); box.getBounds(); } }
import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JOptionPane; public class TranslateDemo { public static void main(String[] args) { // Constructs a frame and show it JFrame frame = new JFrame(); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); Rectangle box = new Rectangle (5, 5, 5, 5); box.setBounds(10, 10, 10, 10); box.getBounds(); JOptionPane.showMessageDialog(frame, "Click OK to continue"); box.translate(10, 10); box.setBounds(20, 20, 20, 20); box.getBounds(); } }