在eclipse中如何导入自定义包,貌似直接用import导入不行哦。那位指点一下
在同一工作区中,MainFrame类在包greedySnake中,testJDiag类在包test中,现在在testDiag中
要使用类MainFrame,但是用import java.greedySnake.*;或是用ctrl+shift+o行不通,那位大侠指点一下
在同一工作区中,MainFrame类在包greedySnake中,testJDiag类在包test中,现在在testDiag中
要使用类MainFrame,但是用import java.greedySnake.*;或是用ctrl+shift+o行不通,那位大侠指点一下
在testJDiag.java中,package MainFrame完整包名.MainFrame;就行了。
MainFrame中声明package greedySnake;
那么MainFrame的完整包名不是greedySnake吗
但是在testJDiag中import greedySnake.MainFrame;不能通过
一般导入就这样啊。不能通过?错误信息是什么啊??
//MainFrame.java
package greedySnake;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame{
boolean isPaused=true;/*isPause与示例的状态有关,不是常量,初始化时,
游戏处于暂停状态,没有自动运行*/
static final int ROWS=30;//常量与实例的状态无关定义为static类型
static final int COLS=50;
Container contentPane=this.getContentPane();
JToolBar jToolBar1=new JToolBar();
JButton jButton1=new JButton("开始");
JButton jButton2=new JButton("继续");
JButton jButton3=new JButton("退出");
JButton jButton4=new JButton("帮助");
JLabel jLabel1=new JLabel("得分:");
JLabel jLabel2=new JLabel("0");
JLabel jLabel3=new JLabel("穿身:");
JLabel jLabel4=new JLabel("0");
JLabel jLabel5=new JLabel("穿墙:");
JLabel jLabel6=new JLabel("0");
JLabel jLabel7=new JLabel("等级:");
ButtonGroup buttonGroup1=new ButtonGroup();
JRadioButton jRadioButton1=new JRadioButton("初级",true);/*
重要的构造方法,JRadioButton(String text,boolean selected)*/
JRadioButton jRadioButton2=new JRadioButton("中级");
JRadioButton jRadioButton3=new JRadioButton("高级");
JPanel jPanel1=new JPanel();
JPanel jPanel2=new JPanel();
JButton[][] playBlocks=new JButton[ROWS][COLS];
public MainFrame(){
super("贪吃蛇游戏");
jbInit();
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=this.getSize();
if(frameSize.height>screenSize.height){
frameSize.height=screenSize.height;
}
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
/**使窗体居中*/
this.setLocation((screenSize.width-frameSize.width)/2,
(screenSize.height-frameSize.height)/2);
}
private void jbInit(){
jToolBar1.setLayout(new FlowLayout());
jToolBar1.add(jButton1);
jToolBar1.add(jButton2);
jToolBar1.add(jButton3);
jToolBar1.add(jButton4);
jPanel1.add(jToolBar1);
jPanel1.add(jLabel1);
jPanel1.add(jLabel2);
jPanel1.add(jLabel3);
jPanel1.add(jLabel4);
jPanel1.add(jLabel5);
jPanel1.add(jLabel6);
jPanel1.add(jLabel7);
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
jPanel1.add(jRadioButton1);
jPanel1.add(jRadioButton2);
jPanel1.add(jRadioButton3);
jPanel1.setLayout(new FlowLayout());
//jPanel1.setBackground(Color.GREEN);
jPanel2.setLayout(new GridLayout(ROWS,COLS,0,0));
jPanel2.setBackground(Color.WHITE);
for(int i=0;i<ROWS;i++){
for(int j=0;j<COLS;j++){
playBlocks[i][j]=new JButton();
playBlocks[i][j].setBackground(Color.WHITE);
jPanel2.add(playBlocks[i][j]);
playBlocks[i][j].setVisible(true);
}
}
SnakeBody snakeBody=new SnakeBody();//MainFrame中定义了蛇身类
for(int i=0;i<snakeBody.length;i++){
//调试代码playBlocks[ROWS/2][COLS/2].setBackground(Color.GREEN);
playBlocks[snakeBody.rows[i]][snakeBody.cols[i]].setBackground(Color.GREEN);
}
contentPane.setLayout(null);
contentPane.add(jPanel1);
contentPane.add(jPanel2);
jPanel1.setBounds(0, 0, 400, 100);
jPanel2.setBounds(0,100,400,300);
this.setSize(450,450);
this.setResizable(false);
this.setVisible(true);
//为各个对象添加事件监听器
jButton3.addActionListener(new JButton3ActionEvent());
}
}
class SnakeBody{
static final int MAXLENGTH=50;
static final int LEFT=0;
static final int RIGHT=1;
static final int UP=2;
static final int DOWN=3;
int direction=LEFT;
int length;
int [] rows=new int [MAXLENGTH];
int [] cols=new int [MAXLENGTH];
public SnakeBody(){
jbInit();
}
private void jbInit(){
length=3;
for(int i=0;i<length;i++){
rows[i]=MainFrame.ROWS/2;
cols[i]=MainFrame.COLS/2-i;
}
}
}
class SnakeThread extends Thread{
SnakeBody snakeBody;
MainFrame mainFrame;
public SnakeThread(SnakeBody sb,MainFrame mf){
snakeBody=sb;
mainFrame=mf;
}
public void run(){
}
private void move(){
switch (snakeBody.direction){
case SnakeBody.LEFT:{
snakeBody.cols[0]--;
}
}
}
}
//TestJDiag.java
package test;
import greedySnake.MainFrame;//错误信息:the import greedySnake cannot be resolved
import java.awt.Color;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JPanel;
public class TestJDialog extends JDialog{
MainFrame mf;//错误信息:MainFrame cannot be resolved to a type
JPanel jPanel1;
public TestJDialog(Frame frameOwner){
super(frameOwner,"Dialogue",true);
jbInit();
}
private void jbInit(){
jPanel1=new JPanel();
jPanel1.setBackground(Color.GREEN);
this.getContentPane().add(jPanel1);
}
public static void main(String[] args){
}
}
帮我试一下啊,谢谢啦
//MainFrame.java
package greedySnake;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame{
boolean isPaused=true;/*isPause与示例的状态有关,不是常量,初始化时,
游戏处于暂停状态,没有自动运行*/
static final int ROWS=30;//常量与实例的状态无关定义为static类型
static final int COLS=50;
Container contentPane=this.getContentPane();
JToolBar jToolBar1=new JToolBar();
JButton jButton1=new JButton("开始");
JButton jButton2=new JButton("继续");
JButton jButton3=new JButton("退出");
JButton jButton4=new JButton("帮助");
JLabel jLabel1=new JLabel("得分:");
JLabel jLabel2=new JLabel("0");
JLabel jLabel3=new JLabel("穿身:");
JLabel jLabel4=new JLabel("0");
JLabel jLabel5=new JLabel("穿墙:");
JLabel jLabel6=new JLabel("0");
JLabel jLabel7=new JLabel("等级:");
ButtonGroup buttonGroup1=new ButtonGroup();
JRadioButton jRadioButton1=new JRadioButton("初级",true);/*
重要的构造方法,JRadioButton(String text,boolean selected)*/
JRadioButton jRadioButton2=new JRadioButton("中级");
JRadioButton jRadioButton3=new JRadioButton("高级");
JPanel jPanel1=new JPanel();
JPanel jPanel2=new JPanel();
JButton[][] playBlocks=new JButton[ROWS][COLS];
public MainFrame(){
super("贪吃蛇游戏");
jbInit();
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=this.getSize();
if(frameSize.height>screenSize.height){
frameSize.height=screenSize.height;
}
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
/**使窗体居中*/
this.setLocation((screenSize.width-frameSize.width)/2,
(screenSize.height-frameSize.height)/2);
}
private void jbInit(){
jToolBar1.setLayout(new FlowLayout());
jToolBar1.add(jButton1);
jToolBar1.add(jButton2);
jToolBar1.add(jButton3);
jToolBar1.add(jButton4);
jPanel1.add(jToolBar1);
jPanel1.add(jLabel1);
jPanel1.add(jLabel2);
jPanel1.add(jLabel3);
jPanel1.add(jLabel4);
jPanel1.add(jLabel5);
jPanel1.add(jLabel6);
jPanel1.add(jLabel7);
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
jPanel1.add(jRadioButton1);
jPanel1.add(jRadioButton2);
jPanel1.add(jRadioButton3);
jPanel1.setLayout(new FlowLayout());
//jPanel1.setBackground(Color.GREEN);
jPanel2.setLayout(new GridLayout(ROWS,COLS,0,0));
jPanel2.setBackground(Color.WHITE);
for(int i=0;i<ROWS;i++){
for(int j=0;j<COLS;j++){
playBlocks[i][j]=new JButton();
playBlocks[i][j].setBackground(Color.WHITE);
jPanel2.add(playBlocks[i][j]);
playBlocks[i][j].setVisible(true);
}
}
SnakeBody snakeBody=new SnakeBody();//MainFrame中定义了蛇身类
for(int i=0;i<snakeBody.length;i++){
//调试代码playBlocks[ROWS/2][COLS/2].setBackground(Color.GREEN);
playBlocks[snakeBody.rows[i]][snakeBody.cols[i]].setBackground(Color.GREEN);
}
contentPane.setLayout(null);
contentPane.add(jPanel1);
contentPane.add(jPanel2);
jPanel1.setBounds(0, 0, 400, 100);
jPanel2.setBounds(0,100,400,300);
this.setSize(450,450);
this.setResizable(false);
this.setVisible(true);
//为各个对象添加事件监听器
jButton3.addActionListener(new JButton3ActionEvent());
}
}
class SnakeBody{
static final int MAXLENGTH=50;
static final int LEFT=0;
static final int RIGHT=1;
static final int UP=2;
static final int DOWN=3;
int direction=LEFT;
int length;
int [] rows=new int [MAXLENGTH];
int [] cols=new int [MAXLENGTH];
public SnakeBody(){
jbInit();
}
private void jbInit(){
length=3;
for(int i=0;i<length;i++){
rows[i]=MainFrame.ROWS/2;
cols[i]=MainFrame.COLS/2-i;
}
}
}
class SnakeThread extends Thread{
SnakeBody snakeBody;
MainFrame mainFrame;
public SnakeThread(SnakeBody sb,MainFrame mf){
snakeBody=sb;
mainFrame=mf;
}
public void run(){
}
private void move(){
switch (snakeBody.direction){
case SnakeBody.LEFT:{
snakeBody.cols[0]--;
}
}
}
}
//TestJDiag.java
package test;
import greedySnake.MainFrame;//错误信息:the import greedySnake cannot be resolved
import java.awt.Color;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JPanel;
public class TestJDialog extends JDialog{
MainFrame mf;//错误信息:MainFrame cannot be resolved to a type
JPanel jPanel1;
public TestJDialog(Frame frameOwner){
super(frameOwner,"Dialogue",true);
jbInit();
}
private void jbInit(){
jPanel1=new JPanel();
jPanel1.setBackground(Color.GREEN);
this.getContentPane().add(jPanel1);
}
public static void main(String[] args){
}
}
帮我试一下啊,谢谢啦
顺便
import greedySnake.*;