How to create a rectangle with multiple images inside? - java

What is the best way to display let's say rectangle (3x5) with icons 20x20 px.? I want to change the image file of every pic icon later (= it's not just static pictures). I tried to make JFrame full of JPanels, but i was able to display only one panel at a time. I don't want to use GridLayout, because I need just small rectangle inside a frame. Any ideas how to do it? Couldn't find any tutorial or solution. I'm completely new to GUI developement. Thanks

You do want to use a GridLayout. Your problem is that the JFrame you put the icons into uses a BorderLayout by default (and really, you shouldn't change the layout of a top level component).
What this means is that, if you add multiple panels to the frame, without using one of the NORTH, EAST, SOUTH, WEST constraints, only one of the panels will be visible and take up all the space. If you use a GridLayout for that one panel you get, the icons will be stretched, because the panel receives all the space due to the frame's BorderLayout. An alternate layout that doesn't stretch its contents is FlowLayout, but the layout to use depends heavily on your context.
To display the icons, a JLabel is handy. Use an ImageIcon for the label's icon. You can later use setIcon() on the label to choose a new icon.
overall, my approach would be this:
use a JFrame which has a BorderLayout
to the frame, add a JPanel to the frame. The default layout is a FlowLayout, which will prevent the stretching
to the panel, add a JPanel with an appropriate GridLayout
to that panel, add the JLabels, each having an appropriate ImageIcon

Related

CardLayout - positioning components

Is there any way to tell a JPanel using CardLayout where to add a component?
Let' s say I have one of these panels in the center of a frame, and I want to display 3 components inside that panel. Would this be possible?
Sure, it's easy. Just put a JPanel as one of the cards, then position the components in the panel using whatever layout works best.

How to make a JPanel dynamic?

I have a JFrame that has a JPanel inside. I call "setPreferredSize(new Dimension(500, 600));" but I want the JPanel and its contents to resize when someone resizes the JFrame.
BorderLayout is the way to go. Components start at their preferred size, but are expanded as needed to fill the region they are in.
Set your layout on your frame with BorderLayout
Add your JPanel by
frame.add(yourPanel, BorderLayout.CENTER);
This will allow it to stretch vertically & horizontally
As for the Contents inside a JPanel, give it a layout that will accommodate stretching as well.
Use a layout manager instead of setting the bounds for each component.
It is going to vary from program to program how you want your components to move.
Take a look at this and try to see which layout will work best for you.

How to put two java swing JPanels next to each other and have a table in it?

I am trying to build my own "Battleship" game and have problems with swing.
I now read endless docs on oracle tutorials on LayoutManagers, but not any of them works as I understand them. They only add a few buttons, but never two individual panels.
JPanel Background = new JPanel();
Background.setLayout(new BoxLayout(Background, BoxLayout.X_AXIS));
panelPlayer = new JPanel();
panelPlayer.setBorder(BorderFactory.createLineBorder(Color.black));
panelPlayer.setSize(700, 600);
// PC Field
panelPc = new JPanel();
panelPc.setBorder(BorderFactory.createLineBorder(Color.black));
panelPc.setSize(700, 600);
//adding to frame
getContentPane().add(Background);
Background.add(panelPlayer);
Background.add(panelPc);
After that I have a loop thats adds 16x16 buttons in a JButton[] once for every panel.
How to get the two panels to show a table layout?
I used GridLayout before, the grid works, but it always takes up the whole space of the frame, not of the Container or Panel or else. The panels are overlapping then.
GridBagLayout just puts the buttons in a row and beyond the screen.
Don't fix the size of the panel while using any layout. It works only when you use null layout
You can achieve your goal with GridBagLayout. While adding buttons specify gridx, gridy correctly, it will add buttons like table
just keep nesting the layouts.
in your case make a big one with two sides -
then in each side place another panel with your grid.
You can solve this by nesting panels. Each panel has its own layout manager, so it is a matter of breaking up your UI into pieces and choosing the layout manager for each piece.
If you want two panels side-by-side, then the panel that contains them should have a FlowLayout manager with horizontal orientation. Create a panel with FlowLayout and add the panels to it.
If each of the the side-by-side panels needs the grid of buttons, then set the panel layout to GridLayout and put the buttons in the panel. This fits what I remember of Battleship; in a grid layout, all the grid elements remain the same size no matter how the window is resized.
That should get you started. If, as I expect, you will want another panel with some game controls on it, look into BorderLayout; it has a section on each edge of a rectangle and another in the middle. Put the panel containing the two grids in the center of a panel using BorderLayout, and then your game controls can go in a panel to the north, south, east, or west of that.
Good luck. Let us know if you have a specific problem (in another question).

Java - Having trouble setting JFrame to a good size

I'm making Minesweeper as a school project. It's close to completion, but the only problem now is setting JFrame's size. I just can't figure out a way to set frames to the size I want.
The program looks almost like a Swing version of the original Minesweeper on Windows XP.
The main frame's layout is flow layout. There's a top panel for the time, mines, and reset button. The top panel's using flow layout, and the bottom panel's using grid layout for the buttons.
I set the preferred size of the frame's content pane. Getting the width is easy (The numbers of fields in a row * my button size), but the problem is getting the height right. The frame always go down to the 2nd last row of the minefield.
I also tried pack() but it resizes it to the preferred size of the content pane, which isn't the right size to begin with. What can I do?
Don't have the JFrame (or better its contentPane) use FlowLayout since this won't give the JFrame the best size for its components. Instead why not have it use the default BorderLayout? Your mine cell's will probably have their getPreferredSize() method overridden and thus will direct the size of the enclosing containers. As always, call pack() on the JFrame after filling it with components and before calling setVisible(true) on it.
Set a preferred size for the buttons in the GridLayout and pack() the frame after adding them.
Don't try to manually set the size. You should let each component display at its preferred size and use the pack() method.
The main frame's layout is flow layout. There's a top panel for the time, mines, and reset button
I would use a BorderLayout. Create a top panel and add it to the NORTH.
Then create a panel for the grid and add it to the CENTER. If you have problems with the buttons in the grid resizing then try creating a JPanel as a wrapper panel. Add the buttons to this panel and then add this panel to the CENTER of the frame. The panel will retain its preferred size.

java border gui

I am not good with GUIs or User Interfaces in Java.
Would a Border or JPanel be good for something like the image below?
So, what would be the best option for me? Thank you.
Read the section from the Swing tutorial on Using Layout Managers. You can easily nest panels to get the desired effect.
Maybe start with a BorderLayout. Then you can add a panel that uses a GridLayout, which contains all your image tiles, to the CENTER of the BorderLayout. Then you can add the scrollpane containing the text area to the SOUTH. Then you can create another panel to add to the EAST.
Be creative and experiment.
You can make 4 seperate panels for a border, using BorderLayout.NORTH,BorderLayout.EAST,BorderLayout.SOUTH,and BorderLayout.WEST, This is the easiest way in my opinion.
By the way, in the top right of your picture, where you wanted the information panel, you should put an information LABEL (JLabel) instead, because they hold text. JLabel topRight = new JLabel(); then set the text, position, etc.
p.s. to erase the borders around every tile (if you want to do so), use setBorderPainted(false).

Categories

Resources