Home > OS >  Problem with buttons exceeding the Panel (JAVA)
Problem with buttons exceeding the Panel (JAVA)

Time:05-19

I'm having a problem with a layout I'm trying to do in java. I have 2 panels in a 800x600 frame. The first panel "gamePanel" is (600x600) and the second "menuPanel" is (200x600).

In the menuPanel there are 4 buttons that I tried to organize as a single column of 4 rows using gridLayout(which partially worked). The buttons appear to be in place but when hovering on them they expand occupying the other panel (gamePanel). I tried placing them using setBounds but they directly disappear.

This is how it works before hovering the buttons. After hovering 2 buttons, but all 4 are displayed the same way

Here is the code:

public class Layout {
    Point point = new Point();
    public Layout() {
        JFrame window = new JFrame();
        ImageIcon icon = new ImageIcon("images/icon.jpg");
        
        //JFRAME 
        window.setSize(800,600);
        window.setLocationRelativeTo(null);
        window.setTitle("Arkanoid");
        window.setUndecorated(true);
        window.setIconImage(icon.getImage());
        
        
        //PANELS
        JPanel gamePanel = new JPanel();
        gamePanel.setBackground(Color.RED);
        gamePanel.setBounds(0, 0, 600, 600);
        
        JPanel menuPanel = new JPanel(new GridLayout(4,1));
        menuPanel.setBackground(Color.BLACK);
        menuPanel.setBounds(600,0,200,600);
        menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        
        window.add(gamePanel);
        window.add(menuPanel);
        
        //Buttons
        JButton closeButton = new JButton("Close Me");
        closeButton.addActionListener(e -> System.exit(0));
        menuPanel.add(closeButton);
        
        JButton playButton = new JButton("Play");
        menuPanel.add(playButton);
        
        JButton Button1 = new JButton("Test1");
        menuPanel.add(Button1);
        
        JButton Button2 = new JButton("Test2");
        menuPanel.add(Button2);
        
        //Labels
     

        //SHOW
        window.setVisible(true);
    }   
}

CodePudding user response:

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Laying Out Components Within a Container section.

A JFrame has a default BorderLayout, which I used to place the two JPanels.

I added a main method so I could run the GUI. I commented out the icon code, which doesn't make any sense for an undecorated JFrame. I moved the setLocationRelativeTo method to after the pack method, so the JFrame is actually centered.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ExampleLayout {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ExampleLayout();
            }
        });
    }

    Point point = new Point();

    public ExampleLayout() {
        JFrame window = new JFrame();
//      ImageIcon icon = new ImageIcon("images/icon.jpg");

        // JFRAME
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Arkanoid");
        window.setUndecorated(true);
//      window.setIconImage(icon.getImage());

        // PANELS
        JPanel gamePanel = new JPanel();
        gamePanel.setBackground(Color.RED);
        gamePanel.setPreferredSize(new Dimension(600, 600));

        JPanel menuPanel = new JPanel(new GridLayout(0, 1));
        menuPanel.setBackground(Color.BLACK);
        menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        menuPanel.setPreferredSize(new Dimension(200, 600));

        window.add(gamePanel, BorderLayout.CENTER);
        window.add(menuPanel, BorderLayout.EAST);

        // Buttons
        JButton closeButton = new JButton("Close Me");
        closeButton.addActionListener(e -> System.exit(0));
        menuPanel.add(closeButton);

        JButton playButton = new JButton("Play");
        menuPanel.add(playButton);

        JButton Button1 = new JButton("Test1");
        menuPanel.add(Button1);

        JButton Button2 = new JButton("Test2");
        menuPanel.add(Button2);

        // Labels

        // SHOW
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

}
  • Related