如何使用eclipse在Java中的JFrame中的特定位置添加图像?

时间:2017-09-17 12:53:16

标签: java eclipse swing jlabel

我使用GUI概念创建了学生信息页面。我想知道如何使用JLabel或任何其他方法在特定位置添加图像?我想要的是围绕整个Jframe的背景图像和在右上角的特定位置的另一个图像。我怎样才能做到这一点?

我还发现了一个使用Jlabel添加图像的代码,但是当我将布局设置为null时,它不能使用我的代码。 我找到的代码

    String path = "Image1.jpg";
    File file = new File(path);
    BufferedImage image = ImageIO.read(file);
    JLabel label = new JLabel(new ImageIcon(image));
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(label);
    f.pack();
    f.setLocation(200,200);
    f.setVisible(true);

以下是我的代码:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JTextArea; 
import javax.swing.JTextField;

public class LoginPage 
{

JFrame jf;
JLabel gender,hobbies,name_label,rollno_label,marks_label,city_label,address_label;
JTextField name_field,rollno_field,marks_field;
JRadioButton male,female;
ButtonGroup bg;
JCheckBox photography,music,sketching,coding;
JComboBox city_combo;
JTextArea adress_textarea;
JButton save, exit;
JMenuBar mbar;
JMenu file,edit,help;
JMenuItem open,save_item,edit_item,close,cut,copy,paste,find,replace,help_content,about,updates;


    public LoginPage()  //constructor
    {

    jf = new JFrame("Student Information");
    name_label = new JLabel("Student's Name");
    name_field = new JTextField();
    rollno_label = new JLabel("Student's Roll Number");
    rollno_field = new JTextField();
    marks_label = new JLabel("Student's Total Marks Achieved");
    marks_field = new JTextField();
    gender = new JLabel("Gender");
    male = new JRadioButton("Male");
    female = new JRadioButton("Female");
    bg = new ButtonGroup();
    hobbies = new JLabel("Hobbies");
    photography = new JCheckBox("Photography");
    music = new JCheckBox("Music");
    coding = new JCheckBox("Coding");
    sketching = new JCheckBox("Sketching");
    city_label = new JLabel("City");
    city_combo = new JComboBox();
    address_label = new JLabel("Residential Address");
    adress_textarea = new JTextArea();
    save = new JButton("Save");
    exit = new JButton("Exit");
    mbar = new JMenuBar();
    file = new JMenu("File");
    edit = new JMenu("Edit");
    help = new JMenu("Help");
    open = new JMenuItem("open");
    save_item = new JMenuItem("Save");
    edit_item = new JMenuItem("Edit");
    close = new JMenuItem("Close");
    cut = new JMenuItem("Cut");
    copy = new JMenuItem("Copy");
    paste = new JMenuItem("Paste");
    find = new JMenuItem("Find");
    replace = new JMenuItem("Replace");
    about = new JMenuItem("About");
    updates = new JMenuItem("Check for Updates");
    help_content = new JMenuItem("Help Content");

    }

void Display() 
{
    jf.setSize(1000, 700);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setLayout(null);
    jf.getContentPane().setBackground( Color.LIGHT_GRAY );

    name_label.setBounds(50, 50, 150, 20);
    name_field.setBounds(300, 50, 200, 20);
    rollno_label.setBounds(50, 100, 150, 20);
    rollno_field.setBounds(300, 100, 200, 20);
    marks_label.setBounds(50, 150, 200, 20);
    marks_field.setBounds(300, 150, 200, 20);
    gender.setBounds(50, 200, 100, 20);
    male.setBounds(300, 200, 80, 20);
    female.setBounds(400, 200, 80, 20);
    hobbies.setBounds(50, 250, 80, 20);
    photography.setBounds(300, 250, 100, 20);
    music.setBounds(420, 250, 80, 20);
    sketching.setBounds(500, 250, 100, 20);
    coding.setBounds(600, 250, 80, 20);
    city_label.setBounds(50, 300, 100, 20);
    city_combo.setBounds(300, 300, 100, 20);
    address_label.setBounds(50, 350, 200, 20);
    adress_textarea.setBounds(300, 350, 300, 100);
    save.setBounds(300, 500, 100, 50);
    exit.setBounds(600, 500, 100, 50);

    bg.add(male);
    bg.add(female);

    city_combo.addItem("Select City");
    city_combo.addItem("Chandigarh");
    city_combo.addItem("Kurali");
    city_combo.addItem("Mohali");
    city_combo.addItem("Panchkula");

    file.add(open);
    file.add(save_item);
    file.add(edit_item);
    file.add(close);
    edit.add(cut);
    edit.add(copy);
    edit.add(paste);
    edit.add(find);
    edit.add(replace);
    help.add(about);
    help.add(help_content);
    help.add(updates);
    mbar.add(file);
    mbar.add(edit);
    mbar.add(help);

    jf.add(name_label);
    jf.add(name_field);
    jf.add(rollno_label);
    jf.add(rollno_field);
    jf.add(marks_label);
    jf.add(marks_field);
    jf.add(gender);
    jf.add(male);
    jf.add(female);
    jf.add(hobbies);
    jf.add(music);
    jf.add(photography);
    jf.add(sketching);
    jf.add(coding);
    jf.add(city_label);
    jf.add(city_combo);
    jf.add(address_label);
    jf.add(adress_textarea);
    jf.add(save);
    jf.add(exit);

    jf.setJMenuBar(mbar);

    jf.setVisible(true);


}

public static void main(String[] args) {
    new LoginPage().Display();
}

}

2 个答案:

答案 0 :(得分:2)

  

我想要的是围绕整个Jframe的背景图片

建议:

  • 创建一个扩展JPanel的类
  • 覆盖其paintComponent方法
  • 请务必在覆盖
  • 中调用super的paintComponent方法
  • 使用g.drawImage(...)
  • 在此方法中绘制背景图片
  • 将此JPanel设为您的JFrame的contentPane或将其添加到contentPane BorderLayout.CENTER,然后将您的GUI组件添加到此JPanel
  • 确保某些组件不是不透明的,例如,在JRadioButtons上调用setOpaque(false),或许还有其他组件,以便显示背景图像。
  

和特定位置的另一张图片,如右上角。我怎样才能做到这一点?

  • 使用上面相同的JPanel,并使用drawImage(...)的重载绘制较小的图像,将图像精确地放置在您想要的位置

注意:

  • 当我为GUI创建背景图像时,我更喜欢在JPanel而不是JLabel中绘制,因为JLabel没有开箱即用作为contentPane或一个像样的容器。
  • 强烈建议您不要使用null布局和setBounds,因为这会导致gui在一个平台上看起来不错,而在另一个平台上看起来不行没有完全看到文本的JLabel,很难升级和维护。学习和使用布局管理器。
  • 您似乎正在使用多个JFrame。如果是,请阅读:The Use of Multiple JFrames: Good or Bad Practice?

例如,这是在JPanel中显示图像作为背景图像以及GUI右上部的较小图像的一种方式:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class LoginPage3 extends JPanel {
    public static final String BG_IMG_PATH = "https://upload.wikimedia.org/wikipedia/"
            + "commons/e/e9/Maesil_%28prunus_mume%29_washed_and_stemmed.jpg";
    public static final String RU_IMG_PATH = "https://upload.wikimedia.org/wikipedia/"
            + "commons/thumb/5/5b/Escudo_de_San_Pedro_de_Atacama.svg/200px-Escudo_de_San_Pedro_de_Atacama.svg.png";

    private BufferedImage backgroundImg;
    private BufferedImage rightUpperImg;

    public LoginPage3(BufferedImage bgImg, BufferedImage ruImg) {
        this.backgroundImg = bgImg;
        this.rightUpperImg = ruImg;
    }

    @Override
    public Dimension getPreferredSize() {
        if (backgroundImg == null || isPreferredSizeSet()) {
            return super.getPreferredSize();
        } else {
            int w = backgroundImg.getWidth();
            int h = backgroundImg.getHeight();
            return new Dimension(w, h);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (backgroundImg != null) {
            g.drawImage(backgroundImg, 0, 0, this);
        }
        if (rightUpperImg != null) {
            int x = getWidth() - rightUpperImg.getWidth();
            g.drawImage(rightUpperImg, x, 0, this);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        BufferedImage bg = null;
        BufferedImage ru = null;
        try {
            bg = ImageIO.read(new URL(BG_IMG_PATH));
            ru = ImageIO.read(new URL(RU_IMG_PATH));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        LoginPage3 mainPanel = new LoginPage3(bg, ru);
        JFrame frame = new JFrame("LoginPage3");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

答案 1 :(得分:1)

对于背景图片:

  • 将其加载为icon,然后使用它创建JLabel。 (请参阅您的第一个代码段)
  • JLabel设置为JFrame的内容窗格。

您需要注意的是Icon没有延伸,因此您必须拥有与JFrame(或更大)完全相同的图像。

对于给定位置的图片,JLabel创建和Icon加载过程是相同的,但在将其添加到JFrame后,您必须设置位置和大小,就像你的其他组件一样,EG致电setBounds() ......