【问题标题】:Java jgraph applet cmd jar won't displayJava jgraph小程序cmd jar不会显示
【发布时间】:2014-03-30 07:45:34
【问题描述】:

不知道为什么我的小程序没有显示。它显示在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);
        }


}

【问题讨论】:

    标签: java jar applet jgraph


    【解决方案1】:

    在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

    【讨论】:

    • 谢谢!您的代码非常准确,没有拼写 BorderLayout。如果您有机会查看其他帖子,我在间距和尝试滚动方面遇到其他问题。 stackoverflow.com/questions/22739051/…
    猜你喜欢
    • 2016-05-28
    • 2018-02-16
    • 2023-04-01
    • 2012-08-04
    • 2011-08-11
    • 2012-02-17
    • 2012-12-30
    • 2013-03-08
    相关资源
    最近更新 更多