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.