public class Draw extends JPanel{
 public Draw(){
  super();
  setLayout(null);
  JLabel label=new JLabel("label");
  label.setToolTipText("display");
  label.setBounds(400,100,400,20);
  add(label);
 }
 public paintComponent(Graphics g){  g.drawLine(30,30,40,20);
}}   在main()中:
    JFrame showFrame=new JFrame("");
    JScrollPane scrollPane=new JScrollPane();
    showFrame.getContentPane().add(scrollPane);
    Draw draw=new Draw();
    scrollPane.setViewportView(draw);  
    showFrame.setBounds(200,200,200,30);//该窗体的宽度小于draw中label的宽度
    showFrame.show();运行时出现2个问题:1 不显示滑动条;2 鼠标放置到label之后显示浮动的“diaplay”,鼠标移走后依旧显示。
求解决方案。

解决方案 »

  1.   

    public paintComponent(Graphics g){
    super.paintComponent(g);   //******//
    g.drawLine(30,30,40,20);
    }
    JFrame showFrame=new JFrame("");
    JScrollPane scrollPane=new JScrollPane();
    showFrame.getContentPane().add(scrollPane);
    Draw draw=new Draw();
    draw.setPreferredSize(new Dimension(900, 200));   //*****//
    scrollPane.setViewportView(draw);
    showFrame.setBounds(200,200,200,30);//该窗体的宽度小于draw中label的宽度
    showFrame.show();
      

  2.   

    谢谢你的帮忙!
    TooltipText可以显示了,但是滑动条没显示出来。我将Draw类构造函数中设置布局为setLayout(null),否则添加的标签无法定位。是不是因为布局管理器的原因而导致滑动条出不来?
      

  3.   

    问题解决如下:
    public class Draw extends JPanel{
     XYLayout xYLayout = new XYLayout();
     public Draw(){
      super();
      setLayout(null);-->setLayout(xYLayout);
      JLabel label=new JLabel("label");
      label.setToolTipText("display");
      label.setBounds(400,100,400,20);//删除
      add(label);-->add(label,new XYConstraints(400,100,400,20))
     }
     public paintComponent(Graphics g){  g.drawLine(30,30,40,20);
    }}   
    XYLayout是Jbuilder中特有的布局方式,大家以后在定位时可以考虑使用该布局
      

  4.   

    XYLayout依赖于Jbuilder,不知可有别的解决办法。
      

  5.   

    主要原因是你对draw没有设置大小,gtlang78() 的答案是可行的
    paintComponent需要调用super.paintComponent
    /**
     * @param args
     */
    public static void main(String[] args)
    {
    JFrame showFrame = new JFrame("Draw"); JScrollPane scrollPane = new JScrollPane();
    Draw draw = new Draw();
    draw.setPreferredSize(new Dimension(500, 200));
    scrollPane.setViewportView(draw);
    showFrame.getContentPane().add(scrollPane); showFrame.setBounds(200, 200, 200, 100);
    showFrame.setVisible(true);
    } public Draw()
    {
    super(null);
    JLabel label = new JLabel("label");
    label.setToolTipText("display");
    label.setBounds(400, 100, 400, 20);
    add(label);
    }  @Override
    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawLine(30, 30, 40, 20);
    }