当前位置: 技术问答>java相关
java小问题,让我寝食难安,特向高人讨教!!!
来源: 互联网 发布时间:2015-05-13
本文导语: //=================== BallPanel.java ======================= import java.awt.*; import javax.swing.*; public class BallPanel extends JPanel { private int dia = 20; public void paintComponent(Graphic...
//=================== BallPanel.java =======================
import java.awt.*;
import javax.swing.*;
public class BallPanel extends JPanel
{
private int dia = 20;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillOval(10, 10, dia, dia);
}
public void setDiameter(int d)
{
dia = (d>=0 ? d : 20);
repaint();
}
public Dimension getPreferredSize()
{
return new Dimension(200, 200);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
}
------------------------------------------------------------------
//====================== MySlider.java =====================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import MyWindowListener;
import BallPanel;
public class MySlider extends JFrame
implements ChangeListener
{
private JSlider slider;
private BallPanel ballPanel;
public MySlider()
{
super("MySlider.java: JSlider测试");
ballPanel = new BallPanel();
ballPanel.setBackground(Color.yellow);
slider = new JSlider(SwingConstants.HORIZONTAL,0,200,10);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.addChangeListener(this);
Container c = getContentPane();
c.add(ballPanel, BorderLayout.CENTER);
c.add(slider, BorderLayout.SOUTH);
setSize(220, 270);
show();
}
public void stateChanged(ChangeEvent e)
{
ballPanel.setDiameter(slider.getValue());
}
public static void main(String args[])
{
MySlider app = new MySlider();
app.addWindowListener(new MyWindowListener());
}
}
------------------------------------------------
两个程序在同一目录下,
编译BallPanel.java通过
编译MySlider.java 报错:
MySlider.java:7: '.' expected
import BallPanel;
^
1 error
import java.awt.*;
import javax.swing.*;
public class BallPanel extends JPanel
{
private int dia = 20;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillOval(10, 10, dia, dia);
}
public void setDiameter(int d)
{
dia = (d>=0 ? d : 20);
repaint();
}
public Dimension getPreferredSize()
{
return new Dimension(200, 200);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
}
------------------------------------------------------------------
//====================== MySlider.java =====================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import MyWindowListener;
import BallPanel;
public class MySlider extends JFrame
implements ChangeListener
{
private JSlider slider;
private BallPanel ballPanel;
public MySlider()
{
super("MySlider.java: JSlider测试");
ballPanel = new BallPanel();
ballPanel.setBackground(Color.yellow);
slider = new JSlider(SwingConstants.HORIZONTAL,0,200,10);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.addChangeListener(this);
Container c = getContentPane();
c.add(ballPanel, BorderLayout.CENTER);
c.add(slider, BorderLayout.SOUTH);
setSize(220, 270);
show();
}
public void stateChanged(ChangeEvent e)
{
ballPanel.setDiameter(slider.getValue());
}
public static void main(String args[])
{
MySlider app = new MySlider();
app.addWindowListener(new MyWindowListener());
}
}
------------------------------------------------
两个程序在同一目录下,
编译BallPanel.java通过
编译MySlider.java 报错:
MySlider.java:7: '.' expected
import BallPanel;
^
1 error
|
import MyWindowListener;
MyWindowListener是你自己做的CLASS吧?这个编译通过了么?
MyWindowListener是你自己做的CLASS吧?这个编译通过了么?
|
有没有包结构?
如果这两个类在同一个目录下,不需要import ,把import BallPanel; 这句去掉!
如果这两个类在同一个目录下,不需要import ,把import BallPanel; 这句去掉!
|
我这样写:
//test.java
public class test
{
public int get()
{
return 4;
}
}
//vb.java
class tt
{
public static void main(String args[])
{
test aa=new test();
}
}
两个文件在同一目录下,先编译test.java得到test.class,再编译vb.java时出错!为什么?和小舟的不是一样吗?
//test.java
public class test
{
public int get()
{
return 4;
}
}
//vb.java
class tt
{
public static void main(String args[])
{
test aa=new test();
}
}
两个文件在同一目录下,先编译test.java得到test.class,再编译vb.java时出错!为什么?和小舟的不是一样吗?
|
这个问题是这样的,java的编译器默认同一目录下的类为在同一个包中,所以你不用再import,而对于jszj(一麦偶) ,我想知道你的错误提示是什么?