Swing 小程序组件在与交互之前是不可见的

发布于 2024-10-18 05:22:57 字数 1770 浏览 1 评论 0原文

我正在尝试制作一个带有简单登录屏幕的小程序,如果我使用普通组件,它可以正常工作,但如果我使用 swing 组件,则对象在单击之前不会显示。我会使用常规组件,但我需要一个屏蔽密码字段(如果有非摇摆版本,请告诉我)。

我正在尝试将其垂直放置在左上角。

public class RdpApplet extends JApplet {
   JButton Connect;
   JTextField Username;
   JPasswordField Password;
   JLabel UsernameLabel;
   JLabel PasswordLabel;

   //(Snip)

   public void paint(Graphics g){
   }
   public void start(){
   }
   public void stop(){
   }
   public void init(){
       SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           JPanel panel = new JPanel (new GridBagLayout());
           GridBagConstraints gbc = new GridBagConstraints();
           gbc.insets = new Insets(2,5,1,1);
           gbc.weightx = 1.0;
           gbc.anchor = GridBagConstraints.WEST;
           gbc.gridwidth = GridBagConstraints.REMAINDER;

           UsernameLabel = new JLabel("Username:");
           panel.add(UsernameLabel,gbc);

           Username = new JTextField(15);
           panel.add(Username,gbc);

           PasswordLabel = new JLabel("Password:");
           panel.add(PasswordLabel,gbc);

           Password = new JPasswordField(15);
           panel.add(Password,gbc);

           Connect = new JButton("Connect");
           panel.add(Connect,gbc);
           gbc.weighty = 1.0;
           gbc.anchor = gbc.NORTHWEST;

           setLayout(new FlowLayout(FlowLayout.LEFT));
           add(panel);
           validate();
           panel.validate();
       }});
   }

   //(Snip)
}

如果我使用 JButton 和 JTextField 和 JLabel,这些项目在我与它们交互之前不会显示(单击文本字段,将鼠标悬停在按钮上,我无法显示标签),如果我使用我得到的普通版本标签周围丑陋的灰色背景。

谁能帮我看看我做错了什么以使一切正常工作。

编辑: 从 Applet 更改为 JApplet 并没有解决问题。

编辑2: 添加了其他方法。

编辑3: 光标现在从用户名框中开始,但在交互之前所有内容仍然不可见。使用所有最新建议更新了代码。

I am trying to make a applet with a simple login screen, if I use normal components it works fine but if I use swing components the object wont show up until it is clicked on. I would use regular components but i need a masked password field (if there is a non swing version please let me know).

I am trying to get a vertical placement in the top left corner.

public class RdpApplet extends JApplet {
   JButton Connect;
   JTextField Username;
   JPasswordField Password;
   JLabel UsernameLabel;
   JLabel PasswordLabel;

   //(Snip)

   public void paint(Graphics g){
   }
   public void start(){
   }
   public void stop(){
   }
   public void init(){
       SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           JPanel panel = new JPanel (new GridBagLayout());
           GridBagConstraints gbc = new GridBagConstraints();
           gbc.insets = new Insets(2,5,1,1);
           gbc.weightx = 1.0;
           gbc.anchor = GridBagConstraints.WEST;
           gbc.gridwidth = GridBagConstraints.REMAINDER;

           UsernameLabel = new JLabel("Username:");
           panel.add(UsernameLabel,gbc);

           Username = new JTextField(15);
           panel.add(Username,gbc);

           PasswordLabel = new JLabel("Password:");
           panel.add(PasswordLabel,gbc);

           Password = new JPasswordField(15);
           panel.add(Password,gbc);

           Connect = new JButton("Connect");
           panel.add(Connect,gbc);
           gbc.weighty = 1.0;
           gbc.anchor = gbc.NORTHWEST;

           setLayout(new FlowLayout(FlowLayout.LEFT));
           add(panel);
           validate();
           panel.validate();
       }});
   }

   //(Snip)
}

If I use JButton and JTextField and JLabel the items do not show up until i interact with them(click on the text field, mouse over for the button, I cant get the label to show up) and if I use the normal versions I get the ugly gray backgrounds around the labels.

Can anyone help show me what I am doing wrong to get everything working properly.

EDIT:
Changing from Applet to JApplet did not solve the problem.

EDIT2:
added other methods.

EDIT3:
Cursor now starts in the user name box but everything is still invisible until interacted with. Updated code with all of the latest suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷迭香的记忆 2024-10-25 05:22:57

您正在覆盖 paint 方法,而没有在其中执行任何操作。这会导致小程序的后代根本不会被绘制。

在 Swing 中,您通常应该从不覆盖 paint() 方法,仅覆盖 paintComponent() 方法(并且通常会调用 super .paintComponent(...) 某处。)

在您的小程序中,看起来您根本不需要 Paint 方法,只需将其删除即可。

You are overwriting the paint method without doing anything in there. This causes that the descendants of the applet are not drawn at all.

In Swing, you should usually never overwrite the paint() method, only the paintComponent() method (and there usually call super.paintComponent(...) somewhere.)

In your applet it looks like you don't need the paint method at all, simply delete it.

长不大的小祸害 2024-10-25 05:22:57
public class RdpApplet extends Applet {
   JButton Connect;

不要将 Swing 与 AWT 混合使用。使用 JApplet。

public class RdpApplet extends Applet {
   JButton Connect;

Don't mix Swing with AWT. Use a JApplet.

゛时过境迁 2024-10-25 05:22:57

也许,当你完成添加所有内容后,在其中添加一个 validate();

http://download.oracle .com/javase/6/docs/api/java/awt/Container.html#validate%28%29

Perhaps, when you're done adding everything, throw a validate(); in there?

http://download.oracle.com/javase/6/docs/api/java/awt/Container.html#validate%28%29

七分※倦醒 2024-10-25 05:22:57

在 EDT 上不会调用 Applet.init()。因此,您需要将 Swing 组件的构造包装在 SwingUtilities.invokeLater() 中,以确保它们在 EDT 上运行。

public void init() {
    SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            // Init code goes here
        }});
}

The Applet.init() does not get called on the EDT. So, you need to wrap the construction of Swing components in a SwingUtilities.invokeLater() to ensure they run on the EDT.

public void init() {
    SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            // Init code goes here
        }});
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文