我有一个问题,我本周一直在处理这个问题。你知道,我是Java的新手,所以可能就是这样......
无论如何,我出于纯粹的好奇心开始了一个项目。我用Java完成了它,但是采用普通的CMD风格,这不是很漂亮。它调用我的十个程序中的一个,由用户使用“Case”选择。例如:如果您输入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
}
非常感谢任何帮助。
如果由于某种原因我错误地输入和/或错误地设置了这篇文章的布局,请接受我的道歉。
答案 0 :(得分: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");
}
}