开发者

Swing applet componets are invisable untill interacted with

开发者 https://www.devze.com 2023-02-11 03:14 出处:网络
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 comp

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();
    开发者_C百科   }});
   }

   //(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.


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.


public class RdpApplet extends Applet {
   JButton Connect;

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


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


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
        }});
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号