简体   繁体   English

如何在JFrame上放置多张图片?

[英]How to put multiple pictures on a JFrame?

I'm having a problem putting multiple images on a JFrame. 我在将多个图像放在JFrame上时遇到问题。 I already added a picture on the JFrame as the primary background. 我已经在JFrame上添加了图片作为主要背景。 But when I try to put another image for the logo of my program, it doesn't show up. 但是,当我尝试在程序徽标中放置其他图像时,该图像没有显示。 Can someone please help me? 有人可以帮帮我吗? thanks. 谢谢。

PS I'm using the Container class in my JFrame. PS我在我的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);
}

} }

not sure what you're trying to achieve, but if you want to have multiple images (like a key image besides the password field and an avatar besides the user name, etc) you could create multiple JLabels with the different images and add them to the panel. 不确定要实现的目标,但是如果要具有多个图像(例如,密码字段之外的键图像和用户名之外的头像等),则可以使用不同的图像创建多个JLabel并将其添加到面板。 If this is your purpose, don't override paint. 如果这是您的目的,请不要覆盖油漆。

You need to read a few tutorials on custom painting, layouts, and gui development. 您需要阅读一些有关自定义绘画,布局和gui开发的教程。

If you want to paint an image you should not load the image every repaint. 如果要绘制图像,则不应在每次重新绘制时都加载图像。 You need to read that image in ahead of time. 您需要提前阅读该图像。 You should not be calling set visible in this method either. 您也不应在此方法中调用set visible。

Any custom painting needs to be done in paintComponent instead of paint. 任何自定义绘画都需要在paintComponent中完成,而不是在paint中完成。

Read this: http://docs.oracle.com/javase/tutorial/uiswing/painting/ 请阅读以下内容: http : //docs.oracle.com/javase/tutorial/uiswing/painting/

It sounds like what you really want to do is create a JFrame with a panel that has a background image - and then use swing and layouts to add other components or panels with images on top of that. 听起来您真正想做的就是创建一个JFrame,该JFrame的面板上有背景图像-然后使用swing和layouts添加其他组件或面板上的图像。

So you would have a JPanel as the content pane with paintComponent overridden to only paint an image. 因此,您将拥有一个JPanel作为内容面板,其中paintComponent被覆盖以仅绘制图像。 Then build new components from there and place them using a layout manager. 然后从那里构建新组件,并使用布局管理器放置它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM