如何使按钮显示其他按钮和选项。

时间:2013-12-08 03:12:03

标签: java swing jbutton jlabel jtextfield

我正在尝试创建程序,您可以按下第一页上的其中一个按钮,它将打开更多按钮和标签等。但我不知道如何制作它,以便当按下按钮时它会显示其他按钮和标签。

代码是

public class Japp extends JApplet implements ActionListener{

    private JPanel panel1= new JPanel();
    private JPanel panel2= new JPanel();

    JPanel container = new JPanel();
    //container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

    JButton reg = new JButton ("Register");
    JButton log = new JButton ("Login");

    JLabel regusrlbl = new JLabel ("Enter Username");
    JLabel regpwdlbl = new JLabel ("Enter Password");
    JLabel regpwdconlbl = new JLabel ("Confirm Username");
    JTextField regusr = new JTextField("");
    JTextField regpwd = new JTextField("");
    JTextField regpwdcon = new JTextField("");
    JButton create = new JButton("Create");

    JLabel logusrlbl = new JLabel ("Enter Username");
    JLabel logpwdlbl = new JLabel ("Enter Password");
    JTextField logusr = new JTextField("");
    JTextField logpwd = new JTextField("");
    JButton login = new JButton("Login");

    public void init(){
    setLayout(null); 
    add(reg).setBounds(50,100,100,30);
    add(log).setBounds(150,100,100,30);

    }
    @SuppressWarnings("deprecation")

    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton)e.getSource();
        if (source.getLabel()=="reg"){

        // TODO Auto-generated method stub
            remove(reg);
            remove(log);
            add(regusrlbl).setBounds(20,20, 150,50);
            add(regusr).setBounds(20,50,100,30);
            add(regpwdlbl).setBounds(200,20,150,50);
            add(regpwd).setBounds(200,50,100,30);
            add(regpwdconlbl).setBounds(195,80,150,30);
            add(regpwdcon).setBounds(200,110,100,30);
            add(create).setBounds(110,140,100,30);
        // setup all the context...
        }   
        else if(source.getLabel()=="log"){
            remove(reg);
            remove(log);
            add(logusrlbl).setBounds(20,20, 150,50);
            add(logusr).setBounds(20,50,100,30);
            add(logpwdlbl).setBounds(200,20,150,50);
            add(logpwd).setBounds(200,50,100,30);
            add(login).setBounds(110,140,100,30);
        }
    }   
}

1 个答案:

答案 0 :(得分:2)

  1. 避免null布局。您无法控制字体在不同系统上的呈现方式,因此除非您打算仅在为其开发的计算机上运行应用程序,否则请避免使用null布局。学习如何使用和理解如何使用不同的布局管理器来实现您的目标。
  2. 使用CardLayout,这就是它的目的,让您轻松更改可见组件组。
  3. 这将要求您将应用程序分解为单独的组件/面板,但是您可以独立地关注每个部分的个性化需求

    使用示例更新

    enter image description here enter image description here enter image description here

    import java.awt.CardLayout;
    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JSeparator;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestCardLayout100 {
    
        public static void main(String[] args) {
            new TestCardLayout100();
        }
    
        public TestCardLayout100() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    LoginPane loginPane = new LoginPane();
                    RegisterPane registerPane = new RegisterPane();
    
                    JPanel startPane = new JPanel(new GridBagLayout());
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = 0;
                    gbc.gridy = 0;
                    startPane.add(loginPane, gbc);
                    gbc.gridx = 2;
                    startPane.add(registerPane, gbc);
                    gbc.gridx = 1;
                    gbc.fill = GridBagConstraints.VERTICAL;
                    startPane.add(new JSeparator(JSeparator.VERTICAL), gbc);
    
                    JPanel loggedInPane = new JPanel(new GridBagLayout());
                    loggedInPane.add(new JLabel("Logged In..."));
    
                    JPanel registeredPane = new JPanel(new GridBagLayout());
                    registeredPane.add(new JLabel("Registered..."));
    
                    final JFrame frame = new JFrame("Testing");
                    final CardLayout cardLayout = new CardLayout();
    
                    loginPane.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            cardLayout.show(frame.getContentPane(), "loggedIn");
                        }
                    });
                    registerPane.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            cardLayout.show(frame.getContentPane(), "registered");
                        }
                    });
    
                    Container contentPane = frame.getContentPane();
                    contentPane.setLayout(cardLayout);
                    contentPane.add(startPane, "startPane");
                    contentPane.add(loggedInPane, "loggedIn");
                    contentPane.add(registeredPane, "registered");
    
                    cardLayout.show(contentPane, "startPane");
    
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class LoginPane extends JPanel {
    
            private final JLabel logusrlbl = new JLabel("Enter Username");
            private final JLabel logpwdlbl = new JLabel("Enter Password");
            private final JTextField logusr = new JTextField(10);
            private final JTextField logpwd = new JPasswordField(10);
            private final JButton login = new JButton("Login");
    
            public LoginPane() {
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(new JLabel("Login"), gbc);
    
                gbc.gridy++;
                gbc.gridwidth = 1;
                add(logusrlbl, gbc);
                gbc.gridy++;
                add(logpwdlbl, gbc);
    
                gbc.gridx++;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.gridy = 1;
    
                add(logusr, gbc);
                gbc.gridy++;
                add(logpwd, gbc);
    
                gbc.gridx = 0;
                gbc.gridy++;
                gbc.anchor = GridBagConstraints.EAST;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.fill = GridBagConstraints.NONE;
                add(login, gbc);
    
            }
    
            public void addActionListener(ActionListener listener) {
                login.addActionListener(listener);
            }
    
        }
    
        public class RegisterPane extends JPanel {
    
            private final JLabel logusrlbl = new JLabel("Enter Username");
            private final JLabel logpwdlbl = new JLabel("Enter Password");
            private final JTextField logusr = new JTextField(10);
            private final JTextField logpwd = new JPasswordField(10);
            private final JButton register = new JButton("Register");
    
            public RegisterPane() {
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(new JLabel("Register"), gbc);
    
                gbc.gridy++;
                gbc.gridwidth = 1;
                add(logusrlbl, gbc);
                gbc.gridy++;
                add(logpwdlbl, gbc);
    
                gbc.gridx++;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.gridy = 1;
    
                add(logusr, gbc);
                gbc.gridy++;
                add(logpwd, gbc);
    
                gbc.gridx = 0;
                gbc.gridy++;
                gbc.anchor = GridBagConstraints.EAST;
                gbc.fill = GridBagConstraints.NONE;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(register, gbc);
    
            }
    
            public void addActionListener(ActionListener listener) {
                register.addActionListener(listener);
            }
    
        }
    
    }