所以,我正在创建一个排序菜单,并且我有一个登录屏幕。当用户登录时,它会加载另一个菜单。我让它加载了第二个菜单,但我不知道如何删除登录按钮和文本字段等。我该怎么做呢?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
public void fixtureList() //Here is where all of the fixture/timetable elements are based.
{
JButton editButton;
JButton createButton;
JButton logoutButton;
editButton = new JButton("Edit");
editButton.setBounds(10, 10, 80, 30);
add(editButton);
createButton = new JButton(("Create"));
createButton.setBounds(145, 10, 80, 30);
add(createButton);
logoutButton = new JButton(("Log Out"));
logoutButton.setBounds(10, 350, 80, 30);
add(logoutButton);
logoutButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
loginPanel();
System.out.println("Loading the login screen");
repaint();
}
});
}
public void loginPanel() //This is where the screen containing all of the login elements is based.
{
setLayout(null);
JButton loginButton;
JTextField usernameField;
JPasswordField passwordField;
loginButton = new JButton("Login");
loginButton.setBounds(80, 250, 70, 30);
add(loginButton);
usernameField = new JTextField(15);
usernameField.setBounds(60, 110, 130, 30);
add(usernameField);
passwordField = new JPasswordField(15);
passwordField.setBounds(60, 150, 130, 30);
add(passwordField);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
fixtureList();
System.out.println("Loading the fixtures screen");
repaint();
}
});
}
public static void main(String[] args)
{
Main window = new Main();
window.setTitle("PE Fixtures v1.0");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(250, 430);
window.loginPanel();
window.setResizable(false);
window.getContentPane().setBackground(new Color(53, 56, 64));
window.setVisible(true);
}
}
});
最佳答案
I tried
CardLayout
earlier and wasn't too happy with it.
我敦促您重新访问 CardLayout
, 描述 here和插图here .它专为您描述的效果而设计。
为了帮助理解,比较这个更原始的approach删除组件并使用 revalidate()
将组件标记为需要重新布局。
关于Java:如何删除 JPanel 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21804676/