【发布时间】:2014-01-29 13:17:46
【问题描述】:
我在将多个图像放在 JFrame 上时遇到问题。我已经在 JFrame 上添加了一张图片作为主要背景。但是,当我尝试为我的程序徽标添加另一个图像时,它并没有显示出来。有人可以帮帮我吗?谢谢。
附:我在我的 JFrame 中使用了 Container 类。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.Container;
public class logIn extends JFrame
{
Random rand = new Random();
int n2 = (int) (1+Math.random()*255);
int n1 = rand.nextInt(n2);
int n3 = rand.nextInt(n2);
int n4 = rand.nextInt(n2);
Color color = new Color(n1,n3,n4);
JLabel image = new JLabel (new ImageIcon("space2.png"));
//JLabel image2 = new JLabel (new ImageIcon("login.png"));
JLabel userName = new JLabel("Username");
JLabel passWord = new JLabel("Password");
JTextField user = new JTextField(10);
JTextField pass = new JTextField(10);
JLabel myDog = new JLabel(new ImageIcon("space.jpeg"));
public static void main (String args[])
{
new logIn();
}
public logIn()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.add(image);
image.setBounds(0,0,1366,768);
JLabel image2 = new JLabel (new ImageIcon("login.png"));
c.add(image2);
image2.setBounds(2000,2000,2000,2000);
//c.add(image2);
//image2.setBounds(10,10,250,250);
//c.add(userName);
//userName.setLayout(null);
//userName.setBounds(50,100,100,50);
setVisible(true);
setSize(1366,768);
setLayout(new BorderLayout());
add(myDog);
myDog.setLayout(null);
}
public void paint (Graphics g)
{
Image a = Toolkit.getDefaultToolkit().getImage("login.png");
g.drawImage(a,0,0,1366,768,this);
super.paint(g);
setVisible(true);
}
}
【问题讨论】:
-
以及您的问题的详细信息。现在你已经问了你的问题,好像我们可以看到你的目标是什么,可以看到它是如何工作的,并且可以看到未显示的代码,所以任何人都可以猜测你到底想要做什么以及它是怎么做的工作。
-
好的。我会把它放在答案中。
-
“在答案中”?不,请通过编辑来更新您的问题。仅在答案部分发布实际答案。
-
我已经上传了代码。抱歉,我是新来的。
-
您可以在布局中使用多个
JPanel对象,并在每个对象中插入一张图片。