为什么在某些计算机上不显示此JavaFX / Swing代码?

时间:2017-03-28 01:32:36

标签: java eclipse swing javafx jar

将以下代码编译为可执行JAR时,会产生不同的结果。

在我们测试的一半计算机上,必须在内容(swing节点)显示之前移动Java FX Window。在另一半,它显示内容而不操纵窗口。所有计算机都是基于Windows的(有些是W10,有些是W7)。

JRE(1.8.0_121)打包到程序中,批处理文件将JAR指向该JRE。

HistoryWindow(Task task, Scene scene) {
    history = task.getHistoryItems();
    String text = "";
    t = task;
    sc = scene;

    panel = new JPanel();
    panel.setSize(new Dimension(1600,850));
    panel.setLayout(new GridLayout(0,1,0,20));
    panel.addMouseListener(this);

    scrollPane = new JScrollPane(panel);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    Stage stage = new Stage();
    pane = new ScrollPane();
    SwingNode swingN = new SwingNode();


    initAndSetPanel();//Placeholder for the setup of the JPanel added to the scrollPane

    scrollPane.revalidate();

    createSwingContent(swingN);
    pane.setContent(swingN);
    stage.setScene(new Scene(pane, 400, 850));
    mystage = stage;
    stage.setTitle("History window");
    stage.initModality(Modality.WINDOW_MODAL);
    stage.initOwner(
            (scene.getWindow()));
    //stage.show();
    stage.showAndWait()
    /* Same result as above
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            stage.showAndWait();
        }
    });*/

}

createSwingNode(swingNode)方法使用SwingUtilities.invokeLater创建swing节点。

private void createSwingContent(final SwingNode swingNode) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                swingNode.setContent(scrollPane);
            }
        });
    }

代码总是运行到stage.showAndWait()(通过print语句验证),我不确定是什么导致了结果的差异。非常感谢帮助。

如果Jpanel设置导致了差异,这里是它的代码(可以在占位符方法中插入)。另外,不要因为我没写这个代码来判断我这个代码:)

for(HistoryItem his : history){
            Date day = his.getDate();
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy | hh:mm aa");
            text = sdf.format(day);

            text = text + " | " + his.getText();

            String addText = "";

            try{
                NameChange name = (NameChange)his;
                addText = "Changed name from " + name.getOldName() + " to " + name.getNewName();
            }catch(Exception e){}

            try{
                Comment com = (Comment)his;
                addText = com.getComment();
                commented = true;
            }catch(Exception e){}

            try{
                PriorityChange pri = (PriorityChange)his;
                String priOld = "";
                String priNew = "";

                switch(pri.getOldStatus()) {
                case 0: priOld = "inactive";break;
                case 1: priOld = "eventual";break;
                case 2: priOld = "current";break;
                case 3: priOld = "urgent";break;
                case 4: priOld = "completed";break; 
                }

                switch(pri.getNewStatus()) {
                case 0: priNew = "inactive";break;
                case 1: priNew = "eventual";break;
                case 2: priNew = "current";break;
                case 3: priNew = "urgent";break;
                case 4: priNew = "completed";break;
                }

                addText = "Changed priority from " + priOld + " to " + priNew;
            }catch(Exception e){}

            changeType = new JLabel(text);
            changeText = new JLabel(addText);

            holder = new JPanel();
            if(commented) {
                holder.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        in = true;
                        out = false;
                    }
                    @Override
                    public void mouseExited(MouseEvent e) {
                        in = false;
                        out = true;
                    }
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        /*JPanel a = (JPanel)e.getSource();
                        JLabel b = (JLabel)a.getComponent(1);
                        String withdraw = b.getText();
                        for(HistoryItem hist: history) {
                            try {
                                Comment comm = (Comment)hist;
                                if(comm.getComment().equals(withdraw))
                                    System.out.println("alsjdlkajsd");
                                    new FXCommentWindow(task, stage);
                                    System.out.println("Something happened");
                            } catch(Exception ex){}
                        }*/
                        if(firstClick == 0 || System.currentTimeMillis()/100 - firstClick > 4 && in) {
                            firstClick = System.currentTimeMillis()/100;
                            //System.out.println("First click: "+firstClick);
                        }
                        else if(System.currentTimeMillis()/100 - firstClick <= 4 && in) {
                            //System.out.println("First try?");
                            firstClick = 0;
                            JPanel a = (JPanel)e.getSource();
                            JLabel b = (JLabel)a.getComponent(1);
                            String withdraw = b.getText();
                            //System.out.println(withdraw);
                            for(HistoryItem hist: history) {
                                try {
                                    Comment comm = (Comment)hist;
                                    if(comm.getComment().equals(withdraw)) {
                                        clickCounter++;
                                        if ((clickCounter % 2) != 0) {
                                            //Ensures window is only opened once
                                            System.out.println("alsjdlkajsd");
                                            //Must run on new thread
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    new FXCommentWindow(task,comm, stage);
                                                    System.out.println("fall through");
                                                    //updateAlt();
                                                    refresh(t,sc);
                                                }
                                            });
                                            //System.out.println("fall through");
                                        }
                                    }


                                } catch(Exception ex){System.out.println(ex);}
                            }
                        }
                    }
                    @Override
                    public void mousePressed(MouseEvent e) {}
                    @Override
                    public void mouseReleased(MouseEvent e) {}
                });
                commented = false;
            }
            holder.setLayout(new GridLayout(0,1,0,10));
            holder.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            holder.setMinimumSize(new Dimension(10,100));

            holder.add(changeType);
            holder.add(changeText);
            panel.add(holder);

        }

0 个答案:

没有答案