[英]Run different class from same java (eclipse) project
This "program" or "app" I'm writing is a thing I want to do to prove myself I can learn java, it's not a serious game, it's just a game for myself to see if I can do things right. 我正在写的这个“程序”或“应用程序”是我想证明自己可以学习Java的一件事,这不是一个严肃的游戏,它只是一个让我自己看是否可以做正确的游戏。
This is my current code 这是我当前的代码
//*Importación del botón
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//*Set de importación para sonido
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; //Fin del set de sonido
import javax.swing.JFrame; //*Importando la ventana
import javax.swing.JLabel; //*Texto en la ventana
import javax.swing.JPanel;
import javax.swing.SwingConstants; //*Swing para alinear texto
//*Comienza el juego
public class Principal {
public static void main(String args[]) {
//*Script original (Añadido final para cerrarlo)
final JFrame vprincipal = new JFrame ();
JLabel texto = new JLabel("Bienvenida",SwingConstants.CENTER);
//*Añadiendo el JPanel no original
JPanel contenedor = new JPanel();
contenedor.setOpaque(true);
contenedor.setBackground(Color.WHITE);
contenedor.setLayout(null);
//*Script original
texto.setText("Bienvenidos a la versión alfa 0.1 de Mazmorra de Puertas");
texto.setLocation(500,500);
vprincipal.add(texto);
String nombre = "Mazmorra de Puertas";
vprincipal.setTitle(nombre);
vprincipal.setSize(400, 200);
vprincipal.setVisible(true);
vprincipal.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
vprincipal.setVisible(true);
vprincipal.setLocation(750,350);
vprincipal.setLayout(new BorderLayout());
contenedor.add(texto);
//*Añadiendo botón
final JButton empezar = new JButton("Empezar");
vprincipal.setContentPane(contenedor);
contenedor.add(empezar);
empezar.setSize(100, 20);
empezar.setLocation(140, 130);
//*Comienzo del juego
final JFrame comienzo = new JFrame();
String nombre2 = "Version a0.1";
comienzo.setTitle(nombre2);
comienzo.setSize(400, 200);
comienzo.setVisible(false);
comienzo.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
comienzo.setVisible(false);
comienzo.setLocation(750,350);
comienzo.setLayout(new BorderLayout());
//*Añadiendo acciones al botón
empezar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
vprincipal.dispose();
comienzo.setVisible(false);
}
});
//*Aquí se añade el sonido
try {
AudioInputStream audio = AudioSystem.getAudioInputStream(new File("Intro.wav"));
final Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
empezar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
clip.stop();
}
});
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}
}
{
}
}
I'd like to access to GuessingGame (An example from a learning java book I'm reading) after this chunk of code: 在这段代码之后,我想访问GuessingGame(我正在阅读的一本学习中的Java书籍中的示例):
//*Añadiendo acciones al botón
empezar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
vprincipal.dispose();
comienzo.setVisible(false);
I've tried with new nameofclass(); 我尝试了新的nameofclass(); and several more things that I'd take long to write here all attempts I'd try. 还有我将在这里写很多时间的所有尝试。
I'm trying to call from class Principal
to class GuessingGame
. 我正在尝试从Principal
类调用到GuessingGame
类。 How to do it when I press the button called empezar
? 当我按下名为empezar
的按钮时该怎么做?
If I understood correctly you want to run method main from another Java class (representing some game). 如果我理解正确,则希望从另一个Java类(表示某些游戏)运行main方法。 Well, you can do it simply by calling: 好吧,您可以简单地通过调用以下命令来实现:
AnotherClass.main({});
Assuming AnotherClass
is the class with game you want to run. 假设AnotherClass
是您要运行的游戏的类。
You can also run another class in the separate process, using ProcessBuilder API : 您还可以使用ProcessBuilder API在单独的过程中运行另一个类:
// method to start class in another JVM process
public static void startProcess(Class<?> clazz) throws Exception {
System.out.println(clazz.getCanonicalName());
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home") + separator + "bin" + separator + "java";
ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", classpath, clazz.getCanonicalName());
Process process = processBuilder.start();
process.waitFor();
}
// run in another process
startProcess(AnotherClass.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.