如何在我的Java框架中实现图片

时间:2017-04-07 12:16:38

标签: java

我不知道如何在我的画面中实现图片。

我不使用布局管理器而是使用绝对定位。

我从互联网上获得了这些代码,所以我真的不明白 但是我还在学习它。

也许你们中的一些人可以帮助我。提前谢谢。

import java.awt.Container;
import java.awt.Insects;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;

public class sssaa {

    public static void addComponentsToPane(Container pane) {

    pane.setLayout(null);

    // Buttons

    JButton Burger = new JButton("Burger");
    Burger.setFocusPainted(false);
    JButton Beilagen = new JButton("Beilagen");
    Beilagen.setFocusPainted(false);
    JButton Salate = new JButton("Salate");
    Salate.setFocusPainted(false);
    JButton Specials = new JButton("Specials");
    Specials.setFocusPainted(false);
    JButton Tagesgericht = new JButton("Tagesgericht");
    Tagesgericht.setFocusPainted(false);
    JButton Aktionsangebot = new JButton("Aktionsangebot");
    Aktionsangebot.setFocusPainted(false);
    JButton Getränke = new JButton("Getränke");
    Getränke.setFocusPainted(false);



    pane.add(Burger);
    pane.add(Beilagen);
    pane.add(Salate);
    pane.add(Specials);
    pane.add(Tagesgericht);
    pane.add(Aktionsangebot);
    pane.add(Getränke);

    //absolute positioning

    Insets insets = pane.getInsets();
    Dimension size = Burger.getPreferredSize();
    Burger.setBounds(20 + insets.left, 50 + insets.top,
                 size.width + 100, size.height + 50);
    size = Beilagen.getPreferredSize();
    Beilagen.setBounds(20 + insets.left, 130 + insets.top,
                 size.width + 90, size.height + 50);
    size = Salate.getPreferredSize();
    Salate.setBounds(20 + insets.left, 210 + insets.top,
                 size.width + 103, size.height + 50);
    size = Burger.getPreferredSize();
    Specials.setBounds(20 + insets.left, 290 + insets.top,
                 size.width + 100, size.height + 50);
    size = Beilagen.getPreferredSize();
    Tagesgericht.setBounds(20 + insets.left, 370 + insets.top,
                 size.width + 90, size.height + 50);
    size = Salate.getPreferredSize();
    Aktionsangebot.setBounds(20 + insets.left, 450 + insets.top,
                 size.width + 103, size.height + 50);
    size = Salate.getPreferredSize();
    Getränke.setBounds(20 + insets.left, 530 + insets.top,
                 size.width + 103, size.height + 50);


    } 

    private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("AbsoluteLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     


    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    //Size and display the window.
    Insets insets = frame.getInsets();
    frame.setSize(1200 + insets.left + insets.right,
                  900 + insets.top + insets.bottom);
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    }); 
    }        
}

1 个答案:

答案 0 :(得分:0)

使用JPanel,以便您有位置来展示您的图片。

JImageComponent myImage = new JImageComponent(insertYourImageHere);

imagePanel.add(myImage);

您也可以使用JLabel存储图像。

frame.add(new JLabel(new ImageIcon("Path to your image")));