gpt4 book ai didi

java - 从另一个类运行 GUI

转载 作者:行者123 更新时间:2023-11-29 07:51:05 25 4
gpt4 key购买 nike

我在“ClientGUI.java”类中创建了我的 GUI,并试图在另一个类(“Client.java”)中创建一个实例。 GUI 只包含两个按钮(“前进”和“后退”),一次只有一个按钮。起初,GUI 似乎工作正常,因为框架是用一个按钮显示的。但是,一旦按下按钮,它就会消失,不会被第二个按钮取代。通过设置断点,我发现调用了正确的 ActionListener 函数并且删除了第一个按钮但没有添加第二个按钮。

GUI 类“ClientGUI.java”:

package GUI;

import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JButton;
import java.net.MalformedURLException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ClientGUI extends JPanel implements ActionListener {

private static JButton btnForward = new JButton("Forward"),
btnBackward = new JButton("Backward");

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}

public ClientGUI() throws MalformedURLException {
setLayout(new BorderLayout());

add(btnForward, BorderLayout.CENTER);

btnForward.addActionListener(this);
btnBackward.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnForward) {
remove(btnForward);

add(btnBackward, BorderLayout.CENTER);

revalidate();
repaint();
}

else if (e.getSource() == btnBackward) {
remove(btnBackward);

add(btnForward);

revalidate();
repaint();
}
}

private static void createAndShowGUI() throws MalformedURLException {
JFrame frame = new JFrame("ClientGUI");

frame.setMinimumSize(new Dimension(500, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ClientGUI());

frame.setSize(500, 400);
frame.setVisible(true);
}
}

以及我想从中使用 GUI 的“Clients.java”类:

import java.net.*;
import GUI.ClientGUI;

public class Client {

public static void main(String[] args) {
Client client = new Client();
}

Client() {
String[] args = {"ggg", "vvv"};

try {
new ClientGUI().main(args);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

感谢您的帮助。

最佳答案

  1. 永远不要调用main 方法。 main 方法是让 JVM 知道程序的入口点。
  2. 不要使用两个main 方法。无论哪个是入口点程序,只有那个程序有main方法
  3. 只需使用一个构造函数。当第一个 GUI 调用第二个时,将构造构造函数中的所有内容。

这是修改后的版本。它有效。

  • 我从 ClientGUI 中删除了 main
  • 我从 Client 类的 main 调用了 ClientGUI.createAndShowGUI()

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Client {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Client();
}
});
}

Client() {
String[] args = {"ggg", "vvv"};

try {
ClientGUI.createAndShowGUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class ClientGUI extends JPanel implements ActionListener {

private static JButton btnForward = new JButton("Forward"),
btnBackward = new JButton("Backward");



public ClientGUI() throws MalformedURLException {
setLayout(new BorderLayout());

add(btnForward, BorderLayout.CENTER);

btnForward.addActionListener(this);
btnBackward.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnForward) {
remove(btnForward);

add(btnBackward, BorderLayout.CENTER);

revalidate();
repaint();
} else if (e.getSource() == btnBackward) {
remove(btnBackward);

add(btnForward);

revalidate();
repaint();
}
}

public static void createAndShowGUI() throws MalformedURLException {
JFrame frame = new JFrame("ClientGUI");

frame.setMinimumSize(new Dimension(500, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ClientGUI());

frame.setSize(500, 400);
frame.setVisible(true);
}
}

关于java - 从另一个类运行 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21161675/

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