gpt4 book ai didi

java - 添加背景图像到 JPanel

转载 作者:行者123 更新时间:2023-12-01 18:40:04 25 4
gpt4 key购买 nike

我发现有关此问题的任何线程都出现错误。我几乎只是想给我的 JPanel 添加背景。

我的代码:

    package org.client;

import java.awt.EventQueue;
import java.awt.Graphics;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
*
* @author Ryan Taubert || Dev.Ryanj
* @version 0.1
*
*/

public class Main extends JFrame {
private static final long serialVersionUID = -7729008412395425144L;
private BufferedImage img;

private static double APP_VERSION = 0.1;
private static String APP_NAME = "Launcher ~ Version: "+APP_VERSION;

private JPanel jp;
private JTextField jUsername;
private JTextField jPassword;

/**
* Width/Height
*/
private int x = 1280, y = 720;


/**
* Launch the Application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
System.out.println("System: [Starting Application: "+APP_NAME+"]");
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Launch Panel
*/
public Main() {
setTitle("Ryan's JLauncher"+APP_NAME);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, x, y);
jp = new JPanel();
try {
img = ImageIO.read(new File("C:\\Users\\Ryan T\\Desktop\\wNSE6p7.jpg"));
} catch(IOException e) {
e.printStackTrace();
}
//jp.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(jp);
jp.setLayout(null);

/**
* ``TextFields``
*/
//username
jUsername = new JTextField(10);
jUsername.setBounds(6, 30, 50, 20);
jUsername.setOpaque(false);
jUsername.setBorder(null);
jp.add(jUsername);

//password
jPassword = new JTextField(10);
jPassword.setBounds(6, 30, 50, 20);
jPassword.setOpaque(false);
jPassword.setBorder(null);
jp.add(jPassword);

/**
* ``Labels``
*/
//username
JLabel username = new JLabel("Username");
username.setBounds(15, 11, 50, 14);
jp.add(username);

//password
JLabel password = new JLabel("Password");
password.setBounds(15, 11, 50, 14);
jp.add(password);


/**
* Log Button
*/
JButton login = new JButton("Log In");
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleStartClient();
}
});
login.setBounds(1, 60, 400, 30);
jp.add(login);

/**
* Create New Account Button
*/
JButton create = new JButton("Create New Account");
create.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleStartClient();
}
});
create.setBounds(1, 120, 400, 30);
jp.add(create);

}

protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}

private void handleStartClient() {

}

}

paint 方法来自 stackoverflow 上的另一个线程,它不起作用。是我做错了吗?

最佳答案

JFrame 没有 paintComponent 方法。您需要将其包装在 JPanel 中,然后将该面板添加到 JFrame

public class Main exends JFrame{
MyPanel panel;
private BufferedImage img;

public Main(){
try {
img = ImageIO.read(new File("C:\\Users\\Ryan T\\Desktop\\wNSE6p7.jpg"));
} catch(IOException e) {
e.printStackTrace();
}
panel= new MyPanel();
add(panel);
}

private class MyPanel extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
}

paintComponent是来自 JComponent 的方法。 JFrame 不是 JComponent 的子类型,它是 Container

另请参阅JPanel javadocJFrame javadoc

关于java - 添加背景图像到 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20299848/

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