Java:如何擦除JPanel屏幕

时间:2014-02-15 22:41:54

标签: java swing

所以,我正在创建一个排序菜单,我有一个登录屏幕。当用户登录时,它会加载另一个菜单。我有它加载第二个菜单但我不知道如何擦除登录按钮和文本字段等我将如何去做这个?

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);



    }
}
            });

2 个答案:

答案 0 :(得分:1)

  

我之前尝试过CardLayout并对它不太满意。

我建议您重新访问CardLayout,描述here并说明here。它专为您描述的效果而设计。

为了帮助理解,请比较一个删除组件的更原始的approach,并使用revalidate()将组件标记为需要重新布局。

答案 1 :(得分:0)

虽然CardLayout是一种更简洁的方法,但您也可以将小部件提升为实例变量,以便点击处理程序可以更改其可见性。我在这里修改了你的例子。但是,在没有布局管理器的情况下使用Swing会引导您的下巴。有些机器会在某个地方产生小部件的渲染(技术术语)。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwingTest extends JFrame {

    JButton loginButton;
    JTextField usernameField;
    JPasswordField passwordField;
    JButton editButton;
    JButton createButton;
    JButton logoutButton;

    private void setLoginFieldVisibility(boolean val) {
        loginButton.setVisible(val);
        usernameField.setVisible(val);
        passwordField.setVisible(val);
        invalidate();
    }

    private void setFixtureFieldVisibility(boolean val) {
        editButton.setVisible(val);
        createButton.setVisible(val);
        logoutButton.setVisible(val);
        invalidate();
    }

    private void initialize() {
        setLayout(null);
        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);        
        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);

        setFixtureFieldVisibility(false);

        loginButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setLoginFieldVisibility(false);
                setFixtureFieldVisibility(true);
            }
        });

        logoutButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setLoginFieldVisibility(true);
                setFixtureFieldVisibility(false);
            }
        });
    }

    public static void main(String[] args) {

        SwingTest window = new SwingTest();

        window.setTitle("PE Fixtures v1.0");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(250, 430);
        window.initialize();
        window.setResizable(false);
        window.getContentPane().setBackground(new Color(53, 56, 64));
        window.setVisible(true);
    }
}