gpt4 book ai didi

java - 使用 Java 使应用程序显示在 GUI 中

转载 作者:行者123 更新时间:2023-12-01 16:06:06 26 4
gpt4 key购买 nike

本周我一直在处理一个问题。你看,我是 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

}

任何帮助将不胜感激。

如果由于某种原因我输入错误和/或错误地设置了这篇文章的布局,请接受我的歉意。

最佳答案

我无法猜测您正在寻找一种从终端运行程序的方法?使用 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");
}
}

关于java - 使用 Java 使应用程序显示在 GUI 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2550070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com