我正在尝试为我的JFrame添加一个标题,因此它显示为“CBallMaze Ball Maze Application”,但每当我在创建JFrame时输入它时,它会在标题中给出错误消息。
如果我不尝试添加标题并且使用这样的公共静态void表示
,则此代码运行正常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));
}
}
答案 0 :(得分:5)
使用此JFrame CBallMaze = new JFrame("Title");
,您可以创建一个JFrame变量并为其分配一个JFrame对象,然后期望它具有CBallMaze行为,这是不会发生的。相反,您需要创建一个CBallMaze对象并将其分配给CBallMaze变量。
所以改变这个:
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);
侧面建议:
setPreferredSize(...)
调用,而是使用布局管理器和智能创建组件(即,为JTextFields提供列属性)来帮助创建一个简单而令人愉悦的GUI。super(...)
构造函数并将该String参数传递给。