Can't create a menu in swing, what's wrong?

72 Views Asked by At

I have very little time so I'll be succinct.

import java.awt.*;
import javax.swing.*;

  public class GameMenu {
    public static void addComponentsToPane(Container cone){     
  JPanel panelA = new JPanel();
  cone.add(panelA);

  panelA.setLayout(new BoxLayout(800, 800));
  JButton b1 = new JButton("one");
  JButton b2 = new JButton("two");
  JButton b3 = new JButton("three");  
  JButton b4 = new JButton("four");
  JButton b5 = new JButton("five");
  JButton b6 = new JButton("six");
  JButton b7 = new JButton("seven");
  JButton b8 = new JButton("eight");
  JButton b9 = new JButton("nine");  

  public GameMenu(){

panelA.setLayout(null);
panelA.setBounds(800, 800);
b1.setBounds(50, 200);
b2.setBounds(350, 200);
b3.setBounds(650, 200);
b4.setBounds(50, 400);
b5.setBounds(350, 400);
b6.setBounds(650, 400);
b7.setBounds(50, 600);
b8.setBounds(350, 600);
b9.setBounds(650, 600);

}

public static void main(String[] args) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      createAndShowGUI();

        }

The constructor javax.swing.BoxLayout is undefined and the entirety of public GameMenu() cannot be resolved. Help with fixing this?

2

There are 2 best solutions below

0
On

You can't set the layout of a JPanel "globally"; you must do it within the constructor.

Also, I don't know why you are changing the layout type twice (first, it's BoxLayout and then it's Null Layout).

0
On

You did not add your buttons to JPanel.Add your button in panel like this:

panelA.add(b1);
panelA.add(b2);
// and so on