与制作JFrame对象相反,我扩展了JFrame并创建了一个Panel。我希望我创建的面板在用户单击某些按钮时关闭,但是即使我使用dispose()方法放置动作侦听器,该面板仍保持打开状态。我知道我的代码现在是一团糟,但是请您看看我做错了什么。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartUp extends JFrame{
private JButton createAccount, login;
private static final int WIDTH = 500;
private static final int HEIGHT = 300;
public StartUp(){
createView();
setTitle("MovieBase Login");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setMinimumSize(new Dimension(400, 200));
setLocationRelativeTo(null);
setResizable(false);
}
private void createView(){
JPanel panel = new JPanel(new GridBagLayout());
getContentPane().add(panel);
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(10,10,10,10);
constraints.gridx = 0;
constraints.gridy = 1;
JLabel label = new JLabel("Welcome to MovieBase, your personal Movie Database!");
panel.add(label, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
createAccount = new JButton("Create Account");
panel.add(createAccount, constraints);
createAccount.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
new CreateAccount();
}
});
createAccount.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
dispose();
}
});
constraints.gridx = 0;
constraints.gridy = 3;
login = new JButton("Login");
panel.add(login, constraints);
login.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
new Login();
}
});
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
try{
StartUp window = new StartUp();
window.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
单独班级
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CreateAccount extends JFrame{
private JLabel user, pass; //label next to username and password field
private JTextField username; //enter username
private JPasswordField password; //enter password
private JButton create;
private String u, p;
private static final int WIDTH = 500;
private static final int HEIGHT = 300;
public CreateAccount(){
createView();
setTitle("Create Account");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setMinimumSize(new Dimension(WIDTH, HEIGHT));
setLocationRelativeTo(null);
setResizable(false);
}
public void setUser(String usn){
u = usn;
}
public String getUser(){
return u;
}
public void setPass(String psd){
p = psd;
}
public String getPass(){
return p;
}
private void createView(){
setVisible(true);
JPanel panelMain = new JPanel(null);
getContentPane().add(panelMain);
user = new JLabel("Username: ");
panelMain.add(user);
user.setBounds(50, 30, 120, 25);
username = new JTextField();
panelMain.add(username);
username.setBounds(140, 30, 150, 25);
pass = new JLabel("Password: ");
panelMain.add(pass);
pass.setBounds(50, 100, 120, 25);
password = new JPasswordField();
panelMain.add(password);
password.setBounds(140, 100, 150, 25);
create = new JButton("Create");
panelMain.add(create);
create.setBounds(170, 180, 100,25);
create.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String usn = username.getText();
String psd = password.getText();
setUser(usn);
setPass(psd);
}
});
create.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
new Login2().setVisible(true);
}
});
create.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
答案 0 :(得分:1)
dispose
上没有JPanel
方法-我看到您两次调用它,两次都是在JFrame
的扩展名上。
更具体地说-Java模式中的方法总是以object.method(arguments)
的形式在某些对象上调用-Java提供了一种快捷方式表示法-如果未指定对象,则暗示this
。调用dispose()
时,并没有明确提供正在调用它的任何对象,并且this
在上下文中是JFrame
的扩展。
如果您只想隐藏面板,则可以致电setVisible(false)