다른 클래스의 main 메소드를 호출 할 수있는 두 개의 버튼이있는 JFrame을 개발하려고합니다. 첫 번째 시도는 각 버튼의 actionPerformed에 직접 삽입하는 것이 었습니다. 이렇게하면 다른 클래스의 JFrame이 열리지만 제목 만 표시되고 프로그램이 추가로 고정 된 JPanel의 내용은 표시되지 않습니다. 닫는 버튼을 누르거나, 작업 관리자 나 일식으로 가서 죽여야합니다.) 두 번째 시도는 actionPerformed에 메소드 호출을 추가하고이 메소드가 이번에는 다른 클래스의 main 메소드를 호출하지만 동일한 결과 (프로그램 고정)를 추가합니다.JButton을 누를 때 다른 클래스의 Java 실행 메인 메소드
테스트 목적으로 다른 클래스의 main 메서드에 대한 호출을 배치했습니다.이 클래스의 main 메서드는 다른 클래스의 프레임이 JPanel의 모든 내용, 기능 등을 성공적으로 나타냈다는 것을 증명했습니다.
부울이 true로 설정 될 때까지 기다리는 것이 주된 방법으로 무한 루프를 만들 수 있다는 것을 알았지 만 그다지 비싸지 않은 방식으로 작동해야합니다. 그래서 여기 나는 너희들에게이 질문을하고있다.
다음은 두 번째 시도 코드입니다.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Chat {
public static void main (String[] args) {
JFrame window = new JFrame("Chat Selection");
//Set the default operation when user closes the window (frame)
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the size of the window
window.setSize(600, 400);
//Do not allow resizing of the window
window.setResizable(false);
//Set the position of the window to be in middle of the screen when program is started
window.setLocationRelativeTo(null);
//Call the setUpWindow method for setting up all the components needed in the window
window = setUpWindow(window);
//Set the window to be visible
window.setVisible(true);
}
private static JFrame setUpWindow(JFrame window) {
//Create an instance of the JPanel object
JPanel panel = new JPanel();
//Set the panel's layout manager to null
panel.setLayout(null);
//Set the bounds of the window
panel.setBounds(0, 0, 600, 400);
JButton client = new JButton("Run Client");
JButton server = new JButton("Run Server");
JLabel author = new JLabel("By xxx");
client.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//run client main
runClient();
}
});
server.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//run server main
}
});
panel.add(client);
client.setBounds(10,20,250,200);
panel.add(server);
server.setBounds(270,20,250,200);
panel.add(author);
author.setBounds(230, 350, 200, 25);
window.add(panel);
return window;
}
private static void runClient() {
String[] args1={"10"};
ClientMain.main(args1);
}
}
죄송합니다. 테스트 목적으로 "입증 된이 클래스 기본 메소드에서 곧바로"추가하는 것을 잊었습니다. 바로 수정하여 – Adrian
메인 스레드를 차단하고 UI를 정지시킵니다. 스레딩을 사용하십시오. – Reji