在 Java 中的 JFrame 上设置背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19602810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting a background image on a JFrame in java
提问by Andrew Stevenson
I am new to trying to create a java interface and am wanting to create one as part of a college project.
我是尝试创建一个 java 接口的新手,我想创建一个作为大学项目的一部分。
at the moment i'm still just working on the opening interface but cant seem to set a back ground image to my frame. Ive watched all the youtube videos i could find and looked through all the forums but still nothing seems to work.
目前我仍然只是在打开界面上工作,但似乎无法为我的框架设置背景图像。我看过我能找到的所有 youtube 视频,并浏览了所有论坛,但似乎仍然没有任何效果。
all the examples ive seen havent had buttons and textboxes already put on it so im not sure if this is the issue but in my 'try and catch' i just constantly get 'image doesn't exist' even though i have put the image with the correct file name in.
我看到的所有示例都没有按钮和文本框,所以我不确定这是否是问题,但在我的“尝试和捕捉”中,我只是不断得到“图像不存在”,即使我已经将图像放在中的正确文件名。
like i said i am new to working with interfaces so for all i know it could be really simple or i havent really messed it all up but if someone can help out it would be really apreciated.
就像我说的,我是使用接口的新手,所以我知道它可能非常简单,或者我还没有真正把它搞砸,但如果有人能帮忙,那将是非常感激的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
public class CoopWelcome extends JFrame {
private ImageIcon image1;
private JButton b1;
private JTextField userName;
private static String password = "";
private JPanel backGround;
CoopWelcome() {
setLayout(new FlowLayout());
//creating username textbox
userName = new JTextField("");
userName.setText("Username");
userName.setForeground(Color.GRAY);
userName.setColumns(10);
getContentPane().add(userName);
//creating password textbox
JPasswordField passWord = new JPasswordField(10);
passWord.setEchoChar('*');
passWord.addActionListener(new AL());
getContentPane().add(passWord);
//adding the button and the label to the panel
b1 = new JButton("something");
getContentPane().add(b1);
//getting the image and displaying to the label
}
public static void main(String[] Args) {
//Creating the interface
CoopWelcome gui = new CoopWelcome();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.pack();
gui.setTitle("The Co-operative");
try {
gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));
} catch (IOException e) {
System.out.println("image doesn't exist");
}
}
static class AL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JPasswordField input = (JPasswordField) e.getSource();
char[] passy = input.getPassword();
String p = new String(passy);
if (p.equals(password)) {
JOptionPane.showMessageDialog(null, "Correct");
} else {
JOptionPane.showMessageDialog(null, "Incorrect");
}
}
}
}
采纳答案by camickr
gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));
gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));
Reasonable approach, but the problem is that by default a JLabel doesn't use a layout manager to you can't easily add components to it. Try:
合理的方法,但问题是默认情况下 JLabel 不使用布局管理器,因此您无法轻松地向其中添加组件。尝试:
JLabel background = new JLabel(...);
background.setLayout( new BorderLayout() );
gui.setContentPane( background );
回答by mkaminsky
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setContentPane(new JLabel(new ImageIcon("someImage.png")));
f.setSize(300,300);
f.setSize(301,301); //just a refresh
回答by Andrew Stevenson
The problem is now solved.
现在问题解决了。
I basically completely redid my JFrame and labels and stuff and managed to get it to work. probably still terribly formatted but hey at least it works now and it is now a hell of a lot easier for future to know where i might be going wrong.
我基本上完全重做了我的 JFrame 和标签和东西,并设法让它工作。可能格式仍然很糟糕,但嘿,至少现在它可以工作了,现在要知道我可能出错的地方要容易得多。
CoopWelcome() {
setLayout (new FlowLayout());
//username field
userName = new JTextField("");
userName.setText("Username");
userName.setForeground(Color.GRAY);
userName.setColumns(10);
getContentPane().add(userName);
//password field
JPasswordField passWord = new JPasswordField(10);
passWord.setEchoChar('*');
passWord.addActionListener(new AL());
getContentPane().add(passWord);
//button
b1 = new JButton("something");
getContentPane().add(b1);
//frame
setSize(500,600);
setVisible(true); setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("img.jpg"));
add(background);
background.setLayout(new FlowLayout());
}
public static void main(String[] Args){
new CoopWelcome();
}