楼主
功能:画出两点之间的距离,并求出距离
问题:调用repaint() 和 paintCompontent()之间搞不清楚,线段画不出
代码:
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;import javax.swing.JLabel;
 
import javax.swing.JTextField; 
public class LineLength extends javax.swing.JFrame  
{
    private Point xPoint,yPoint;    
    private JLabel label = new JLabel("Length");
    private JTextField text = new JTextField();
     
    public LineLength()
    {
        this.createUserInterface();    
    }
    
    private void createUserInterface()
    {
        this.setTitle("Painter");
        this.setSize(500,500);
        this.setVisible(true);
        
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);
        
        label.setBounds(10,330,75,25);
        contentPane.add(label);
        
        text.setBounds(80,330,75,25);
        contentPane.add(text);
        
        
        
        this.addMouseListener(
                new MouseListener() 
                {
                    public void mouseClicked(MouseEvent event)
                    {
                    }
                    
                    public void mouseExited(MouseEvent evt)
                    {
                    }
                    
                    public void mouseEntered(MouseEvent evt)
                    {
                        
                    }
                    public void mousePressed(MouseEvent evt)
                    {
                        DrawPanelMousePressed(evt);
                    }
                    
                    public void mouseReleased(MouseEvent evt)
                    {
                        DrawPanelMouseReleased(evt);
                    }
                    
                }
        );    }
    public void DrawPanelMousePressed(MouseEvent evt)
    {
        this.text.setText("");
        this.xPoint = evt.getPoint();
    }
    
    public void DrawPanelMouseReleased(MouseEvent evt)
    {
        this.yPoint = evt.getPoint();
        this.text.setText(this.lineLength());
        
        repaint();
        //问题:调用repaint()
    }
    
    public String lineLength()
    {
        double xDistance = xPoint.x - yPoint.x;
        double yDistance = xPoint.y - yPoint.y;
        double length = Math.sqrt(xDistance * xDistance + yDistance * yDistance);
        DecimalFormat value = new DecimalFormat("0.00");
        return value.format(length);
    }
    
    public void paintComponent(Graphics g)
    {
        if(xPoint != null && yPoint != null)
        {
            g.drawLine(xPoint.x, xPoint.y, yPoint.x, yPoint.y); 
        }             
    }
 
    public static void main(String[] args)
    {
        LineLength wall = new LineLength();
        wall.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    }
}

解决方案 »

  1.   

    JFrame本身不画图,而是它里面的frame.getContentPane();
    建议写一个MyPanel类继承JPanel,并加上你写的public void paintComponent(Graphics g)方法来画图。
    将MyPanel放到JFrame中去
    frame.getContentPane.add(myPanel);
      

  2.   

    这个样子的话,我就不能把计算出的距离放在JTextField里拉,
      

  3.   

    怎么不可以啊,也可以啊,你可以把JTextField的引用放在成员变量里啊,这样里面的任何方法都可以访问它了
      

  4.   

    我在继承类里面写了一个
    private JTextField text;
    并设置了它的setText()属性,来获得外部的JTextField 对象
    但为什么在面板panel 上会画出这个JTextField ??????怎么解决???
    这个paintComponent(Graphics g)到底是个什么东东,小弟一知半解向各位大大请教!!