[英]JLabel image won't display on JFrame
我使用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
}
}
}
}
null
布局。 Swing旨在與布局管理器一起使用。 像素完美的布局是現代GUI設計中的一種幻覺。 您無法控制字體的可用性或在單個系統上如何呈現字體。 布局經理消除了猜測工作,從而無法確定組件之間如何協同工作 setVisible
。 如果需要在框架可見后添加/刪除組件,則需要調用revalidate
看一下在容器內布置組件的更多詳細信息
ImageIcon icon = new ImageIcon("mm.png");
JLabel label = new JLabel(icon);
但是由於某種原因它不會顯示我的圖像文件
您創建了Icon和JLabel,但看不到將標簽添加到GUI的位置。
鑒於您已經擁有的東西,問題在於您尚未添加標簽,並且尚未設置標簽范圍。 例如:
this.add(label);
label.setBounds(130, 10, 140, 80);
不過,我將引用MadProgrammer的建議-使用嵌套的布局管理器來實現所需的功能,而不是預先計算像素值。
我剛剛發現,實際上我遇到了同樣的問題,我的圖像不會顯示在 Label 上,所以我發現,當您使用(空)布局時,請確保當您使用setBounds()
function 時,確保寬度和高度必須與您使用的圖片或圖標相同。
例子
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends JFrame{
JLabel label;
ImageIcon icon;
MyFrame(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setLayout(null);
getContentPane().setBackground(Color.BLACK);
icon = new ImageIcon("icon.png"); // this icon's width and height is 126
label = new JLabel();
label.setIcon(icon);
label.setBounds(0,0,216,216); // 216 is width and height of my image
this.add(label);
this.setVisible(true);
}
}
我希望這對您有意義,並且您知道在使用setBounds
時必須傳遞圖標/圖像的寬度和高度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.