How do I increase the height of custom JPanel in a JFrame? - java

I want to increase the height of a custom JPanel in a JFrame. I tried to use pa.setSize(700,200) but it does not change anything.
Here is the code:
JFrame f = new JFrame("Hello");
f.setResizable(true);
JPanel pa = new JPanel();
JButton btn = new JButton("Exit");
pa.setBackground(Color.red);
pa.setSize(700, 200);
f.setUndecorated(true);
f.getContentPane().add(pa, BorderLayout.NORTH);
f.setSize(new Dimension(700,700));
f.setLocation(500, 500);
f.setVisible(true);

its very simple solution instead of pa.setsize() change it into pa.setPreferredSize(new Dimension(width,60));

Related

JPanel won't show in JFrame

I'm trying to create a JPanel (non-resizable) showing a grid of buttons but when I try to add the JPanel to a JFrame it won't show.
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
frame.setSize(681,920);
frame.setResizable(true);
JLabel label = new JLabel();
label.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
JButton btn = new JButton();
btn.setContentAreaFilled( false );
btn.setBorder( null );
btn.setBounds(214,210,0,0);
label.add(btn);
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
The output should be a resizable frame with inside a 3x4 grid of buttons.
If I don't use the panel and I put the line frame.setResizable(false) it works as expected but I need to add more stuff to the frame so I need to put the buttons safe in a panel.
Both panel and label are added to your frame, to make sure they are added write
JLabel label = new JLabel("JLABEL");
and
panel.setBackground(Color.BLUE);

JFrame always on top, but below another jframe?

My problem is, that I have 3 different JFrames (I can not put 3 in one because all include ImageIcons and Mouslisteners). When I open the programm they are exactly where I want them to be, but when I click on the JFrame in the background it appears in the foreground and the others are moving to the background..
I know that I can set a JFrame.alwaysOnTop(true) but when I set one on top, the other one is moving to the background.
So I need something like a command where I can set one JFrame as the Background, one as the Foreground and one as something in between and it should not be switchable by clicking. Can somebody help me?
ImageIcon img1 = new ImageIcon(getClass().getResource("/image/x.png"));
ImageIcon img2 = new ImageIcon(getClass().getResource("/image/y.png"));
ImageIcon img3 = new ImageIcon(getClass().getResource("/image/z.png"));
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();
JFrame frame3 = new JFrame();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
label1.setIcon(img1);
label2.setIcon(img2);
label3.setIcon(img3);
JPanel panel1 = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel1 = new JPanel();
panel1.add(label1);
panel2.add(label2);
panel3.add(label3);
frame1.setContentPane(panel1);
frame2.setContentPane(panel2);
frame3.setContentPane(panel3);
panel1.setBorder(null);
panel2.setBorder(null);
panel3.setBorder(null);
frame1.setVisible(true);
frame2.setVisible(true);
frame3.setVisible(true);
The expectet result is a full screen background jframe with one big jframe in the middle of it and on top of the middle-JFrame there is another JFrame. And I dont want the JFrames to change their position.
Thank you!
You have to set the JFrames' "FocusableWindowState" to false.
frame1.setFocusableWindowState(false);
frame2.setFocusableWindowState(false);
frame3.setFocusableWindowState(false);
To put one to the background an anotherone to the foreground you just have to set them visible in a specific order.
frame1.setFocusableWindowState(false);
frame2.setFocusableWindowState(false);
frame3.setFocusableWindowState(false);
Like that the first JFrame will be in background, the second in between and the last in foreground.
ImageIcon img1 = new ImageIcon(getClass().getResource("/image/x.png"));
ImageIcon img2 = new ImageIcon(getClass().getResource("/image/y.png"));
ImageIcon img3 = new ImageIcon(getClass().getResource("/image/z.png"));
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();
JFrame frame3 = new JFrame();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
label1.setIcon(img1);
label2.setIcon(img2);
label3.setIcon(img3);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
panel1.add(label1);
panel2.add(label2);
panel3.add(label3);
frame1.setContentPane(panel1);
frame2.setContentPane(panel2);
frame3.setContentPane(panel3);
panel1.setBorder(null);
panel2.setBorder(null);
panel3.setBorder(null);
frame1.setFocusableWindowState(false);
frame2.setFocusableWindowState(false);
frame3.setFocusableWindowState(false);
frame1.setVisible(true);
frame2.setVisible(true);
frame3.setVisible(true);
I hope that I could help you!

Java - Painting

Hello im trying to create a grid in java and paint each cell but I dont kown what im doing wrong. Each cell is a JPanel and I add each cell to the to the mazePanel. After that the mazePanel is added to the frame. But only get the result shown in the next image:
public void printFrame() {
JFrame frame;
JPanel mazePanel = new JPanel();
frame = new JFrame("The Maze");
frame.setSize(600, 600);
mazePanel.setLayout(new GridLayout(2, 2));
mazePanel.setBackground(Color.cyan);
JPanel cell = new JPanel();
cell.setBackground(Color.gray);
mazePanel.add(cell);
cell.setBackground(Color.BLACK);
mazePanel.add(cell);
cell.setBackground(Color.red);
mazePanel.add(cell);
cell.setBackground(Color.GREEN);
mazePanel.add(cell);
frame.add(mazePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
A Component cannot be added to a Container more than once (eg cannot have multiple parents). In this case, what you see is the result of the last addition of the variable 'cell' to its parent mazePanel (green - together with the background color of mazePanel (cyan) due to the layout). If you wish to add multiple 'cells', then create new JPanel's with the appropriate background color and add them to the mazePanel
JFrame frame;
JPanel mazePanel = new JPanel();
frame = new JFrame("The Maze");
frame.setSize(600, 600);
mazePanel.setLayout(new GridLayout(2, 2));
mazePanel.setBackground(Color.cyan);
JPanel cell = new JPanel();
JPanel cell2 = new JPanel();
JPanel cell3 = new JPanel();
JPanel cell4 = new JPanel();
cell.setBackground(Color.gray);
mazePanel.add(cell);
cell2.setBackground(Color.BLACK);
mazePanel.add(cell2);
cell3.setBackground(Color.red);
mazePanel.add(cell3);
cell4.setBackground(Color.GREEN);
mazePanel.add(cell4);
frame.add(mazePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
You have to create separate cell objects like mentioned above. I believe this gives you what you are looking for.

How i can change JButton size in java?

I am learning java GUI programming and I would like to create button. Problem is when button is created it is huge. How I can resize button?
JFrame frame = new JFrame("Ikkuna <3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel teksti = new JLabel("assdad", SwingConstants.CENTER);
teksti.setFont(new Font("Eras Demi ITC", Font.PLAIN, 32));
teksti.setForeground(Color.BLUE);//tekstin väri
ImageIcon img = new ImageIcon("C:/Users/account/Documents/NetBeansProjects/eka/src/eka/Kuvat/Trollface.PNG"); //Tämä on kuvake(icon).
frame.setIconImage(img.getImage());
JButton nappula = new JButton("Start");
frame.getContentPane().add(nappula);
nappula.setPreferredSize(new Dimension(83, 291));
nappula.setLocation(500, 350);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
teksti.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(teksti, BorderLayout.PAGE_START);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
I also tried this:
nappula.setPreferredSize(new Dimension(83, 291));
You Can set the fonts to the button and change its size
Font f = new Font("Times New Roman",Font.BOLD,20);
nappula.setFont(f);
Set it of any size change 20 to any value......
Try it....
You're using default layout from the looks of it. Try using
nappula.setSize(new Dimension(x, y));

JButtons only appear on JFrame if in BorderLayout.CENTER, not SOUTH or NORTH

So I'm trying to create a gui, I've tinkered with gui's before in java but I'm still new to them. So my issued here is that my JLabels (butLabel & cbLabel) are filled with buttons and checkboxes. Sadly my JFrame will only show whichever is set to the BorderLayout.CENTER. NORTH & SOUTH don't ever show, even if I only set the butLabel to SOUTH and don't even use the cbLabel. What am I overlooking?? It's much appreciated, thanks!
public class mainWindow
{
JFrame frame = new JFrame("Main Window");
JLabel butLabel = new JLabel();
JLabel cbLabel = new JLabel();
JButton showBut = new JButton("Show");
JButton exitBut = new JButton("Exit");
JButton addBut = new JButton("Add");
JButton remBut = new JButton("Remove");
JCheckBox aCB = new JCheckBox("Airplane");
JCheckBox bCB = new JCheckBox("Boat");
JCheckBox cCB = new JCheckBox("Clock");
public mainWindow()
{
frame.setLayout(new BorderLayout()); //I know this is set by default to BorderLayout but I just did it when I was out of options to try.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(360, 480));
butLabel.setLayout(new GridLayout(1,4));
cbLabel.setLayout(new GridLayout(2, 2));
butLabel.add(showBut);
butLabel.add(exitBut);
butLabel.add(addBut);
butLabel.add(remBut);
cbLabel.add(aCB);
cbLabel.add(bCB);
cbLabel.add(cCB);
frame.add(butLabel, BorderLayout.CENTER);
frame.add(cbLabel, BorderLayout.NORTH);
}
public void setVisible()
{
butLabel.setVisible(true);//Didn't think I needed butLabel.setVisible or the cbLabel.setVisible but
cbLabel.setVisible(true);//again I was trying things that I thought might make sense.
frame.setVisible(true);
}
}
do not use Label for grouping elements, use JPanel instead
I have tried replace all
Label
with
Panel
it works

Categories