我在“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();
}
}
}
感谢您的帮助。
答案 0 :(得分:4)
main
方法。 main
方法是让JVM知道程序的入口点。main
方法。无论哪个入口点程序,只在那个main
方法
这是一个经过改进的版本。它有效。
main
ClientGUI
ClientGUI.createAndShowGUI()
班级的main
致电Client
。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);
}
}