I am trying to make a minesweeper that has a different space for a smiley icon that we can click and the buttons which we have to click to play.
public final class testFrame extends JFrame implements MouseListener, ActionListener {
private JFrame screen = null;
private JPanel composite = new JPanel();
public testFrame() {
screen = new JFrame();
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
screen.setResizable(true);
composite.setLayout(new BorderLayout());
//this button is not showing also
JButton button = new JButton("Text goes here");
composite.add(button);
Container cp = screen.getContentPane(); // JFrame's content-pane
cp.setLayout(new GridLayout(5, 5, 2, 2)); // in 10x10 GridLayout
//codes to add buttons
}
So here I am trying to add the container cp to the screen. But it opened
two screen
Sorry if this seems like minor things but I am really new to this java GUI so please help me.
EDIT:
I removed the extends JFrame and used the screen instead. It kinda works but I can't seem to put the container cp to a panel. The requirement is that I have to use container cp. So I cannot change. Thank you
public final class TestFrame implements MouseListener, ActionListener {
private JFrame screen = null;
private JPanel composite = new JPanel();
private JPanel topPanel = new JPanel();
public TestFrame() {
screen = new JFrame("TestFrame");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
topPanel.setLayout(new BorderLayout());
//composite.setLayout(new BorderLayout());
//button in topPanel
JButton button = new JButton("Text goes here");
topPanel.add(button, BorderLayout.PAGE_START);
//Content Pane
Container cp = screen.getContentPane();// JFrame's content-pane
cp.setLayout(new GridLayout(5, 5, 2, 2)); // in 10x10 GridLayout
//composite.add(cp, BorderLayout.CENTER);
screen.add(topPanel);
// screen.add(composite);
}
Now it looks like
this
There are a few things.
You are extending JFrame as well as using it as an attribute of the same class so you can ether use this instead of screen or you remove the extends JFrame as it is redundant and not needed
To answer your question. A JFrame contains a Panel called ContentPane this pane is were you add you panels to (you already did the layout thing right). So the solution is:
cp.add(composite);
You've got too many JFrames, the testFrame class which extends JFrame (and which should be re-named TestFrame to comply with Java naming conventions) and the screen variable. Use only one.
You can and should nest JPanels to achieve your desired result. For instance if you want a grid as well as some control buttons, create a JPanel, give it a BorderLayout, put your grid JPanel in the BorderLayout.CENTER postion and a JPanel with control JButtons in a different position, say BorderLayout.PAGE_START
Related
I'm trying to put a JPanel inside OR on a JPanel, whichever may be the case, ultimately I just want this to work like this
As you can see on the picture, the red line is a JFrame and it has 2 JPanels inside it, on the green JPanel there are some different JPanels.
I need help with the green JPanel and the little JPanels inside it. Is there any way to make it work like this?
Any help would be greatly appreciated!
==============EDIT 1==============
So here is some code, to show you what I've done so far with the help of #hfontanez.
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class Main
{
public static void main(String[] args)
{
//JFrame
JFrame jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(1920, 1080);
jframe.setResizable(false);
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
//parentJpanel - This is the main panel
JPanel parentJpanel = new JPanel();
parentJpanel.setBackground(Color.YELLOW);
parentJpanel.setSize(1920, 1080);
parentJpanel.setLayout(new BorderLayout());
//smallPanel - This is the little panel on the bottom
JPanel smallPanel = new JPanel();
smallPanel.setBackground(Color.GREEN);
smallPanel.setSize(1920, 300);
smallPanel.setLocation(0, 780);
smallPanel.setLayout(new BoxLayout(smallPanel, BoxLayout.PAGE_AXIS));
parentJpanel.add(smallPanel);
jframe.add(parentJpanel);
}
}
I expected the top part to be yellow, and the small part on the bottom to be green, yet the whoel thing turned green. What did I do wrong?
The pictured GUI is created using three panels.
The YELLOW panel is the game play area. It has no layout, no components (which define their own preferred sizes) and is custom painted, so it defines a sensible preferred size to report to the layout manager.
The GREEN panel contains controls. It uses a FlowLayout.
The RED panel uses a BorderLayout, and puts the YELLOW panel in the CENTER and the GREEN panel in the PAGE_END.
Code
This is the code that made the screenshot seen above.
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class GameLayout {
GameLayout() {
// The main GUI. Everything else is added to this panel
JPanel gui = new JPanel(new BorderLayout(5, 5));
gui.setBorder(new EmptyBorder(4, 4, 4, 4));
gui.setBackground(Color.RED);
// The custom painted area - it is a panel that defines its preferred size.
gui.add(new GamePanel());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
buttonPanel.setBackground(Color.GREEN);
for (int ii = 1; ii<5; ii++) {
buttonPanel.add(new JButton("B " + ii));
}
gui.add(buttonPanel,BorderLayout.PAGE_END);
JFrame f = new JFrame("Game Layout");
f.setContentPane(gui);
f.setLocationByPlatform(true);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
Runnable r = () -> new GameLayout();
SwingUtilities.invokeLater(r);
}
}
class GamePanel extends JPanel {
GamePanel() {
setBackground(Color.YELLOW);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 100);
}
}
You need to use a LayoutManager so when you put the JPanel inside the other JPanel it will have the correct look. If you simply put panels inside the others, the parent JPanel will use its default layout manager, which is FlowLayout.
For the look of it, it seems you need to use Border Layout for the parent (yellow) panel. For the green, you have options, but I think your best bet is to use Box Layout with a PAGE_AXIS Component Orientation.
In general, you need to be familiarized with two things: 1) Layout Managers and how they behave, and 2) the default layout behavior of JComponents.
i am trying to design a JFrame with 2 different JPanels in it, one on the left with AbsoluteLayout and one on the right with a GridLayout with variable dimensions.
After adding some components to the JPanels, i add them to the JFrame contentPane and use the method JFrame.pack() hoping to get a JFrame with the minimum size possible that can show all of its components, but what i am getting is the minimum size to show only the JPanel on the right with the GridLayout, the JPanel on the left gets overlapped by the one on the right.
Is there any good way to use the JFrame.pack() method and it still shows both JPanel completly?
Here is the code:
public class GameGUI extends JFrame{
private int labSize;
private JFrame mainFrame;
private JPanel labPanel;
private JPanel choicesPanel;
private JButton exitButton;
private JButton replayButton;
public GameGUI(int n) {
labSize=n;
mainFrame = new JFrame("Maze Game");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(new GridLayout(1, 0));
mainFrame.setVisible(false);
labPanel=new JPanel(new GridLayout(labSize,labSize));
choicesPanel=new JPanel(new GridLayout(0, 1));
choicesPanel.setLayout(null);
replayButton=new JButton("Replay");
replayButton.setBounds(10, 11, 80, 30);
exitButton=new JButton("Exit");
exitButton.setBounds(10, 51, 80, 30);
choicesPanel.add(replayButton);
choicesPanel.add(exitButton);
mainFrame.getContentPane().add(choicesPanel);
mainFrame.getContentPane().add(labPanel);
}
public void refreshLabShowing(char[][] lab){
labPanel.removeAll();
for(int i=0;i<labSize;i++){
for(int u=0;u<labSize;u++){
labPanel.add(new JLabel(String.valueOf(lab[i][u])));
}
}
mainFrame.pack();
mainFrame.setVisible(true);
}
}
pack() will only work if with layout managers, as pack() itself queries layout manager for required dimension so here, you cannot use absolute layout and pack().
I am working with Swing, trying to learn how to use it. I am doing an excercise in my book where I am supposed to make buttons (JButtons) like the ones a dvd-player has. I am adding no funcionality to the buttons at this point. The program worked just fine when I first ran it. Then I thought that I would expand it by making a second panel (JPanel) with the same buttons. When I run my code however, I just get one set of buttons (not two areas with two sets of buttons).
Have I misunderstood the concept of Panels? I have understood a Panel to be an area (a container) in my frame where I can display different output, and that I can have several panels i one frame.
Here is my code:
import javax.swing.*;
public class Oppgave91 extends JFrame
{
public Oppgave91()
{
super ("We make buttons");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton play = new JButton("Play");
JButton stopeject = new JButton("Stop/Eject");
JButton rewind = new JButton("Rewind");
JButton fastforward = new JButton("FastForward");
JButton pause = new JButton("Pause");
JPanel panel = new JPanel();
panel.add(play);
panel.add(stopeject);
panel.add(rewind);
panel.add(fastforward);
panel.add(pause);
JPanel panel2 = new JPanel();
panel2.add(play);
panel2.add(stopeject);
panel2.add(rewind);
panel2.add(fastforward);
panel2.add(pause);
add(panel);
add(panel2);
setVisible(true);
}
public static void main(String[] args)
{
Oppgave91 showbuttons = new Oppgave91();
showbuttons.pack();
}
}
A component can have only 1 parent. See the last line of my code snippet for how it should be done
JPanel panel = new JPanel();
panel.add(play);
panel.add(stopeject);
panel.add(rewind);
panel.add(fastforward);
panel.add(pause);
//right now panel is the parent component of play, stop, eject
JPanel panel2 = new JPanel();
panel2.add(play); //play is now owned by panel2, not panel
panel2.add(stopeject); //stopeject is now owned by panel2, not panel
panel2.add(new JButton("Rewind")); // this is how you should do this
I am creating a JPanel form which will contain several other JPanels. I want to place this inside a JScrollPane. Then I want to place the JScrollPane into a JTabbedPane as one of the tabs. I'm having a problem though -- my JPanel form winds up expanding when placed in the scrollpane even though I have set size, preferredsize, maximumsize, etc.
public class test
{
private static JFrame frame = new JFrame();
private static JTabbedPane pane0 = new JTabbedPane();
private static JScrollPane pane1 = new JScrollPane();
private static JPanel pane2 = new JPanel();
//add the rest of your JPanels here
public static void main(String[] args)
{
frame.setSize(400,400);
//add all the other attributes here
frame.add(pane0);
pane0.add(pane1);
pane1.add(pane2);
//go ahead and add the rest of your panels here
frame.pack();//resizes the frame so that its subcomponents fit well inside.
}
}//this last bracket is for the class itself. Sorry i couldn't tab everything the right //way.
Is this what you're trying to do? That's what i understood from your question. By the way, if your JPanel is expanding, change the size of your frame as well.
We are working on a project where we encountered a problem with including more than two Panels on the same JFrame .What we want is one Panel above the other.
Can the community help give an example of ho to implement this or refer me to a good tutorial or guide related to our Java Swing needs?
Assuming you want two panels added to a single frame:
Set a layout for your parent JFrame and add the two panels. Something like the following
JFrame frame = new JFrame();
//frame.setLayout(); - Set any layout here, default will be the form layout
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
frame.add(panel1);
frame.add(panel2);
Assuming you want to add one panel over the other
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
frame.add(panel1);
panel1.add(panel2);
There is no limit on the number of panels to be added on the JFrame. You should understand that they all are containers when seen on a higher level.
if you want each of the frames/panels the same size, use the GridLayout, with a grid of 1(column) and 2(rows)
Frame myFrame;
GridLayout myLayout = new GridLayout(2,1);
myFrame.setLayout(myLayout);
Panel p1;
Panel p2;
myFrame.add(p1);
myFrame.add(p2);
if the panels are different size use the BorderLayout.... set the upper frame to "North" and the lower one to "South" or "Center"
Frame myFrame;
myFrame.setLayout(new BorderLayout() );
Panel p1;
Panel p2;
myFrame.add(p1, BorderLayout.NORTH);
myFrame.add(p2, BorderLayout.CENTER);
//you can also use card Layout, that enables you to add multiple card-panels on Main panel.
CardLayout cl;
JPanel main,one,two,three;
JButton button1,button2;
cl = new CardLayout();
main.setLayout(cl);
main.add(one,"1");
main.add(two,"2");
main.add(three,"3");
cl.show(main,"1");
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1)
cl.show(main,"2");
else if(e.getSource() == button2)
cl.show(main,"3");
}