过去半小时我一直在为这个错误烦恼。 JPanel cannot be resolved to a type
但是,我已经导入了javax.swing.JPanel;
,我看不出为什么会造成这种情况的另一个原因。我在好的或其他问题上找不到任何帮助。然而,问题的有趣之处在于,所有变量都来自swing包。我已经尝试重新添加JRE System Library
(我正在使用eclipse),但这没有任何帮助。这是我正在使用的一些代码;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
public class Workshop {
private JFrame window = new JFrame("Meejo WorkShop");
private JLayeredPane lpane = new JLayeredPane();
private JPanel panelBg = new JPanel();
private JPanel panelGame = new JPanel();
private JPanel panelProp = new JPanel();
private JPanel panelWork = new JPanel();
是问题的一个例子。所有变量都会错误[Var Type] Cannot be resolved to a type
超过rivate
和= ne
任何想法,为什么?如果是这样,我该如何解决?感谢。
编辑:完整的代码是;
package mj.workshop;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
public class Workshop {
private final JFrame window = new JFrame("Meejo WorkShop");
private final JLayeredPane lpane = new JLayeredPane();
private final JPanel panelBg = new JPanel();
private final JPanel panelGame = new JPanel();
private final JPanel panelProp = new JPanel();
private final JPanel panelWork = new JPanel();
private final JMenuBar windowMenu = new JMenuBar();
private final JMenu menuFile = new JMenu("File");
private final JMenu menuEdit = new JMenu("Edit");
private final JMenu menuHelp = new JMenu("Help");
// private JMenu menuTest = new JMenu("Test");
private final JMenuItem menuFileNew = new JMenuItem("New");
private final JMenuItem menuFileSave = new JMenuItem("Save");
private final JMenuItem menuFileSaveAs = new JMenuItem("Save As");
private final JMenuItem menuFileExit = new JMenuItem("Exit");
private final JMenuItem menuEditOptions = new JMenuItem("Options");
// private JMenuItem menuEditRedo = new JMenuItem("Redo");
// private JMenuItem menuEditUndo = new JMenuItem("Undo");
// private JMenuItem menuEditCopy = new JMenuItem("Copy");
// private JMenuItem menuEditCut = new JMenuItem("Cut");
// private JMenuItem menuEditPaste = new JMenuItem("Paste");
// private JMenuItem menuEditInsert_Comment = new
// JMenuItem("Insert Comment");
private final JMenuItem menuHelpSupport = new JMenuItem("Support");
private final JMenuItem menuHelpAbout = new JMenuItem("About");
private final JMenuItem menuHelpTips = new JMenuItem("Tips");
private final JPopupMenu menuHelpTipsPopUp = new JPopupMenu("Tips");
// private JMenuItem menuTestStart = new JMenuItem("Start");
// private JMenuItem menuTestPause = new JMenuItem("Pause");
// private JMenuItem menuTestStop = new JMenuItem("Stop");
int xSize, ySize;
public Workshop() {
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit();
xSize = ((int) tk.getScreenSize().getWidth());
ySize = ((int) tk.getScreenSize().getHeight());
window.setSize(xSize, ySize);
xSize = window.getWidth();
ySize = window.getHeight();
Image icon = Toolkit.getDefaultToolkit().getImage("images/icon.png");
window.setIconImage(icon);
window.setBackground(Color.WHITE);
window.setMinimumSize(new Dimension(600, 450));
setUpMenu();
Thread mainThread = new Thread();
mainThread.start();
window.setPreferredSize(new Dimension(xSize, ySize));
window.setLayout(new BorderLayout());
window.add(lpane, BorderLayout.CENTER);
lpane.setBounds(0, 0, 600, 400);
panelBg.setBackground(Color.WHITE);
panelBg.setBounds(0, 0, xSize, ySize);
panelBg.setOpaque(true);
panelProp.setBackground(new Color(235, 235, 235));
panelProp.setBounds(0, (int) (Math.round(ySize * 0.20)),
(int) (Math.round(xSize * 0.175)),
(int) (Math.round(ySize * 0.80)));
panelProp.setOpaque(true);
panelWork.setBackground(new Color(235, 235, 235));
panelWork.setBounds((int) (Math.round(xSize * 0.825)),
(int) (Math.round(ySize * 0.20)),
(int) (Math.round(xSize * 0.175)),
(int) (Math.round(ySize * 0.80)));
panelWork.setOpaque(true);
panelGame.setBackground(Color.BLUE);
panelGame.setBounds((int) (Math.round(xSize * 0.175)),
(int) (Math.round(ySize * 0.20)),
(int) (Math.round(xSize * 0.65)),
(int) (Math.round(ySize * 0.80)));
panelGame.setOpaque(true);
lpane.add(panelBg, new Integer(0), 0);
lpane.add(panelGame, new Integer(1), 0);
lpane.add(panelProp, new Integer(1), 0);
lpane.add(panelWork, new Integer(1), 0);
window.pack();
window.setVisible(true);
while (true) {
windowrefresh();
}
}
public void windowrefresh() {
try {
Thread.sleep(1000);
xSize = window.getWidth();
ySize = window.getHeight();
panelProp.setBounds(0, (int) (Math.round(ySize * 0.20)),
(int) (Math.round(xSize * 0.175)),
(int) (Math.round(ySize * 0.80)));
panelWork.setBounds((int) (Math.round(xSize * 0.825)),
(int) (Math.round(ySize * 0.20)),
(int) (Math.round(xSize * 0.175)),
(int) (Math.round(ySize * 0.80)));
panelGame.setBounds((int) (Math.round(xSize * 0.175)),
(int) (Math.round(ySize * 0.20)),
(int) (Math.round(xSize * 0.65)),
(int) (Math.round(ySize * 0.80)));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void setUpMenu() {
menuFileSave.setEnabled(false);
menuFileSaveAs.setEnabled(false);
menuFile.add(menuFileNew);
menuFile.add(menuFileSave);
menuFile.add(menuFileSaveAs);
menuFile.add(new JSeparator());
menuFile.add(menuFileExit);
menuEdit.add(menuEditOptions);
// menuEdit.add(menuEditRedo);
// menuEdit.add(menuEditUndo);
// menuEdit.add(menuEditCopy);
// menuEdit.add(menuEditCut);
// menuEdit.add(menuEditPaste);
// menuEdit.add(menuEditInsert_Comment);
menuHelp.add(menuHelpSupport);
menuHelp.add(menuHelpAbout);
menuHelp.add(new JSeparator());
menuHelp.add(menuHelpTips);
// windowTest.add(menuTestStart);
// windowTest.add(menuTestPause);
// windowTest.add(menuTestStop);
windowMenu.add(menuFile);
windowMenu.add(menuEdit);
windowMenu.add(menuHelp);
// windowMenu.add(menuTest);
windowMenu.setFont(new Font("Arial", Font.PLAIN, 12));
window.setJMenuBar(windowMenu);
menuHelpTips.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
if (evt.isPopupTrigger()) {
menuFileSave.setEnabled(true);
menuFileSaveAs.setEnabled(true);
}
}
@Override
public void mouseReleased(MouseEvent evt) {
if (evt.isPopupTrigger()) {
menuFileSave.setEnabled(true);
menuFileSaveAs.setEnabled(true);
}
}
});
}
}
答案 0 :(得分:2)
正如Brian的评论所说,这只是一个编码问题。更改为UTF-8会在重新执行所有导入后修复所有错误。