网格背景图像

时间:2015-05-03 19:50:11

标签: java eclipse swing layout

我目前有一个JFrame,它有许多不同的摇摆组件,如JButtons和JLabels。我可以添加背景图像并将按钮保留在图像前面吗?我已经找了其他的例子,但由于某些原因它们不适用于我的网格布局

        package Test1;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;


public class test {



private static JFrame SplashScreen;
private static JButton button1;
private JLabel splashImage;



private GridBagLayout frameLayout;
private GridBagConstraints constraints;

//method that add components to the jframe
private void addComponent( JFrame SplashScreen, Component component, int row, int column, int width, int height)
{
    constraints.gridx = column; //set gridx
    constraints.gridy = row; //set gridy
    constraints.gridwidth = width; //set gridwidth
    constraints.gridheight = height; //set gridheight
    frameLayout.setConstraints( component, constraints); //set constraints
    SplashScreen.getContentPane().add(component); //add component
}

public test() {


    SplashScreen = new JFrame("SplashScreen");
    frameLayout = new GridBagLayout();
    constraints = new GridBagConstraints();
    SplashScreen.getContentPane().setLayout(frameLayout);

    constraints.weightx = 1; //can grow taller
    constraints.weighty = 1; //can grow wider

    button1 = new JButton("This is a button");  
    addComponent(SplashScreen, button1, 1, 1, 1, 1); //adds the button to the Jframe SplashScreen

    splashImage = new JLabel(new ImageIcon("/Backgroundtests/src/Test1/leagueIMG.png"));
    addComponent(SplashScreen, splashImage, 0,0,1,1);




    SplashScreen.setSize(960,540);  //sets the size of the window
    SplashScreen.setResizable(false);
    SplashScreen.setVisible(true);  //sets the panel visibility to true
    SplashScreen.setLocationRelativeTo(null);
    SplashScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




    button1.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent argO)
        {
            //new Window(); 
            SplashScreen.setVisible(false); //closes the window after the button has been clicked so it doesnt run in the background
        }

    });
}

}

1 个答案:

答案 0 :(得分:1)

建议:

  • 创建一个扩展JPanel的类
  • 使用Graphics方法paintComponent(Graphics g)
  • 在此类的g.drawImage(myImage, 0, 0, null)方法中绘制图像
  • 不要忘记在paintComponent覆盖中首先调用super.paintComponent(g)方法。
  • 如果需要,将此JPanel的布局设置为GridBagLayout。
  • 添加到GridBagLayout时,创建一个GridBagConstraints对象,并使用约束对象添加组件:add(myComponent, myGridBagConstraint)
  • 如果您在此绘图JPanel上添加另一个JPanel,请确保添加的JPanel不是通过addedJPanel.setOpaque(false)不透明的。这样,容器JPanel中显示的图像将通过添加的JPanel显示。

例如,下面的代码创建了这个GUI:

Background Image in GUI

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.io.IOException;
import java.net.URL;

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

public class ShowBackgroundImage {
   public static final String IMAGE_PATH = "https://duke.kenai.com/glassfish/GlassFishMedium.jpg";

   private static void createAndShowUI() {
      Image image = null;
      try {
         URL url = new URL(IMAGE_PATH);
         image = ImageIO.read(url);
         // JLabel label = new JLabel(new ImageIcon(image));
         ImagePanelA imagePanel = new ImagePanelA(image);

         JFrame frame = new JFrame("Big Duke Image");
         frame.getContentPane().add(imagePanel);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
      } catch (IOException e) {
         e.printStackTrace();
      }

   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

@SuppressWarnings("serial")
class ImagePanelA extends JPanel {
   private Image image;

   public ImagePanelA(Image image) {
      this.image = image;
      setLayout(new GridBagLayout());

      add(new JButton("Monday"), createGbc(0, 0, 1, 1));
      add(new JButton("Tuesday"), createGbc(1, 0, 1, 1));
      add(new JButton("Wednesday"), createGbc(0, 1, 1, 1));
      add(new JButton("Thursday"), createGbc(1, 1, 1, 1));
   }

   private GridBagConstraints createGbc(int x, int y, int w, int h) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = w;
      gbc.gridheight = h;

      gbc.weightx = 0.0; // bunches stuff in center in x orientation
      gbc.weighty = 0.0; // bunches stuff in center in y orientation
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = new Insets(5, 5, 5, 5);

      return gbc;
   }

   @Override
   public Dimension getPreferredSize() {
      Dimension superSize = super.getPreferredSize();
      if (isPreferredSizeSet() || image == null) {
         return superSize;
      }
      int prefW = Math.max(image.getWidth(null), superSize.width);
      int prefH = Math.max(image.getHeight(null), superSize.height);
      return new Dimension(prefW, prefH);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (image != null) {
         g.drawImage(image, 0, 0, null);
      }
   }
}