如何调试 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");
}
}
}
}
我想打开一个框架,在框架中,显示 "Enter Password: " 的文本,在右侧是一个文本框,您可以在其中输入密码。这种情况下的密码是 "daniel."
当您正确输入密码时,会弹出另一个 window 提示密码正确。如果不是,则会弹出一个不同的 window 说它不正确。但是,当我 运行 程序时,只显示框架而不是框架内的实际内容。
您应该在添加内容后让框架可见:
frame.add(panel);
frame.setVisible(true); // move down here
}
P.S. JPanel
有默认的布局管理器,它是 FlowLayout
所以所有的内容都会显示在行内。简而言之,panel.add(label, BorderLayout.WEST)
不会给出预期的输出。
你只需要在frame.add(panel);
之后添加frame.validate();
。
虽然您拥有的代码很可能会工作,但理想情况下,您应该将任何 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();
}
});
}
有关模式的详细信息,请参阅 oracle 文档 here。
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");
}
}
}
}
我想打开一个框架,在框架中,显示 "Enter Password: " 的文本,在右侧是一个文本框,您可以在其中输入密码。这种情况下的密码是 "daniel."
当您正确输入密码时,会弹出另一个 window 提示密码正确。如果不是,则会弹出一个不同的 window 说它不正确。但是,当我 运行 程序时,只显示框架而不是框架内的实际内容。
您应该在添加内容后让框架可见:
frame.add(panel);
frame.setVisible(true); // move down here
}
P.S. JPanel
有默认的布局管理器,它是 FlowLayout
所以所有的内容都会显示在行内。简而言之,panel.add(label, BorderLayout.WEST)
不会给出预期的输出。
你只需要在frame.add(panel);
之后添加frame.validate();
。
虽然您拥有的代码很可能会工作,但理想情况下,您应该将任何 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();
}
});
}
有关模式的详细信息,请参阅 oracle 文档 here。