JComponents在有图片背景时未显示出来?

7 浏览
0 Comments

JComponents在有图片背景时未显示出来?

我的组件没有显示出来。我该如何修复这个问题?

代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class login implements ActionListener{
    JTextField gusername;
    JTextField gpassword;
    static String username;
    static String password;
    
    void logini() throws IOException {
        JFrame window = new JFrame("Login");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(300, 250);
        window.setResizable(false);
        window.setVisible(true);
        
        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");
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("ok")){
            try {
                this.username = (gusername.getText());
                this.password = (gpassword.getText());
                System.out.println("0");
            }
            catch(NumberFormatException ex){
                System.out.println("ERROR: Could not preform function: 7424");
            }
        }
    }
}

结果:

0
0 Comments

JComponents not showing up with picture background? 这个问题的出现原因是在调用window.setVisible(true)之前,所有的组件都没有添加到窗口中。解决方法是将window.setVisible(true)的调用放到所有组件添加完成之后。

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

0
0 Comments

问题出现的原因:

在第一个例子中,问题可能是因为在CustomPanel中绘制的图片没有显示出来。可能的原因是图片的路径不正确,或者图片加载时发生了错误。

解决方法:

- 确保图片的路径正确。在代码中使用getClass().getResource(path)来获取图片的URL路径。

- 确保图片的加载过程中没有发生错误。可以在try-catch块中捕获IOException来处理错误。

在第二个例子中,问题可能是因为使用JLabel作为图片的容器时图片没有显示出来。可能的原因是图片的URL路径不正确,或者图片加载时发生了错误。

解决方法:

- 确保图片的URL路径正确。在代码中使用new URL(path)来创建图片的URL对象。

- 确保图片的加载过程中没有发生错误。可以在try-catch块中捕获MalformedURLException来处理错误。

以上是问题出现的可能原因和解决方法,希望对您有帮助。

0