在JButton MouseClicked中执行的Java类

时间:2011-03-17 07:47:40

标签: java swing netbeans jbutton actionlistener

我是Java新手。有了这个课,我可以打开一个pdf文件:

public class ViewPdf {
  public static void main(String[] args) throws Exception {
    Process p = 
      Runtime.getRuntime()
        .exec("rundll32 url.dll,FileProtocolHandler c:/pdf/mypdf.pdf");
    p.waitFor();
    System.out.println("Done.");
  }
}

现在想要使用Jbutton鼠标事件来触发此类并打开该pdf。我在Netbeans工作可以有人给我一些帮助吗?

NewJFrame.java

public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
        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">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout =
            new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, 
                layout.createSequentialGroup()
                .addContainerGap(217, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(108, 108, 108))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addComponent(jButton1)
                .addContainerGap(210, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration

}

提前致谢... ^^

2 个答案:

答案 0 :(得分:1)

你拥有的地方

// TODO add your handling code here:

您只需添加代码即可执行PDF打开

Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler c:/pdf/mypdf.pdf"); p.waitFor(); System.out.println("Done.");

答案 1 :(得分:1)

作为替代方案,您可以在按钮处理程序中使用open()