2010-03-31 51 views
0

我有一個問題,我一直在處理這個星期。你看,我是新來的Java,所以這可能是...在Java中的GUI中進行應用程序展示

無論如何,我已經開始了純粹的好奇心項目。我在Java中完成了它,但是以簡單的CMD風格,這不是很漂亮。它稱爲我的十個程序之一,由用戶使用「案例」選擇。例如:如果你輸入1,它將進入程序1.完成後,它會返回到選擇狀態,直到用戶按下0.

之後,我一直在試圖爲它做一個GUI。我已經能夠單獨創建選擇器並使其響應所選的數字。如果按1並單擊確定,它會告訴您已經選擇了程序1.但是,我卻無法使其運行該程序。 我一直在試圖讓程序顯示在jPanel中,這是主GUI的一部分。我失敗了。

該程序單獨運行,但我不能讓它們像CMD一樣運行。

這些應用都在Eclipse SDK 3.5.2製成的 的GUI是在NetBeans IDE中取得6.8

下面是唯一的GUI代碼(版畫是PT-BR,我的母語):

主程序:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/* 
* ProgramasGUI.java 
* 
* Created on 31/01/2010, 22:56:19 
*/ 

package Main; 
import Main.Prog1; 

/** 
* 
* @author Zeh® 
*/ 
public class ProgramasGUI extends javax.swing.JFrame { 

    /** Creates new form ProgramasGUI */ 
    public ProgramasGUI() { 
     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() { 

     selecao = new javax.swing.JTextField(); 
     mostrador = new javax.swing.JLabel(); 
     selecionar = new javax.swing.JButton(); 
     mostrador1 = new javax.swing.JLabel(); 
     jPanel1 = new javax.swing.JPanel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     selecao.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       selecaoActionPerformed(evt); 
      } 
     }); 

     mostrador.setText("Qual o programa desejado?"); 

     selecionar.setText("OK"); 
     selecionar.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       selecionarActionPerformed(evt); 
      } 
     }); 

     mostrador1.setText("Saiba aqui o Programa escolhido!"); 

     jPanel1.addComponentListener(new java.awt.event.ComponentAdapter() { 
      public void componentShown(java.awt.event.ComponentEvent evt) { 
       jPanel1ComponentShown(evt); 
      } 
     }); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 100, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 100, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addContainerGap() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(selecao, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(selecionar, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)) 
         .addGap(18, 18, 18) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(mostrador1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(mostrador, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(162, 162, 162) 
         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(39, Short.MAX_VALUE) 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(30, 30, 30) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(selecao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(mostrador, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(selecionar) 
        .addComponent(mostrador1)) 
       .addContainerGap()) 
     ); 

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

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

    private void selecionarActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
      int programa = (int)((Double.parseDouble(selecao.getText()))); 
      switch (programa) { 
      case 1: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 2: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 3: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 4: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 5: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 6: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 7: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 8: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 9: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break; 
      case 10: 
      mostrador1.setText("O programa " + programa + " foi escolhido!"); 
      break;} 
    }           

    private void jPanel1ComponentShown(java.awt.event.ComponentEvent evt) { 

       int programa = (int)((Double.parseDouble(selecao.getText()))); 
      switch (programa) { 
      case 1: 
      addNotify(Prog1.); 
      break;} 
    } 

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

    // Variables declaration - do not modify 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JLabel mostrador; 
    private javax.swing.JLabel mostrador1; 
    private javax.swing.JTextField selecao; 
    private javax.swing.JButton selecionar; 
    // End of variables declaration 
} 

而這裏的子計劃:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/* 
* Prog1.java 
* 
* Created on 02/02/2010, 23:57:59 
*/ 

package Main; 

/** 
* 
* @author Zeh® 
*/ 
public class Prog1 extends javax.swing.JPanel { 

    /** Creates new form Prog1 */ 
    public Prog1() { 
     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() { 

     jLabel1 = new javax.swing.JLabel(); 
     numesc = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 
     jLabel4 = new javax.swing.JLabel(); 
     jButton1 = new javax.swing.JButton(); 
     campo = new javax.swing.JTextField(); 

     jLabel1.setText("Saiba aqui o Antecessor!"); 

     numesc.setText("O número escolhido é: X"); 

     jLabel3.setText("Saiba aqui o Sucessor!"); 

     jLabel4.setText("Insira um número para saber seu Antecessor e Sucessor!"); 

     jButton1.setText("Calcular!"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     campo.setText("Insira seu número"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addGap(16, 16, 16) 
           .addComponent(jButton1)) 
          .addComponent(campo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addGap(10, 10, 10) 
           .addComponent(jLabel3)) 
          .addGroup(layout.createSequentialGroup() 
           .addGap(10, 10, 10) 
           .addComponent(numesc, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)) 
          .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))) 
        .addComponent(jLabel4)) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel4) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(campo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel1)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(numesc) 
       .addGap(11, 11, 11) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel3) 
        .addComponent(jButton1)) 
       .addContainerGap(24, Short.MAX_VALUE)) 
     ); 
    }// </editor-fold> 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     int num = (int)((Double.parseDouble(campo.getText()))); 
      numesc.setText("O número escolhido é: " + num); 
      int x = num-1; 
      jLabel1.setText ("O antecessor é: " + x); 
      int y = num+1; 
      jLabel3.setText ("O sucessor é: " + y); 
    } 


    // Variables declaration - do not modify 
    private javax.swing.JTextField campo; 
    private javax.swing.JButton jButton1; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JLabel numesc; 
    // End of variables declaration 

} 

任何幫助將不勝感激。

如果由於某種原因,我輸入錯誤和/或錯誤地設置了這篇文章的佈局,請接受我的道歉。

回答

1

我不能猜測你正在尋找一種方式從終端運行程序?使用Runtime.getRuntime()。exec(program_call_command); 示例將在GNU/Linux上打印文件夾內容:

public class Executor { 
     public static void main(String[] args) throws IOException { 
      Process p = Runtime.getRuntime().exec("ls"); 
      InputStream in = p.getInputStream(); 
      int size = in.available(); 
      byte[] buf = new byte[size]; 
      in.read(buf); 
      System.out.println(new String(buf)); 
     } 
    } 

Or maybe if all your programs are in java, you can create a new thread for each and call main method for every program. 

public class Executor { 
    public static void main(String[] args) throws IOException { 
     System.out.println("Executor started"); 
     Thread thread = new Thread(){ 
      @Override 
      public void run() { 
       Prog_1.main(null); 
      } 
     }; 
     thread.start(); 

     System.out.println("Executor finished"); 
    } 

} 

class Prog_1{ 
    public static void main(String[] args) { 
     System.out.println("I am going to sleep"); 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
      System.out.println("Really, don't whant to"); 
     } 
     System.out.println("Its time to wake up"); 
    } 
}