Window size is wrong or not setting in Swing - java

This is my first swing application. I'm trying to create an window and add a button. On clicking the button, it should display some value on the console. Everythign works fine, but the window is very small. I've specified 800*600, but then also the window size is small, that is its wrapping the button size only.
Here is my code snippet:
JFrame frame = new JFrame("My Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new MyClass(), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.pack();
frame.setVisible(true);
Snippet from MyClass.java:
setLayout(new GridBagLayout());
JButton button = new JButton("Button");
button.addActionListener(this);
add(button);
How to make the window size as 800*600?

The pack() method should always be called on a GUI based on a JFrame, so leave that in. It reduces the GUI to the smallest size needed to display the components in it. But don't go calling setSize(Dimension) after that, before checking it is larger than the minimum size.

Remove frame.pack(); from your code.
More information:
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#pack%28%29

You are calling pack method. The api said: "It causes this Window to be sized to fit the preferred size and layouts of its subcomponents"
You haven't specified the preferred size. Try it doing this:
frame.setPreferredSize(new Dimension(800,600));

Related

How to add a JPanel to a JFrame with Runnable method

When i create a JPanel and add it to my JFrame and add my canvas to it (space) it will show just the JPanel and not the any of the graphics. Why is it only showing the JPanel when i want it to show both of them at the same time?
Source Code:http://pastebin.com/Cw9E0a8j
If youre intent was to add the your canvas inside the jPanel you try change the following lines in your source
Original Code :
frame.add(p);
frame.add(space, BorderLayout.CENTER);
Suggested Code :
p.add(space);
frame.add(p, BorderLayout.CENTER);
If You are willing to view both JPanel and the canvas in the JFrame try giving positioning to the canvas also.But a different layout will do better. Try out the given example.
Original Code :
frame.setLayout(new BorderLayout());
frame.add(p);
frame.add(space, BorderLayout.CENTER);
Suggested Code 1 :
By providing the position to the JPanle in the Border Layout.
frame.setLayout(new BorderLayout());
frame.add(p, BorderLayout.NORTH);
frame.add(space, BorderLayout.CENTER);
Suggested Code 2 :
By Changing the Layout of the JFrame.
frame.setLayout(new GridLayout(0,2));
frame.add(p);
frame.add(space);
For more information please refer official documentation[1].
[1]. https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
The solution is very simple. In your code you are placing the 'space' in the center of the window, therefore you are telling it to take up the whole screen. What you should do is this.
frame.add(p, BorderLayout.EAST);//EAST for the rigth
frame.add(space, BorderLayout.WEST);//WEST for the left

Java - placing items in JPanel like in JFrame

I try to do simple game, made of few 'screens' (menu, options, etc), which could work by displaying and hiding several JPanels. When I add some stuff on each panel and run the program, the only thing that appears is empty JFrame. I've made id looking like this:
public class Frame extends JFrame{
JPanel panel1 = new JPanel();
JFrame frame = new JFrame("halo");
JButton button = new Button();
int WIDTH=600,HEIGHT=600;
public Frame(){
frame.add(game);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel1);
panel1.setBounds(0,0,WIDTH,HEIGHT)
panel1.add(button);
button.setBounds(10,10,30,30);
}}
Everything essential is imported and declared, it's just 'simpled' version. What's wrong with it?
A few things:
Using the classname Frame may conflict with java.awt.Frame.
You have to set the frame visible by calling frame.setVisible(true); Before that, call frame.pack();
You are subclassing JFrame, but also declaring and preparing a different JFrame. Eliminate one of them.
Add the components to the content pane of the JFrame: frame.getContentPane().add(subcomponent);
Setting a layout manager and packing after adding elements should fix the issue. Also it's unclear why you're referring to frame when you're constructing this. Try this version:
public class Frame extends JFrame{
JPanel panel1 = new JPanel();
JButton button = new Button();
int WIDTH=600,HEIGHT=600;
public Frame(){
setTitle("Halo");
setLayout(new BorderLayout());
add(panel1, BorderLayout.CENTER);
panel1.setBounds(0,0,WIDTH,HEIGHT);
panel1.setLayout(new BorderLayout());
panel1.add(button, BorderLayout.SOUTH);
button.setBounds(10,10,30,30);
pack();
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JFrame uses a BorderLayout by default, this means that only one component can occupy each of the five available positions that BorderLayout managers.
By default, if you don't specify where you want the component to be, it will be added to the CENTRE position
You should break your UI down into logical units and use JPanels to manage them
You can use a combination of panels and layouts to generate complex UIs
You should also consider using a CardLayout to allow you to switch between base views
You should also call setVisible on the JFrame last, after you've established the basic UI, otherwise you'll need to call revalidate and and repaint to update the UI
You should avoid using setBounds/setSize/setLocation on components and rely on the use of layout managers, as they are designed to manage the differences in rendering pipelines that occurs across multiple different operating systems and hardware platforms

AbsoluteLayoutDemo.java in Oracle Java Tutorials

Question 1:
I am writing a program to use an absolute layout because I need to reposition a JLabel at runtime. I have tried to understand the following java demo program (from Oracle Java Tutorials):
http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/AbsoluteLayoutDemoProject/src/layout/AbsoluteLayoutDemo.java
It suggests the following:
//Size and display the window.
Insets insets = frame.getInsets();
frame.setSize(300 + insets.left + insets.right, 125 + insets.top + insets.bottom);
frame.setVisible(true);
The frame is not set to be visible before the insets values are calculated, so they are all 0 (zero), am I getting it right?
I have tried to use frame.setSize(300, 125); to replace the code and Eclipse gives me the same result (the same window size as I have observed). Is it wrong to have setSize before setVisible?
Question 2:
In my own program, I have the following code segment:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel moving_label = new JLabel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
panel.setLayout(null);
frame.add(panel);
panel.add(moving_label);
moving_label.setBounds(150, 150, 50, 50);
moving_label.setIcon(img);
frame.setVisible(true);
frame.setBounds(100, 100, 300+frame.getInsets().left+frame.getInsets().right,
300+frame.getInsets().top+frame.getInsets().bottom);
//I don't need to have panel.validate() or panel.repaint(), right?
a) Is the above code segment in a correct order (I am a newbie of java's Swing GUI)?
b) I am using Eclipse on my MacBook Pro to run Java programs. I have 40 JLabels (each with an ImageIcon) in a JPanel in a JFrame. If I use the above codes to generate the GUI, it takes half a second to show all JLabels after the window is displayed. Why aren't the JLabels shown at the same time the window is displayed?
Any help will be appreciated. Thank you so much for your time :)
The frame is not set to be visible before the insets values are calculated, so they are all 0 (zero), am I getting it right?
Correct, components do not have a size until the frame has been realized, which means the native objects on the OS you are running on have been created. This is done when you pack() the frame or make the frame visible.
If you are trying to make the panel that contains the label a size of (300, 125) then the easier approach would be to do:
JPanel panel = new JPanel();
panel.setPreferredSize( new Dimension(300, 125) );
panel.add(...);
frame.add( panel );
frame.pack();
//frame.setLocation(...);
//frame.setLocationByPlatform();
frame.setVisible( true );
Now the pack() method will determine the size of the frame based on the preferred size of all the components added to the frame. No need to worry about insets.
Note: as a general rule you should not invoke setPreferredSize() on a component, since each component is responsible for determining its own size.

Component on JPanel not showing in original size

I made a basic game on a JFrame and I'm currently trying to add a scoreboard on top my frame by using two separate JPanels. I tried to do it with WindowBuilder but the problem is that my GameFrame class component isn't shown fully in the game frame. It looks like this:
The code is as follows:
JFrame frame = new JFrame("Game");
frame.setSize(500, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 100, 500, 600);
panel.add(new GameFrame());
frame.getContentPane().add(panel);
frame.setVisible(true);
How can I solve the problem?
A JPanel uses FlowLayout by default which respects preferred sizes. As its unlikely that this is currently overridden for your GameFrame component class, you need to use a layout manager which uses the maximum area available
panel.setLayout(new BorderLayout());
I think setting the layout to null is this issue. Set a proper Layout as shown below
frame.setLayout(new GridLayout(1,1));)
Have you tried one of these: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html ?
If so, what's not working?
A BorderLayout could be useful if you want the scoreboard along the top, by using panel.add(new ScoreBoard(), BorderLayout.PAGE_START) for example.

Why is this JPanel not sticking to the specified size?

So, I'm trying to learn Java Swing and custom components. I've created a JFrame, given it a background color, and added a JPanel:
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 2000);
frame.setBackground(Color.WHITE);
JPanel jp = new JPanel();
jp.setBackground(Color.BLUE);
jp.setSize(40, 40);
frame.add(jp);
frame.setVisible(true);
The result is a 1000x2000 window colored blue (as opposed to a white window with a 40x40 blue box inside it). Why is the JPanel expanding beyond its specified size?
Using your code, simply add one line to change the LayoutManager of the JFrame. Then, when you add the component, it will keep it's preferred size.
Also, instead of calling jp.setSize(40,40), call jp.setPreferredSize(new Dimension(40,40)).
And, you need to call pack() on the JFrame to tell it to layout its components.
JFrame frame = new JFrame("Testing");
frame.setLayout(new FlowLayout()); // New line of code
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1000, 2000)); // Modified line of code
frame.setBackground(Color.WHITE);
JPanel jp = new JPanel();
jp.setBackground(Color.BLUE);
jp.setPreferredSize(new Dimension(40, 40)); // Modified line of code
frame.add(jp);
frame.pack(); // added line of code
frame.setVisible(true);
Also, you should read up on all of the different LayoutManagers available to you. Here is a great tutorial.
The default layout manager for a JFrame is BorderLayout. When you add a component to the frame without constraints, it uses BorderLayout.CENTER as the default constraint. This means the component takes up all available space, regardless of its requested size.

Categories

Resources