Java Swing Resizing the JFrame and JPanel with all drawn Elements - java

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);
}
}

Related

Netbeans isn't show my gui project with java [duplicate]

I am trying to get a JInternalFrame to appear on my screen when a button is pressed, a pop up effect basically. However when the button is pressed the JInternalFrame does not appear on the screen. Also when I resize the screen all the elements expand with it, I am wondering if there is a way to get a pop up window to appear on the screen and keep the layout manager I have now still in place so that when the window is resized the elements are also resized with it
public class testing2 implements ActionListener {
JButton buttonAppear = new JButton();
JLayeredPane LayeredPane = new JLayeredPane();
public static void main(String[] args) {
new testing2();
}
public testing2() {
LayeredPane = new JLayeredPane();
BorderLayout borderlayoutpane = new BorderLayout();
LayeredPane.setLayout(borderlayoutpane);
JPanel mainPanel = new JPanel();
BorderLayout borderlayout = new BorderLayout();
mainPanel.setLayout(borderlayout);
JButton button = new JButton("Button");
mainPanel.add(button, "Center");
buttonAppear = new JButton("Panel Appear");
buttonAppear.addActionListener(this);
mainPanel.add(buttonAppear, "South");
LayeredPane.add(mainPanel, 2);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(LayeredPane);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == buttonAppear)
{
JInternalFrame inFrame = new JInternalFrame("Internal Frame", true, true, true, true);
inFrame.setBounds(10, 10, 200, 200);
inFrame.setVisible(true);
LayeredPane.add(inFrame, 1);
}
}
}
a pop up effect basically.
Then use a JDialog. A JInternalFrame was designed to work with a JDesktopPane.
mainPanel.add(button, "Center");
Don't use hardcode strings for the constraint. Use the field provided by the API:
mainPanel.add(button, BorderLayout.CENTER);
Also, follow Java naming conventions. Variable names should NOT start with an upper case character. Be consistent.
Don't know if it will make a difference but components with a higher layer number are painted on top of components with a lower index. So I would guess the panel (which is opaque) would just paint over top of the internal frame. Read the section from the Swing tutorial on How to Use Layered Panes. Also read the section on How to Use Root Panes to find the special variable for "popups" on a layered pane.

Can't add ImageIcon to JFrame

I've been trying to add an image to a JFrame but I can't seem to get it done.
I looked at online tutorials and other similar questions but nothing seems to work.
ImageIcon wiz = new ImageIcon("wizard.png");
ImageIcon assassin = new ImageIcon("assassin.png");
JFrame frame = new JFrame("Select");
frame.setBounds(50, 50,1000, 1000);
JButton w = new JButton("Wizard");
JButton a = new JButton("Assasin");
JFrame f = new JFrame("Image");
JLabel img1 = new JLabel(wiz);
frame.setLayout(null);
f.setLayout(null);
f.setIconImage(wiz.getImage());
w.setBounds(30,380,100,60);
frame.add(w);
a.setBounds(200, 380, 100, 60);
frame.add(a);
f.setVisible(true);
frame.setVisible(true);
I think the main problem in your program is that you try absolute positioning of components (e.g. JLabel) using setLayout(null) and setBounds() on components.
In Swing, the correct way to place components is by using layout managers. See this tutorial for details about how to use layout managers:
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
As a sample program, I've successfully set images (both as JFrame's icon and inside JFrame's contents area) in below program. Try it and see.
This is a screenshot of my sample JFrame.
import javax.swing.*;
public class FrameWithIcon
{
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Since I'm not setting a layout manager to contentPane, it's default (BorderLayout) is used
//This sets the image in JFrame's content area
f.getContentPane().add(new JLabel(new ImageIcon("star.png")));
//This sets JFrame's icon (shown in top left corner of JFrame)
f.setIconImage(new ImageIcon("star.png").getImage());
f.setBounds(300, 200, 400, 300);
f.setVisible(true);
}
}

Centering a JFrame?

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);
}
}

How to set a size for a JPanel inside a JFrame?

I am trying to put a JPanel in the center of a fullscreen JFrame with a defined size, but it's always stretching for the whole screen...
I tried setBounds, setPreferredSize, but they don't work.
This is the code:
public static void showScene(){
//Create the frame to main application.
JFrame frame = new JFrame("Application"); // set the title of the frame
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // set the size, maximum size.
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create the panel to store the main menu.
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(500,200));
panel.setBackground(new java.awt.Color(0,0,255));
frame.add(panel,BorderLayout.CENTER);
}
public static void main (String[] args){
showScene();
}
BorderLayout stretches the contents to fill the parent container.
If you want the child to have smaller size than the parent, use some other LayoutManager (try FlowLayout).
You can change layout using following code.
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 0,90)));
frame.add(panel);
frame.pack();
For further reference, follow visual guide to layout managers.
Good luck.

JButton added to JPanel doesn't show

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).

Categories

Resources