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.
Related
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
How to accomplish vertical alignment -- JPanels on top of each other with 100% width size?
I want something like this but am unclear how to go about it.
Setting a BoxLayout LayoutManager should help you to align vertically (see Y_AXIS) .
One simple way you can use BorderLayout for that:
frame = new JFrame();
frame.setBounds(100, 100, 495, 311);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
JPanel jPanel = new JPanel();
frame.getContentPane().add(jPanel, BorderLayout.CENTER);
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
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));
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.