use*_*098 1 java user-interface swing
我在“ ClientGUI.java”类中创建了我的GUI,并尝试在另一个类(“ Client.java”)中创建了一个实例。GUI只包含两个按钮(“前进”和“后退”),一次仅显示一个按钮。刚开始,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);
}
}
Run Code Online (Sandbox Code Playgroud)
我要使用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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助。
main方法。该main方法使JVM知道程序的入口点。main方法。以入口程序为准,main仅在该程序中有一种方法这是改版。有用。
main从ClientGUIClientGUI.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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2511 次 |
| 最近记录: |