2010-03-31 10 views
0

J'ai un problème avec lequel j'ai traité cette semaine. Vous voyez, je suis nouveau à Java, donc c'est probablement ...Création d'une application à l'intérieur d'une interface graphique en Java

De toute façon, j'ai commencé un projet par pure curiosité. Je l'ai fait en Java, mais en style simple CMD, ce qui n'est pas très joli. Il appelle l'un de mes dix programmes, choisis par l'utilisateur en utilisant "Case". Par exemple: Si vous tapez 1, il passera au programme 1. Après l'avoir terminé, il reviendra à la sélection jusqu'à ce que l'utilisateur appuie sur 0.

Ensuite, j'ai essayé de créer une interface graphique pour cela. J'ai été capable de créer le sélecteur seul et le faire répondre aux numéros choisis. Si vous appuyez sur 1 et cliquez sur ok, il vous dira que vous avez choisi le programme 1.

Cependant, je n'ai pas réussi à faire fonctionner le programme. J'ai essayé de faire apparaître le programme dans un jPanel, une partie de l'interface graphique principale. J'ai échoué lamentablement.

Le programme s'exécute séparément mais je ne peux pas les faire fonctionner ensemble comme le CMD.

Les applications ont été faites dans Eclipse SDK 3.5.2 Les interfaces graphiques ont été faites dans Netbeans IDE 6.8

Voici le code pour l'interface graphique uniquement (les impressions sont PT-BR, ma langue maternelle):

programme principal:

/* 
* 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 
} 

Et voici le sous-programme:

/* 
* 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 

} 

Toute aide serait très appréciée.

Si, pour une raison ou une autre, j'ai fait une erreur en tapant et/ou en définissant la mise en page de ce message de façon incorrecte, veuillez accepter mes excuses.

Répondre

1

Je ne peux pas deviner ce que vous cherchez un moyen d'exécuter le programme du terminal? Utilisez Runtime.getRuntime(). Exec (commande_call_command); L'exemple va imprimer le contenu du dossier sous 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"); 
    } 
} 
Questions connexes