Java JFrame类型的方法createGUI()未定义
我试图在我的JFrame中添加一个标题,这样它就会显示“CBallMaze Ball Maze Application”,但每当我在创建JFrame时键入该标题时,它就会在标题中显示错误消息 如果我不尝试添加一个标题,并且让publicstaticvoid这样表述,那么这段代码运行得很好Java JFrame类型的方法createGUI()未定义,java,swing,types,jframe,undefined,Java,Swing,Types,Jframe,Undefined,我试图在我的JFrame中添加一个标题,这样它就会显示“CBallMaze Ball Maze Application”,但每当我在创建JFrame时键入该标题时,它就会在标题中显示错误消息 如果我不尝试添加一个标题,并且让publicstaticvoid这样表述,那么这段代码运行得很好 CBallMaze JFrame = new CBallMaze(); JFrame.setSize(775, 650); JFrame.createGUI(); JFrame.setVisible(true)
CBallMaze JFrame = new CBallMaze();
JFrame.setSize(775, 650);
JFrame.createGUI();
JFrame.setVisible(true);
当前代码=
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(">");
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));
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类CBallMaze扩展JFrame实现ActionListener
{
//下面是我声明我在整个程序中使用的所有不同对象的地方
私有按钮按钮右、按钮左、按钮上、按钮下、按钮左、按钮左、按钮左、按钮右、按钮右、按钮右、按钮中心、选项一、选项二、选项树、选项一、选项一、选项一、选项一、场景动作、场景运行、场景重置;
private JPanel Panel Centre、Panel Right、Panel Bottom、Button Panel、compassPanel、Options Panel、selectionPanel、panelAct、panelRun、panelReset;
私人JTextField optionTF、squareTF、directionTF;
专用JLabel选项,方形,方向;
私有图标iconAct、iconRun、ICONRET;
公共静态void main(字符串[]args)
{
JFrame CBallMaze=新JFrame(“标题”);
CBallMaze.setSize(775650);
CBallMaze.createGUI();
CBallMaze.setVisible(true);
}
私有void createGUI()
{
setDefaultCloseOperation(关闭时退出);
容器窗口=getContentPane();
setLayout(新的BorderLayout());
//面板
panelCentre=新的JPanel();
面板中心。设置首选尺寸(新尺寸(625450));
镶板中心。立根背景(颜色。红色);
窗口。添加(面板中心);
panelCentre.setLayout(新网格布局(21,30));
panelRight=新的JPanel();
panelRight.setPreferredSize(新尺寸(180450));
面板右。背景(颜色。白色);
添加(面板右,边框布局。东);
选项面板=新的JPanel();
选项面板。设置首选尺寸(新尺寸(150100));
选件面板.立根背景(颜色.白色);
panelRight.add(选项Panel,BorderLayout.EAST);
buttonPanel=新的JPanel();
buttonPanel.setPreferredSize(新尺寸(175100));
按钮面板。挫折地面(颜色。白色);
panelRight.add(buttonPanel,BorderLayout.EAST);
selectionPanel=newjpanel();
selectionPanel.setPreferredSize(新尺寸(175150));
selectionPanel.setBackground(颜色:白色);
panelRight.add(selectionPanel,BorderLayout.EAST);
compassPanel=新JPanel();
compassPanel.setPreferredSize(新尺寸(175300));
圆规面板。背景(颜色。蓝色);
panelRight.add(compassPanel,BorderLayout.EAST);
panelBottom=新的JPanel();
面板底部。设置首选尺寸(新尺寸(875,50));
面板底部。立根地面(颜色。白色);
添加(panelBottom,BorderLayout.SOUTH);
panelAct=新的JPanel();
面板。设置首选尺寸(新尺寸(200,40));
镶板。背景(颜色。白色);
panelBottom.add(panelAct,BorderLayout.SOUTH);
panelRun=newjpanel();
panelRun.setPreferredSize(新尺寸(200,40));
镶板镶边.立根(颜色.白色);
panelBottom.add(panelRun,BorderLayout.SOUTH);
panelReset=新的JPanel();
panelReset.setPreferredSize(新尺寸(200,40));
面板复位。立根地面(颜色:白色);
panelBottom.add(panelReset,BorderLayout.SOUTH);
//显示
选项=新的JLabel(“选项:”);
选项面板。添加(选项,边框布局。行_开始);
option.setEnabled(true);
选项。设置前景(颜色为黑色);
选项。设置水平对齐(JLabel.左);
optionTF=新的JTextField(“”);
选项面板。添加(选项TF,边框布局。线条结束);
optionTF.setEnabled(真);
optionTF.setPreferredSize(新尺寸(50,25));
optionTF.setHorizontalAlignment(JTextField.CENTER);
square=新的JLabel(“square:”);
选项面板。添加(正方形,边框布局。线条开始);
square.setEnabled(真);
正方形。设置前景(颜色。黑色);
方形。设置水平对齐(JLabel左);
squareTF=新的JTextField(“”);
选项面板。添加(squareTF,BorderLayout.LINE_END);
squareTF.setEnabled(真);
squareTF.setPreferredSize(新尺寸(50,25));
squareTF.setHorizontalAlignment(JTextField.CENTER);
方向=新的JLabel(“方向:”);
选项面板。添加(方向、边界布局、直线和起点);
方向。setEnabled(真);
方向。设置前景(颜色。黑色);
方向设置水平对齐(JLabel.左);
directionTF=新的JTextField(“”);
选项面板。添加(方向TF,边界布局。线_结束);
directionTF.setEnabled(真);
方向TF.setPreferredSize(新尺寸(50,25));
方向tf.setHorizontalAlignment(JTextField.CENTER);
//钮扣
buttonTL=新的JButton(“”);
buttonPanel.add(buttonTL);
按钮L.设置首选尺寸(新尺寸(45,25));
按钮L.setEnabled(错误);
buttonUp=新的JButton(“^”);
buttonPanel.add(buttonUp);
按钮设置首选尺寸(新尺寸(45,25));
buttonUp.addActionListener(此);
buttonTR=新的JButton(“”);
buttonPanel.add(buttonTR);
按钮中心设置首选尺寸(新尺寸(45,25));
按钮ntr.setEnabled(false);
buttonLeft=新的JButton(“”);
按钮面板。添加(按钮右侧);
按钮右侧。设置首选尺寸(新尺寸(45,25));
按钮右。addActionListener(此);
buttonBL=新的JButton(“”);
按钮面板。添加(按钮L);
按钮BL.设置首选尺寸(新尺寸(45,25));
按钮BL.setEnabled(错误);
按钮向下=新按钮(“v”);
buttonPanel.add(buttonDown);
设置首选尺寸(新尺寸(45,25));
addActionListener(这个);
buttonBR=新的JButton(“”);
按钮面板。添加(按钮br);
按钮设置首选尺寸(新尺寸(45,25));
按钮br.setEnabled(假);
optionOne=新的按钮(“选项一”);
selectionPanel.add(选项一);
optionOne.setPreferredSize(新尺寸(125,25));
optionTwo=新的JButton(“选项二”);
selectionPanel.add(选项二)
JFrame CBallMaze = new JFrame("Title");
CBallMaze.setSize(775, 650);
CBallMaze.createGUI();
CBallMaze.setVisible(true);
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);