我面临一点问题,因为我是Java中的newbe。我做了两个框架,第一个名字是test,第二个是Login。在测试Jframe中,有三个按钮。第三个名称是" Admin"是带我到第二帧。如何在不关闭第一帧的情况下将第三个按钮带到第二帧?
提前致谢。
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class test extends JFrame {
private static final int width = 600, height = 480;
private static final int buttonWidth = 200, buttonHeight = 100;
static JFrame frame;
static JButton button;
static JButton button2;
static JButton button3;
public static void main(String[] args) {
createWindow();
createButtons();
addEverything();
frame.setVisible(true);
}
private static void createWindow() {
frame = new JFrame("Welcome to Easy Hire");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
}
private static void createButtons() {
button = new JButton("Hire A Taxi");
button.setLocation(0, 0);
button2 = new JButton("User Login");
button2.setLocation(200, 0);
button3 = new JButton("Admin");
button3.setLocation(400, 0);
}
private static void addEverything() {
addButtons();
}
private static void addButtons() {
frame.getContentPane().setLayout(null);
frame.getContentPane().add(button);
frame.getContentPane().add(button2);
frame.getContentPane().add(button3);
button.setSize(200,50);
button2.setSize(200,50);
button3.setSize(200,50);
}
}
答案 0 :(得分:3)
建议:
addActionListener(...)
方法向其添加ActionListeners。我没有看到你试过这个。有用的Java教程: