当前位置: 代码迷 >> Java相关 >> 在不同文件下的两个类,如何用其中一个调用另一个???
  详细解决方案

在不同文件下的两个类,如何用其中一个调用另一个???

热度:128   发布时间:2012-07-17 09:23:11.0
在不同文件下的两个类,如何用其中一个调用另一个???
其中一个类:
import javax.swing.*;
import java.PushCounterPanel;

public class PushCounter
{
public static void main(String []args)
{
  JFrame frame=new JFrame("Push Counter");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  frame.getContentPane().add(new PushCounterPanel());
  frame.pack();
  frame.setVisible(true);
}
}


另一个类:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PushCounterPanel extends JPanel;
{
private int count;
private JButton push;
private JLabel label;

public PushCounterPanel()
{
  count=0;
  
  push=new JButton ("Push me");
  push.addActionListener(new ButtonListener());
  
  label=new JLabel ("Pushes:"+count);
  
  add(push);
  add(label);
  
  setPreferredSize(new Dimension (300,40));
  setBackground(Color.cyan);
}
private class ButtonListener implements ActionListener
{
  public void actionPerformed(ActionListener event)
  {
   count++;
   label.setText("Pushes:"+count);
  }
}
}
搜索更多相关的解决方案: void  private  public  frame  

----------------解决方案--------------------------------------------------------
导包!
----------------解决方案--------------------------------------------------------
引入包,调用时在上面这么写 : import 包名.*; 或者;import 包名.类名;

----------------解决方案--------------------------------------------------------
  相关解决方案