简体   繁体   English

如何在Java Swing中调试缺少的帧内容?

[英]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. 我希望打开一个框架,并在其中打开“ Enter Password:”(输入密码:)文本,并在其右侧显示一个文本框,您可以在其中输入密码。 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. PS JPanel具有默认的布局管理器FlowLayout因此所有内容将内联显示。 In short, panel.add(label, BorderLayout.WEST) won't give the expected output. 简而言之, panel.add(label, BorderLayout.WEST)不会提供预期的输出。

You just need to add frame.validate(); 您只需要添加frame.validate(); after frame.add(panel); 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: 尽管您拥有的代码很可能会工作,但是理想情况下,您应该将所有Java swing初始化包装到SwingUtilities.invokeLater(...)以便它在swing线程上运行:

  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. 有关模式详细信息,请参见此处的 oracle文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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