0

Okay so I've looked around for answers to this issue and I can't seem to find any that work with my code. It's a bit messy as it's one of my first Java programs. The JButton and Image don't want to align to the center and I'm not sure what I'm doing wrong.

Here's the code I have now:

import java.awt.Color;
import java.awt.Component;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.DimensionUIResource;

public class lightswitch {
    public static void main(String[] args) {
        int scene = 0;
        Color cGray = new Color(50, 50, 50);
        JFrame window = new JFrame("LightSwitch");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setPreferredSize(new DimensionUIResource(1080, 720));

        SwingUtilities.invokeLater(new Runnable(){
           public void run() {
               switch (scene) {
                   case 0:
                        drawWindow(window, cGray, 1);
                        break;
                   default:
                        break;
               }
           } 
        });
    }

    public static void drawWindow(JFrame window, Color bgc, int title) {
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.setAlignmentX(Component.CENTER_ALIGNMENT);

        if (title == 1) {
            //create logo
            ImageIcon logoIcon = new ImageIcon("./paks/img/logo.png");
            p.add(new JLabel(logoIcon));

            //create play button
            JButton playButton = new JButton("Play");
            playButton.setPreferredSize(new DimensionUIResource(250, 50));
            playButton.setAlignmentX(Component.CENTER_ALIGNMENT);
            p.add(playButton);

            p.setBackground(bgc);
            window.getContentPane().add(p);
            window.pack();
            window.setVisible(true);
        } else {
            p.setBackground(bgc);
            window.pack();
            window.setVisible(true);
        }
    }
}

Please try and make your answers kind of noob-friendly

Also, I'm not sure if this makes any difference but I am using VSCode and Java 16.0.1.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You need to add the image-JLabel and the JButton to a JPanel, perhaps one using BorderLayout, with the JLabel BorderLayout.Center, and the button (or a JPanel that holds the JButton) BorderLayout.PAGE_END. These inner JPanels need to be non-opaque, e.g., `.setOpaque(false)`. Then the main container, likely a JPanel should use GridBagLayout, and then the BorderLayout using JPanel is added to the main one. Done. – Hovercraft Full Of Eels Jun 25 '21 at 01:19
  • There are different ways to do this. If you want to use your `BoxLayout` then you need to: 1) align both the button and label using the alignmentX and 2) add `p.add(Box.createVerticalGlue())` to the panel before the label and after the button. Also, follow Java class naming conventions. Class names should start with an upper case character. Learn by example from the class names of the Java API. – camickr Jun 25 '21 at 01:27

0 Answers0