0

不知道为什么我的小程序没有显示。它显示在eclipse中,但是当我编译一个jar并从windows cmd运行它时,它不显示。输出System.out.println在整个代码中都有效,并出现在命令行上。我做错了什么,是编译可执行jar还是带有清单的东西?我需要在 jar 或 windows 中获得某种权限吗?我应该使用诸如属性文件而不是参数之类的东西吗?第一次使用 jars、applet、jgraph。我使用的命令是 java -jar test.jar c:/test.xml

main.java

public static void main(String[] args) {

        File file = new File(args[0]);

        GraphMain(file).init();

        System.out.println("The file path is " + file);

    }

GraphMain.java

public class GraphMain extends JApplet {


    //Stuff

    File DRUGBANK_DIR;

    public GraphMain(File DrugBankFile)
    {
        DRUGBANK_DIR = DrugBankFile;
    }

    public  void init()
    {

        // a lot stuff



            // create a JGraphT graph
           UndirectedGraph<String, DefaultEdge> g = 
                new ListenableUndirectedGraph<String, DefaultEdge>( DefaultEdge.class );

            // create a visualization using JGraph, via an adapter
            m_jgAdapter = new JGraphModelAdapter<String, DefaultEdge>( g );

            JGraph jgraph = new JGraph( m_jgAdapter );

            adjustDisplaySettings( jgraph );
            getContentPane(  ).add( jgraph );
            resize( DEFAULT_SIZE );



            //stuff

          positionVertices(vertices0, vertices1);
        }

        private void positionVertices(List<String> vertices0, List<String> vertices1)
        {
            int dy0 = DEFAULT_SIZE.height / (vertices0.size() + 5);
            int y0 = dy0;
            for (String v0 : vertices0)
            {
                positionVertexAt(v0, 100, y0);
                y0+=dy0;
            }
            int dy1 = DEFAULT_SIZE.height / (vertices1.size() + 2);
            int y1 = dy1;
            for (String v1 : vertices1)
            {
                positionVertexAt(v1, 700, y1);
                y1+=dy1;
            }
        }



        private void adjustDisplaySettings( JGraph jg ) {
            jg.setPreferredSize( DEFAULT_SIZE );

            Color  c        = DEFAULT_BG_COLOR;
            String colorStr = null;

            try {
                colorStr = getParameter( "bgcolor" );
            }
             catch( Exception e ) {}

            if( colorStr != null ) {
                c = Color.decode( colorStr );
            }

            jg.setBackground( c );
        }


        private void positionVertexAt( Object vertex, int x, int y ) {
            DefaultGraphCell cell = m_jgAdapter.getVertexCell( vertex );
            Map<?, ?>              attr = cell.getAttributes(  );
            Rectangle2D        b    = GraphConstants.getBounds( attr );

            GraphConstants.setBounds( attr, new Rectangle2D.Double( x, y, b.getWidth(), b.getHeight() ) );

            Map<DefaultGraphCell, Map<?, ?>> cellAttr = new HashMap<DefaultGraphCell, Map<?, ?>>(  );
            cellAttr.put( cell, attr );
            m_jgAdapter.edit( cellAttr, null, null, null);
        }


}
4

1 回答 1

0

在cmd中运行时,它不会显示,因为它不是在applet环境中。它没有任何东西可以显示。为了让它工作,你需要创建你自己的显示,也就是一个 JFrame。试试这个代码:

public static void main(String args[]){
    File file=.....;
    final GraphMain applet=new GraphMain(file);
    applet.init();
    JFrame f=new JFrame("Title goes here");
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            applet.stop();
            applet.destroy();
            System.exit(0);
        }
    });
    f.setLayout(new BorderLayout());
    f.add(applet, BorderLayoyt.CENTER);
    f.setSize(whatever size your applet is);
    applet.start();
    f.setVisible(true);
}

我不保证这段代码没有错误(我现在只是在浏览器中编写的),但您可以将其用作起点。

此外,因为您正在使用,File您需要签署您的 jar 以使其工作。请参阅此处的 Oracle 教程:http: //docs.oracle.com/javase/tutorial/deployment/jar/signindex.html

于 2014-03-29T23:45:34.373 回答