gpt4 book ai didi

java - JLabel 图像不会显示在 JFrame 上

转载 作者:行者123 更新时间:2023-12-01 18:33:51 26 4
gpt4 key购买 nike

我使用 JFrame 为程序创建了一个Login 表单。它将显示所有文本标签、输入字段、按钮和其他 GUI 元素,但由于某种原因,它不会显示我的图像文件(图像 "mm.png" 存储在项目的父目录中) .

我一定做错了什么。也许有人可以帮助我。

我的代码如下。

非常感谢。

import java.io.*;
import java.net.*;
import java.awt.*;//contains layouts, buttons etc.
import java.awt.event.*; //contains actionListener, mouseListener etc.

import javax.swing.*; //allows GUI elements

public class Login extends JFrame implements ActionListener, KeyListener {

private JLabel usernameLabel = new JLabel("Username/Email:");
private JLabel userPasswordLabel = new JLabel("Password:");
public JTextField usernameField = new JTextField();
private JPasswordField userPasswordField = new JPasswordField();
private JLabel status = new JLabel("Status: Not yet logged in.");

private JButton loginButton = new JButton("Login");
private JButton registerButton = new JButton("New User");

public Login() {
super("Please Enter Your Login Details...");// titlebar
setVisible(true);
setSize(400, 260);
this.setLocationRelativeTo(null); // places frame in center of screen
this.setResizable(false); // disables resizing of frame
this.setLayout(null); // allows me to manually define layout of text
// fields etc.

ImageIcon icon = new ImageIcon("mm.png");
JLabel label = new JLabel(icon);

this.add(usernameLabel);
this.add(userPasswordLabel);
this.add(usernameField);
this.add(userPasswordField);
this.add(loginButton);
this.add(registerButton);
this.add(status);

usernameLabel.setBounds(30, 100, 120, 30); // (10, 60, 120, 20);
userPasswordLabel.setBounds(30, 125, 80, 30);// (10, 85, 80, 20);
usernameField.setBounds(150, 100, 220, 30);
userPasswordField.setBounds(150, 125, 220, 30);
loginButton.setBounds(150, 180, 110, 25);
registerButton.setBounds(260, 180, 110, 25);
status.setBounds(30, 210, 280, 30);
status.setForeground(new Color(50, 0, 255)); // sets text colour to blue

loginButton.addActionListener(this);
registerButton.addActionListener(this);
registerButton.setEnabled(false);
userPasswordField.addKeyListener(this);

}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String userName = usernameField.getText();
String password = userPasswordField.getText();
if (userName.equals("mick") && password.equals("mick")) {
status.setText("Status: Logged in.");
this.setVisible(false);
new Client("127.0.0.1").startRunning();
} else {
status.setText("Status: Password or username is incorrect.");
status.setForeground(new Color(255, 0, 0)); // changes text
// colour to red
}
}

}

}

最佳答案

  1. 不要使用null布局。 Swing 被设计为与布局管理器一起使用。像素完美布局是现代 GUI 设计中的一种幻想。您无法控制字体的可用性或它们在各个系统上的呈现方式。布局管理器在确定组件如何协同工作之间的关系时无需进行猜测
  2. 仅在完成框架内容的构建后才调用 setVisible。如果您需要在框架可见后添加/删除组件,则需要调用revalidate

看看Laying Out Components Within a Container了解更多详情

关于java - JLabel 图像不会显示在 JFrame 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com