Java运行GUI没有控制台窗口

时间:2015-04-03 13:04:08

标签: java batch-file

我正在尝试从批处理文件启动Java GUI(在Netbeans中创建),而不打开命令窗口。我一直在寻找最后一小时,并尝试过javawstart /B javaw等,但没有任何效果。有谁知道我做错了什么?

  

run_GUI.BAT

@ECHO OFF

start /B javaw GUI -ORBInitialPort 1050 -ORBInitialHost localhost

PAUSE
  

GUI.java

import javax.swing.*;
/**
 *
 * @author  
 */
public class GUI extends javax.swing.JFrame {

    /**
     * Creates new form GUI
     */
    public GUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        outerPanel = new javax.swing.JPanel();
        upperInnerPanel = new javax.swing.JPanel();
        gameWinsPanel = new javax.swing.JPanel();
        wonLabel = new javax.swing.JLabel();
        lostLabel = new javax.swing.JLabel();
        gameWinsLabel = new javax.swing.JLabel();
        gameLossesLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textDisplay = new javax.swing.JTextArea();
        gameButtonsPanel = new javax.swing.JPanel();
        newGameButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        lowerInnerPanel = new javax.swing.JPanel();
        optionButtonsPanel = new javax.swing.JPanel();
        paperButtton = new javax.swing.JButton();
        scissorsButton = new javax.swing.JButton();
        stoneButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(500, 500));
        setMinimumSize(new java.awt.Dimension(500, 0));
        setPreferredSize(new java.awt.Dimension(600, 657));
        getContentPane().setLayout(new java.awt.GridLayout());

        outerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "PAPER SCISSORS STONE", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N

        upperInnerPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        gameWinsPanel.setLayout(new java.awt.GridBagLayout());

        wonLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        wonLabel.setText("Won:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipady = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 0);
        gameWinsPanel.add(wonLabel, gridBagConstraints);

        lostLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lostLabel.setText("Lost:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipady = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 11, 0);
        gameWinsPanel.add(lostLabel, gridBagConstraints);

        gameWinsLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        gameWinsLabel.setText("0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 19;
        gridBagConstraints.ipady = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 57);
        gameWinsPanel.add(gameWinsLabel, gridBagConstraints);

        gameLossesLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        gameLossesLabel.setText("0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 19;
        gridBagConstraints.ipady = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 9, 11, 0);
        gameWinsPanel.add(gameLossesLabel, gridBagConstraints);

        textDisplay.setColumns(20);
        textDisplay.setRows(5);
        jScrollPane1.setViewportView(textDisplay);

        gameButtonsPanel.setLayout(new java.awt.GridLayout(3, 0));

        newGameButton.setText("New Game");
        gameButtonsPanel.add(newGameButton);

        jButton1.setText("Exit");
        gameButtonsPanel.add(jButton1);

        javax.swing.GroupLayout upperInnerPanelLayout = new javax.swing.GroupLayout(upperInnerPanel);
        upperInnerPanel.setLayout(upperInnerPanelLayout);
        upperInnerPanelLayout.setHorizontalGroup(
            upperInnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, upperInnerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(upperInnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(upperInnerPanelLayout.createSequentialGroup()
                        .addComponent(gameButtonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(gameWinsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        upperInnerPanelLayout.setVerticalGroup(
            upperInnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, upperInnerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(upperInnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(upperInnerPanelLayout.createSequentialGroup()
                        .addComponent(gameWinsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gameButtonsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE))
                .addContainerGap())
        );

        lowerInnerPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        optionButtonsPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        optionButtonsPanel.setLayout(new java.awt.GridLayout());

        paperButtton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        paperButtton.setText("PAPER");
        optionButtonsPanel.add(paperButtton);

        scissorsButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        scissorsButton.setText("SCISSORS");
        optionButtonsPanel.add(scissorsButton);

        stoneButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        stoneButton.setText("STONE");
        optionButtonsPanel.add(stoneButton);

        javax.swing.GroupLayout lowerInnerPanelLayout = new javax.swing.GroupLayout(lowerInnerPanel);
        lowerInnerPanel.setLayout(lowerInnerPanelLayout);
        lowerInnerPanelLayout.setHorizontalGroup(
            lowerInnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lowerInnerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optionButtonsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        lowerInnerPanelLayout.setVerticalGroup(
            lowerInnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, lowerInnerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optionButtonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout outerPanelLayout = new javax.swing.GroupLayout(outerPanel);
        outerPanel.setLayout(outerPanelLayout);
        outerPanelLayout.setHorizontalGroup(
            outerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(outerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lowerInnerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(upperInnerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        outerPanelLayout.setVerticalGroup(
            outerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(upperInnerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lowerInnerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        getContentPane().add(outerPanel);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel gameButtonsPanel;
    private javax.swing.JLabel gameLossesLabel;
    private javax.swing.JLabel gameWinsLabel;
    private javax.swing.JPanel gameWinsPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lostLabel;
    private javax.swing.JPanel lowerInnerPanel;
    private javax.swing.JButton newGameButton;
    private javax.swing.JPanel optionButtonsPanel;
    private javax.swing.JPanel outerPanel;
    private javax.swing.JButton paperButtton;
    private javax.swing.JButton scissorsButton;
    private javax.swing.JButton stoneButton;
    private javax.swing.JTextArea textDisplay;
    private javax.swing.JPanel upperInnerPanel;
    private javax.swing.JLabel wonLabel;
    // End of variables declaration//GEN-END:variables
}

2 个答案:

答案 0 :(得分:0)

如果您不想要控制台窗口,请不要使用批处理文件。

只需指定JavaW的路径,就可以拥有合适的命令行,无需批处理。它也可以像快捷方式一样。 Start表示执行Windows命令行,与“开始 - 运行”或快捷方式的目标相同。有VBS和JS。

根本不需要批处理文件。

答案 1 :(得分:0)

如果您不希望它在控制台中运行,则需要将其转换为.jar文件。如果制作得当,罐子可以执行,并且可以作为GUI程序在没有控制台的情况下运行。

要创建一个.jar文件,您需要做三件事。

1:确保正确设置了命令提示符(假设您使用的是Windows,否则您将需要用于操作系统的等效命令)以使用javac命令编译Java。如果javac在控制台中有效,则jar命令也可以。

2:您的程序的类文件,您应该已经拥有。

3:清单。

因此,要创建清单,只需打开记事本并键入以下内容:

Main-Class: GUI

在您的班级名称之后,按Enter或return。如果清单末尾没有换行符,则它将行不通。

将此文件保存为与类文件相同的文件夹“ main.mf”

完成所有操作后,打开命令提示符,根据需要更改当前工作目录,然后输入以下内容:

jar cvmf main.mf GUI.jar Gui.class

这将创建一个名为GUI.jar的.jar文件,双击该文件即可在没有控制台的情况下运行您的程序。

我建议您查找有关.jar文件的更多信息,因为它们对于将文件打包在一起进行分发非常有用。