Title: The method createGUI() is undefined for type JFrame
Tags: java;swing;types;jframe;undefined
Question: I am trying to add a title to my JFrame so it reads "CBallMaze Ball Maze Application" but whenever I type that in when I am creating the JFrame, it gives me the error message in the title.
This code runs fine if I don't try to add a title and have the public static void phrased like this
CBallMaze JFrame = new CBallMaze();
JFrame.setSize(775, 650);
JFrame.createGUI();
JFrame.setVisible(true);
Current Code =
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CBallMaze extends JFrame implements ActionListener
//Below is where I have declared all the different objects I have used throughout my program
private JButton buttonRight, buttonLeft, buttonUp, buttonDown, buttonTL, buttonTR, buttonBL, buttonBR, buttonCenter, optionOne, optionTwo, optionThree, optionExit, scenarioAct, scenarioRun, scenarioReset;
private JPanel panelCentre, panelRight, panelBottom, buttonPanel, compassPanel, optionsPanel, selectionPanel, panelAct, panelRun, panelReset;
private JTextField optionTF, squareTF, directionTF;
private JLabel option, square, direction;
private Icon iconAct, iconRun, iconReset;
public static void main(String[] args)
JFrame CBallMaze = new JFrame("Title");
CBallMaze.setSize(775, 650);
CBallMaze.createGUI();
CBallMaze.setVisible(true);
private void createGUI()
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout() );
//Panels
panelCentre = new JPanel();
panelCentre.setPreferredSize(new Dimension(625, 450));
panelCentre.setBackground(Color.RED);
window.add(panelCentre);
panelCentre.setLayout(new GridLayout(21, 30));
panelRight = new JPanel();
panelRight.setPreferredSize(new Dimension(180, 450));
panelRight.setBackground(Color.WHITE);
window.add(panelRight, BorderLayout.EAST);
optionsPanel = new JPanel();
optionsPanel.setPreferredSize(new Dimension(150, 100));
optionsPanel.setBackground(Color.WHITE);
panelRight.add(optionsPanel, BorderLayout.EAST);
buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(175, 100));
buttonPanel.setBackground(Color.WHITE);
panelRight.add(buttonPanel, BorderLayout.EAST);
selectionPanel = new JPanel();
selectionPanel.setPreferredSize(new Dimension(175, 150));
selectionPanel.setBackground(Color.WHITE);
panelRight.add(selectionPanel, BorderLayout.EAST);
compassPanel = new JPanel();
compassPanel.setPreferredSize(new Dimension(175, 300));
compassPanel.setBackground(Color.BLUE);
panelRight.add(compassPanel, BorderLayout.EAST);
panelBottom = new JPanel();
panelBottom.setPreferredSize(new Dimension(875, 50));
panelBottom.setBackground(Color.WHITE);
window.add(panelBottom, BorderLayout.SOUTH);
panelAct = new JPanel();
panelAct.setPreferredSize(new Dimension(200, 40));
panelAct.setBackground(Color.WHITE);
panelBottom.add(panelAct, BorderLayout.SOUTH);
panelRun = new JPanel();
panelRun.setPreferredSize(new Dimension(200, 40));
panelRun.setBackground(Color.WHITE);
panelBottom.add(panelRun, BorderLayout.SOUTH);
panelReset = new JPanel();
panelReset.setPreferredSize(new Dimension(200, 40));
panelReset.setBackground(Color.WHITE);
panelBottom.add(panelReset, BorderLayout.SOUTH);
//Displays
option = new JLabel("Option:    ");
optionsPanel.add(option, BorderLayout.LINE_START);
option.setEnabled(true);
option.setForeground(Color.BLACK);
option.setHorizontalAlignment(JLabel.LEFT);
optionTF = new JTextField("");
optionsPanel.add(optionTF, BorderLayout.LINE_END);
optionTF.setEnabled(true);
optionTF.setPreferredSize(new Dimension(50, 25));
optionTF.setHorizontalAlignment(JTextField.CENTER);
square = new JLabel("Square:   ");
optionsPanel.add(square, BorderLayout.LINE_START);
square.setEnabled(true);
square.setForeground(Color.BLACK);
square.setHorizontalAlignment(JLabel.LEFT);
squareTF = new JTextField("");
optionsPanel.add(squareTF, BorderLayout.LINE_END);
squareTF.setEnabled(true);
squareTF.setPreferredSize(new Dimension(50, 25));
squareTF.setHorizontalAlignment(JTextField.CENTER);
direction = new JLabel("Direction:  ");
optionsPanel.add(direction, BorderLayout.LINE_START);
direction.setEnabled(true);
direction.setForeground(Color.BLACK);
direction.setHorizontalAlignment(JLabel.LEFT);
directionTF = new JTextField("");
optionsPanel.add(directionTF, BorderLayout.LINE_END);
directionTF.setEnabled(true);
directionTF.setPreferredSize(new Dimension(50, 25));
directionTF.setHorizontalAlignment(JTextField.CENTER);
//buttons
buttonTL = new JButton("");
buttonPanel.add(buttonTL);
buttonTL.setPreferredSize(new Dimension(45, 25));
buttonTL.setEnabled(false);
buttonUp = new JButton("^");
buttonPanel.add(buttonUp);
buttonUp.setPreferredSize(new Dimension(45, 25));
buttonUp.addActionListener(this);
buttonTR = new JButton("");
buttonPanel.add(buttonTR);
buttonTR.setPreferredSize(new Dimension(45, 25));
buttonTR.setEnabled(false);
buttonLeft = new JButton("<");
buttonPanel.add(buttonLeft);
buttonLeft.setPreferredSize(new Dimension(45, 25));
buttonLeft.addActionListener(this);
buttonCenter = new JButton("");
buttonPanel.add(buttonCenter);
buttonCenter.setPreferredSize(new Dimension(45, 25));
buttonCenter.setEnabled(false);
buttonRight = new JButton("&gt;");
buttonPanel.add(buttonRight);
buttonRight.setPreferredSize(new Dimension(45, 25));
buttonRight.addActionListener(this);
buttonBL = new JButton("");
buttonPanel.add(buttonBL);
buttonBL.setPreferredSize(new Dimension(45, 25));
buttonBL.setEnabled(false);
buttonDown = new JButton("v");
buttonPanel.add(buttonDown);
buttonDown.setPreferredSize(new Dimension(45, 25));
buttonDown.addActionListener(this);
buttonBR = new JButton("");
buttonPanel.add(buttonBR);
buttonBR.setPreferredSize(new Dimension(45, 25));
buttonBR.setEnabled(false);
optionOne = new JButton("Option One");
selectionPanel.add(optionOne);
optionOne.setPreferredSize(new Dimension(125, 25));
optionTwo = new JButton("Option Two");
selectionPanel.add(optionTwo);
optionTwo.setPreferredSize(new Dimension(125, 25));
optionThree = new JButton("Option Three");
selectionPanel.add(optionThree);
optionThree.setPreferredSize(new Dimension(125, 25));
optionExit = new JButton("Exit");
selectionPanel.add(optionExit);
optionExit.setPreferredSize(new Dimension(125, 25));
try
//iconAct = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("Act.jpg")));
iconAct = new ImageIcon("Act.jpg");
catch (Exception e)
System.err.println("Act Icon ImageIcon "+e);
scenarioAct = new JButton("Act");
scenarioAct.setIcon(iconAct);
panelAct.add(scenarioAct);
scenarioAct.addActionListener(this);
scenarioAct.setPreferredSize(new Dimension(75, 30));
try
//iconRun = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("Run.jpg")));
iconRun = new ImageIcon("Run.jpg");
catch (Exception e)
System.err.println("Run Icon ImageIcon "+e);
scenarioRun = new JButton("Run");
scenarioRun.setIcon(iconRun);
panelRun.add(scenarioRun);
scenarioRun.addActionListener(this);
scenarioRun.setPreferredSize(new Dimension(75, 30));
try
//iconReset = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("Reset.jpg")));
iconReset = new ImageIcon("Reset.jpg");
catch (Exception e)
System.err.println("Reset Icon ImageIcon "+e);
scenarioReset = new JButton("Reset");
scenarioReset.setIcon(iconReset);
panelReset.add(scenarioReset);
scenarioReset.addActionListener(this);
scenarioReset.setPreferredSize(new Dimension(75, 30));
Comment: Please, follow the naming convention of Java.

Here is the accepted answer: With this: JFrame CBallMaze = new JFrame("Title");, you're creating a JFrame variable and assigning it a JFrame object, and then expecting it to have CBallMaze behaviors, something that isn't going to happen. Instead, you will want to create a CBallMaze object and assign it to a CBallMaze variable.
So change this:
JFrame CBallMaze = new JFrame("Title");
CBallMaze.setSize(775, 650);
CBallMaze.createGUI();
CBallMaze.setVisible(true);
to this:
CBallMaze cBallMaze = new CBallMaze("Title"); // different variable type and object
cBallMaze.setSize(775, 650); // note the variable begins with lower case letter
cBallMaze.createGUI();
cBallMaze.setVisible(true);
Side recommendations:
Get rid of all those setPreferredSize(...) calls and instead use the layout managers and smart creation of components (i.e., giving JTextFields a column property) to help create a simple and pleasing GUI.
If you're going to call a constructor that takes a String parameter, you'd darn well better make a constructor that takes a String parameter. The first line should call the super(...) constructor and pass that String parameter in.
Comment for this answer: It now says "the constructor CBallMaze(String) is undefined". I do see my error with how my use of JFrame/CBallMaze though, thank you!
Comment for this answer: but creating that makes no difference to the generated frame. am i looking in the wrong place because i'm expecting the title to appear at the top of the window? feel like i've overlooked something incredibly obvious here
Comment for this answer: Yes! Thank you very much. You're a patient human being. That fixed it completely.
Comment for this answer: @wolfiejoe: see edit to answer, and believe what the error message is telling you -- it's not lying to you.
Comment for this answer: @wolfiejoe: It makes a big difference it you call the super constructor and pass in the String parameter as I've already recommended.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/30/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><swing><types><jframe><undefined>', 'Title': 'The method createGUI() is undefined for type JFrame', 'ViewCount': 1001, 'id': 29958211, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}