Java Swing GUI in other classes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Java Swing GUI in other classes



I'm currently working on my first Swing project in Java. I want to split my GUI into 3 seperate classes. My main class, TicTacToeGUI, and two others: MenuBar and Board.


TicTacToeGUI


MenuBar


Board



I have a problem with my menu. I can't figure out how to make it visible. The code is compiling but the menu is not showing.



TicTacToeGUI:


package TicTacToee;

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

public class TicTacToeGUI {

public TicTacToeGUI() {
super();
Board board = new Board();
}

public static void main(String args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TicTacToeGUI window = new TicTacToeGUI();
//window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}



Board:


package TicTacToee;

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

public class Board {

private JFrame frame;
private JButton board;

public Board(){
initializeBoard();
}

public void initializeBoard() {
frame = new JFrame();
frame.setTitle("Tic Tac Toe v0.1");
frame.setBounds(100, 100, 600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
frame.setResizable(false);
//Creating MENU
MenuBar menuBar = new MenuBar();
frame.setJMenuBar(menuBar);
frame.setVisible(true);

//JPanel panel = new JPanel();
//panel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
//frame.getContentPane().add(panel, BorderLayout.CENTER);
//panel.setLayout(new GridLayout(3, 3, 2, 2));


//board = new JButton[3][3];
}
}



MenuBar:


package TicTacToee;

import javax.swing.*;

public class MenuBar extends JMenuBar {

private JMenuBar menuBar;
private JMenu menu;
private JMenuItem newGame;
private JMenuItem quit;

public MenuBar(){
initializeMenuBar();
}

public void initializeMenuBar() {
//Create menu Bar
menuBar = new JMenuBar();

menu = new JMenu("MENU");
menuBar.add(menu);

newGame = new JMenuItem();
menu.add(newGame);
}
}





window.setJMenuBar(new MenuBar())??
– MadProgrammer
4 hours ago


window.setJMenuBar(new MenuBar())





Im a little confused why you extend JMenuBar, but then in the Class you create another JMenuBar object? You need to use “this” instead of calling a new JMenuBar in the MenuBar class.
– Jacob B.
4 hours ago




1 Answer
1



You aren’t extending JMenuBar correctly. Try something like this:


JMenuBar


public void initializeMenuBar(){

menu = new JMenu("MENU");
this.add(menu);

newGame = new JMenuItem();
menu.add(newGame);

}



or maybe


public void initializeMenuBar(){
//Create menu Bar
menuBar = new JMenuBar();

menu = new JMenu("MENU");
menuBar.add(menu);

newGame = new JMenuItem();
menu.add(newGame);

this = menuBar;

}





I think the question is why extend JMenuBar at all? I've never needed to extend it. Just use it as is and call .add() to add menu items. docs.oracle.com/javase/tutorial/uiswing/components/menu.html
– markspace
3 hours ago


JMenuBar


.add()





@markspace That’s what I’D normally do too, but he said he intentionally wanted it separated into three classes so I obliged.
– Jacob B.
3 hours ago





"he said he intentionally wanted it separated into three classes" The menu bar logic might be in another class without extending it. "so I obliged." See Is “Don't do it” a valid answer?
– Andrew Thompson
20 mins ago





@AndrewThompson the thing is, it’s not a “don’t do it” scenario. Extending a JMenuBar can be a valid solution, it’s not going to kill his code. That the great part about software engineering, you can do it however you want as long as it works and (preferably) other people can understand it.
– Jacob B.
18 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results