简体   繁体   中英

How to debug missing frame contents in Java Swing?

package me.daniel.practice;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Switch
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Password Login System");
        frame.setSize(400, 100);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setBackground(Color.WHITE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        JLabel label = new JLabel("Enter Password: ");
        JPasswordField pass = new JPasswordField(10);
        pass.setEchoChar('*');
        pass.addActionListener(new AL());
        panel.add(label, BorderLayout.WEST);
        panel.add(pass, BorderLayout.EAST);
        frame.add(panel);
    }

    private static String password = "daniel";

    static class AL implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            JPasswordField input = (JPasswordField) e.getSource();
            char[] passy = input.getPassword();
            String p = new String(passy);

            if (p.equals(password))
            {
                JOptionPane.showMessageDialog(null, "Correct");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Incorrect");
            }
        }
    }
}

I want a frame to open and within it, text that says, "Enter Password: " and on its right, a text box that you are able to type you password into. The password in this situation is "daniel."

When you enter the password correctly, another window pops up saying that it's correct. If not, a different window pops up saying that it's incorrect. However, when I run the program, only the frame shows up and not the actual content within the frame.

You should make your frame visible after adding contents to it:

  frame.add(panel);
  frame.setVisible(true); // move down here
}

PS JPanel have default layout manager which is FlowLayout so all the contents would appear inline. In short, panel.add(label, BorderLayout.WEST) won't give the expected output.

You just need to add frame.validate(); after frame.add(panel); .

Although the code you have will most likely work, ideally you should wrap any Java swing initialisation into a SwingUtilities.invokeLater(...) so that it runs on the swing thread:

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run()
      {
        JFrame frame = new JFrame("Password Login System");
        frame.setSize(400, 100);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setBackground(Color.WHITE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        JLabel label = new JLabel("Enter Password: ");
        JPasswordField pass = new JPasswordField(10);
        pass.setEchoChar('*');
        pass.addActionListener(new AL());
        panel.add(label, BorderLayout.WEST);
        panel.add(pass, BorderLayout.EAST);
        frame.add(panel);
        frame.validate();
      }
    });
  }

See oracle docs here for mode details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM