开发者

Making a app show inside a GUI in Java

开发者 https://www.devze.com 2022-12-25 08:36 出处:网络
I have one problem I\'ve been dealing with for this week. You see, I\'m new to Java, so that\'s probably it...

I have one problem I've been dealing with for this week. You see, I'm new to Java, so that's probably it...

Anyways, I've started a project out of pure curiosity. I had it done in Java, but in plain CMD-style, which is not very pretty. It calls one of my ten programs, chosen by the user using "Case". Eg.: If you type 1, it will go to program 1. After you finish it, it goes back to selection until the user press 0.

After that, I've been trying to make a GUI for it. I've been able to create the selector alone and make it respond to the numbers chosen. If you press 1 and click ok, it will tell you you've chosen program 1.

I, however, have not been able to make it run the program. I've been trying to make the program show up in a jPanel, part of the main GUI. I failed miserably.

The program runs in separate but I can't make them run together like the CMD one.

The apps were made in Eclipse SDK 3.5.2 The GUIs were made in Netbeans IDE 6.8

Here's the code for the GUI only (the prints are in PT-BR, my native language):

Main program:

/*
 * 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:
   开发者_JS百科 }                                       

    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
}

And here's the sub-program:

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

}

Any help would be very appreciated.

If I, for some reason, made a mistake typing and/or setting the layout of this post incorrectly, please accept my apologies.


I can't guess what you are searching for a way to run program from terminal? Use Runtime.getRuntime().exec(program_call_command); Example will print folder content on 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");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消