用jgraph画了一些东西,example里面最后的步骤是这样:
        // Show in Frame
        JFrame frame = new JFrame();
        frame.getContentPane().add(new JScrollPane(graph));
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);但是我想要自己定义一个jframe,并且在其中加入按钮之类。姑且这样写:
class mainFrame extends JFrame{
    public void main(){
        mainFrame mf = new mainFrame();
        mf.drawContent();
        mf.pack();
        mf.setVisible(true);
    }    private void drawContent(){
        GraphModel model = new DefaultGraphModel();
        JGraph graph = new JGraph(model);
        //用jgraph画好        this.getContentPane().add(new JScrollPane(graph));
    }当然还有netbean自动生成的一些代码(现在还没有用到按钮,只是先画出界面),没有列出来。请大家帮忙看看,为什么这样运行之后什么都没有?jgraph绘图过程是没问题的,是从example里面来的。谢谢!

解决方案 »

  1.   

    validate();  //组件改动后,调用些方法,以便内容正确显示
    加这个方法试试
      

  2.   

    始终没有解决,只有调用
    JFrame.getContentPane().add(graph)才能在JFrame中空白的地方(即什么都没创建)显示出图像来
      

  3.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class SimpleGui3C implements ActionListener {
        JFrame frame;
        public static void main(String[] args) {
         SimpleGui3C gui = new SimpleGui3C();
         gui.go();
        }
        public void go() {
         frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
         JButton button = new JButton("chang colors");
         button.addActionListener(this);
        
         MyDrawPanel drawPanel = new MyDrawPanel();
        
         frame.getContentPane().add(BorderLayout.SOUTH,button);
         frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
         frame.setSize(300,300);
         frame.setVisible(true);
        }
    public void actionPerformed(ActionEvent arg0) {
    // TODO 自动生成方法存根
    frame.repaint();
    }}class MyDrawPanel extends JPanel {
    public void paintComponent(Graphics g) {

    g.fillRect(0,0, this.getWidth(), this.getHeight());

    int red = (int) (Math.random()*255);
    int green = (int) (Math.random()*255);
    int blue = (int) (Math.random()*255);

    Color randomColor = new Color(red,green,blue);
    g.setColor(randomColor);
    g.fillOval(70, 70, 100, 100);
    }
    }
    换个思维方式吧