日期:2014-05-20  浏览次数:20703 次

graphics问题,求高手啊,弄了好久还是不会。如果能详细点,就好,谢谢!
import java.awt.*;
import javax.swing.*;;
public class text extends JFrame{
private JLabel panelNorth;
private JPanel panelSouth;
private JButton but_start;
private JButton but_out;
private JPanel pan;
private BorderLayout lay;
public text()
{
setTitle("面板");
lay=new BorderLayout();
setLayout(lay);
lay.setVgap(15);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(400,100,500,550);
init();
setVisible(true);
}

public void repaintComponent(Graphics g)
{
super.paintComponents(g); 
g.setColor(Color.black);
g.drawLine(50,50,100,100);
}

void init()
{
panelNorth=new JLabel("画线!");
panelNorth.setFont(new Font("楷体",30,24));
panelNorth.setForeground(Color.blue);
this.getContentPane().add(panelNorth,BorderLayout.NORTH);


panelSouth=new JPanel();
but_start=new JButton("开始");
but_out=new JButton("退出");
but_start.setFont(new Font("楷体",10,16));
but_out.setFont(new Font("楷体",10,16));
panelSouth.add(but_start);
panelSouth.add(but_out);
this.getContentPane().add(panelSouth,BorderLayout.SOUTH);

pan=new JPanel();
pan.repaint();
this.getContentPane().add(pan,BorderLayout.CENTER);
}
public static void main(String args[])
{
new text();
}
}


不知道怎么使用graphics,画线根本画不了。。。

------解决方案--------------------
改了下,自己看一下区别吧
Java code
import java.awt.*;
import javax.swing.*;

public class text extends JFrame {

    private JLabel panelNorth;
    private JPanel panelSouth;
    private JButton but_start;
    private JButton but_out;
    private JPanel pan;
    private BorderLayout lay;

    public text() {
        setTitle("面板");
        lay = new BorderLayout();
        setLayout(lay);
        lay.setVgap(15);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(400, 100, 500, 550);
        init();
        setVisible(true);
    }

    void init() {
        panelNorth = new JLabel("画线!");
        panelNorth.setFont(new Font("楷体", 30, 24));
        panelNorth.setForeground(Color.blue);
        this.getContentPane().add(panelNorth, BorderLayout.NORTH);

        panelSouth = new JPanel();
        but_start = new JButton("开始");
        but_out = new JButton("退出");
        but_start.setFont(new Font("楷体", 10, 16));
        but_out.setFont(new Font("楷体", 10, 16));
        panelSouth.add(but_start);
        panelSouth.add(but_out);
        this.getContentPane().add(panelSouth, BorderLayout.SOUTH);

        pan = new JPanel() {

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.black);
                g.drawLine(50, 50, 100, 100);
            }
        };
        this.getContentPane().add(pan, BorderLayout.CENTER);
    }

    public static void main(String args[]) {
        new text();
    }
}