代码之家  ›  专栏  ›  技术社区  ›  Tarunjeet Singh Salh

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

  •  0
  • Tarunjeet Singh Salh  · 技术社区  · 8 年前

    我使用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 回复  |  直到 8 年前
        1
  •  2
  •   DontKnowMuchBut Getting Better    8 年前

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

    • 创建一个扩展JPanel的类,
    • paintComponent 方法
    • g.drawImage(...)
    • 要么将这个JPanel作为JFrame的contentPane,要么将其添加到contentPane BorderLayout。然后将GUI组件添加到此JPanel
    • 确保某些组件不是不透明的,例如调用 setOpaque(false) 在你的JRadioButtons上,也许还有其他的,这样背景图像就会显示出来。

    另一个图像位于特定位置,如右上角。我怎样才能做到这一点?

    • 使用上面相同的JPanel,并使用 drawImage(...) 这正好将图像放置在您想要的位置

    笔记:

    • 强烈地 建议您不要使用 null 布局和挫折,因为这将导致gui在一个平台上看起来很好,但在另一个平台上可能不太好,其中JLabel的文本没有完全看到,很难升级和维护。学习并使用布局管理器。
    • The Use of Multiple JFrames: Good or Bad Practice?

    例如,有一种方法可以将图像作为背景图像以及较小的图像显示在GUI的右上部,所有这些都在JPanel中:

    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);
        }
    }
    
        2
  •  1
  •   Usagi Miyamoto    8 年前

    对于背景图像:

    • icon ,并创建 JLabel
    • 设置 JFrame

    你要做的就是 Icon 不会拉伸,因此您必须拥有与您的 窗口

    对于给定位置的图像 标签类 偶像 setBounds() ...