JComponents not showing up with picture background?

前端 未结 2 1256
谎友^
谎友^ 2020-11-22 02:24

My components are not showing up. How do I fix this?

Code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener         


        
2条回答
  •  庸人自扰
    2020-11-22 02:50

    window.setVisible(true); should be invoked only after all the components have been added to the frame.
     void logini() throws IOException {
            JFrame window = new JFrame("Login");
            JPanel mainp = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            window.add(mainp);
    
            BufferedImage myPicture = ImageIO.read(new File("c:\\bgd.png"));
            JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
            mainp.add(picLabel, c);
    
            c.gridx = 0;
            c.gridy = 1;
            gusername = new JTextField();
            gusername.setText("Username");
            mainp.add(gusername, c);
    
            c.gridx = 0;
            c.gridy = 2;
            gpassword = new JTextField();
            gpassword.setText(" password ");
            mainp.add(gpassword, c);
    
            c.gridx = 0;
            c.gridy = 3;
            JButton login = new JButton("Login");
            mainp.add(login, c);
    
            login.addActionListener(this);
            login.setActionCommand("ok");
    
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setSize(500, 250);
            window.setResizable(false);
            window.setVisible(true);
        }
    

提交回复
热议问题