将背景图像添加到JPanel [英] Adding a background Image to JPanel
本文介绍了将背景图像添加到JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到关于此问题的任何线程的错误。我只是为我的JPanel添加背景。
I get error's with any threads I could find about this issue. Pretty much I just was to add a background to my 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& ;;它不起作用。我做错了吗?
The paint method is from another thread here on stackoverflow & It doesn't not work. Am I doing it wrong or?
推荐答案
JFrame
没有 paintComponent
方法。您需要将其包装在 JPanel
中,然后将该面板添加到 JFrame
JFrame
has no paintComponent
method. You need to wrap it in a JPanel
then add that panel to the 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
的子类型,它是 容器
paintComponent
is a method from JComponent
. JFrame
is not a subtype of JComponent
, it is a Container
此外,请参阅 JPanel javadoc 和 JFrame javadoc
这篇关于将背景图像添加到JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文