I'm using one of the methods to try to center my frame called
frame.setLocationRelativeTo(null);
but it's centering it by putting the upper-right corner of the frame in the center of the screen, and that's not what I wanted. I wanted to make it so that it centers the frame itself in the middle, or in other words, I want the center of the frame to the in the center of the screen. Is there any other methods to do this? Thanks for the help.
More codes below:
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
} //I'm using this method to override the Jframe and return the panel size
and I'm using the:
frame.pack();
method to make the Jframe fit whatever size the jpanel size is, which is why a size isn't created for the jframe. But even if I do create the size for the jframe, and use the setLocationRelativeTo(null) method, nothing changes.
Normally if you're using Java 1.4 or newer the method setLocationRelativeTo(null) should help. It's weird that it reacts like that, could you maybe post your java file so we can see what's wrong?
Also, you have to setSize() your frame BEFORE using the setLocationRelativeTo(null) method.
Kindly,
Solid
You can get the size of your screen with Toolkit.getDefaultToolkit().getScreenSize(). Assuming you're not using multiple displays, it's simple from there to set the location of your JFrame such that it is centered in the screen.
public static void main(String[] args){
JFrame f = new JFrame();
f.getContentPane().add(new JLabel("Label"), BorderLayout.CENTER);
f.getContentPane().add(new JButton("Button"), BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((screen.width - f.getWidth())/2, (screen.height - f.getHeight())/2);
f.setVisible(true);
}
It is important that the pack() call happens before setLocation, or it might not be centered again.
You could even create a subclass of JFrame that re-centers itself whenever it packs, as such:
class CenteredFrame extends JFrame{
#Override
public void pack(){
super.pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - getWidth())/2, (screen.height - getHeight())/2);
}
}
Related
I'm developing a game in Java Swing. I use absolut coords and a fixed window size in order to set every element in its place. But if I resize the JFrame, the drawn elements dont scale. How can I achieve that?
In my example I would set the default window size to 1280 x 720 and develop the game according to this size. Then it'd be perfect to just let java automatically resize the elements.
If you want a fixed size window, you can use JFrame.setResizable(boolean) API to make your window non-resizable. Example:
public class FixedSizeJFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("My Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 1280, 720);
frame.getContentPane().add(new JLabel("My Label"), BorderLayout.NORTH);
frame.getContentPane().add(new JTextArea("Sample text"), BorderLayout.CENTER);
frame.setResizable(false);
frame.setVisible(true);
}
}
I am making a small game for my enjoyment and when I add my JPanel to the JFrame nothing is showing up that i have drawn in my other class.
Here is my main class
package Game;
import java.awt.*;
import javax.swing.*;
import Game.Player;
public class Frame {
static JFrame f = new JFrame("xx");
public static JLabel points = new JLabel();
public static void main(String[] args) {
Player pl = new Player();
JPanel p = new JPanel();
points.setFont(new Font("Verdana",1,20));
p.add(pl);
p.add(points);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setSize(800, 800);
f.setTitle("Box Game");
f.add(p);
f.setVisible(true);
}
}
I am having a hard time figuring it out so can someone please help me... THANKS!
nothing is showing up that i have drawn in my other class.
You are add components to a panel. A JPanel uses a FlowLayout by default. The FlowLayout respects the preferred size of component you add to the panel. If the components don't paint then the preferred size of the component is probably (0, 0).
nothing is showing up that i have drawn in my other class
If you are doing custom painting then you need to make sure you override the getPreferredSize() method of the class to return the size so the layout manager can do its job.
Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started.
I am writing a Game in Java, and I don't want to use a layout manager for my JFrame. My class extends JFrame, and looks something like this:
//class field
static JPanel contentPane;
//in the class constructor
this.contentPane = new JPanel();
this.contentPane.setLayout(null);
this.setContentPane(contentPane);
I want my JPanel be exactly 600x600 px, but when I set the size of my JFrame by calling the this.setSize(600,600) method, the JPanel size is less than the 600x600 px because the border of the JFrame window is included too.
How can I set the size of the JPanel to be exactly 600x600 px?
P.S. I have seen all of the previous post and none of them work for me.
For example:
Get the real size of a JFrame content does not work for me.
What else can I do?
How can I set the size of the JPanel to be exactly 600x600 px?
Override the getPreferredSize() method of your custom game panel to return the size you want the panel to be.
#Override Dimension getPreferredSize()
{
return new Dimension(600, 600);
}
Then the basic code to create your frame will be:
GamePanel panel = new GamePanel();
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
Now all your game logic will be contained in the GamePanel class.
If you don't need the Frame-Decorations (icon, title, min/max/close-buttons and the border), you can make a Frame undecorated, then it has exactly the size you gave it
java.awt.Frame.setUndecorated(boolean)
I can't seem to add a JButton to a JPanel.
I have a PropWindow (JFrame) that has a PropView (JPanel) in it. the PropView-JPanel seems to be added correctly because I can draw shapes on it with paint().
But when I use this to try adding a button it just won't show up att all :/
JButton testButton;
public PropView(int width, int height) {
super(true);
setLayout(null);
setSize(width, height);
//TestButton
testButton = new JButton("Test");
testButton.setLocation(10,10);
testButton.setSize(100, 50);
testButton.setVisible(true);
add(testButton);
setFocusable(true);
setVisible(true);
}
The JFrame and the JPanel are both 250x600 px.
I can't tell from the code snippet you posted but just in case: make sure you call pack () on the frame after you have added the panel or any other components.
Also, it's usually discouraged to extend a JPanel or JFrame, unless you have a good reason to do it, just a heads up.
Here you have a short tutorial about displaying frames:
And some sample code in it that might help:
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
Make sure you added PropPanel to PropWindow using myPropWindow.getContentPane().add(myPropPanel), not just myPropWindow.add(myPropPanel).
I have a single JLabel in my JFrame, initially displayed using pack().
My problem is that whenever I try to resize the window, the JLabel also moves (depending on which side of the window is resized), largely due to the layout of the JFrame.
To be more specific, whenever I resize the window up/down, the JLabel stays centered. I would like that not be the case.
I'm sorry if my question is confusing. I'll gladly provide diagrams if requested.
public class JavaApplication8 {
public static void main(String[] args) throws MalformedURLException {
// TODO code application logic here
JFrame window = new JFrame("test");
URL bgURL = new URL("https://si0.twimg.com/profile_images/3585146044/3b695fa73490227f792fa4e46d4a7a57.jpeg");
ImageIcon bg = new ImageIcon(bgURL);
JLabel label = new JLabel(bg);
window.add(label, BorderLayout.CENTER);
window.pack();
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
Use a different layout manager:
window.setLayout(new FlowLayout());
would be sufficient for the example.
Another possibility, if you want to keep using BorderLayout, is placing the label to a different position:
window.add(label, BorderLayout.NORTH);
Also note that you should access swing components only from the event dispatch thread.