如何删除 JFrame 边框以让图像接触边缘

2022-01-24 00:00:00 graphics border java swing jframe

我让一个朋友为我制作的程序制作了背景,这样它看起来就不会那么简单了,我认为放置图像的最佳方法是制作一个 JLabel,用图像填充它,然后设置它到屏幕的大小.这工作得很好,除了 JFrame 周围有一个小边框,我无法让 JLabel 触摸框架的边缘.想法?我附上了一张图片.

I had a friend make a background for the program I made so that it wouldn't look so plain, and I thought the best way to place the images would be to make a JLabel, fill it with an image, and set it to the size of the screen. This worked fine, except there is a small border around the JFrame and I can't get the JLabel to touch the edges of the frame. Thoughts? I have attached a picture.

public class ProgramDriver extends JFrame {

private JPanel contentPane;
private static CardLayout cardLayout;
private JTextField addGradeN;
private JTextField addGradeD;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ProgramDriver frame = new ProgramDriver();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

//Global Variables 
...
    manager = new StateManager(gb);

    //JFrame Settings
    setTitle("Grade Book");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 656, 530);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    cardLayout = new CardLayout(0,0);
    contentPane.setLayout(cardLayout);
    setResizable(false);

    //Home Panel 
    final JPanel Home = new JPanel();
    contentPane.add(Home, "Home");
    Home.setLayout(null);

    JButton btnSeeGrades = new JButton("See Grades");
    ...

    //Grades Panel
    JPanel Grades = new JPanel();
    contentPane.add(Grades, "Grades");
    Grades.setLayout(null);'

推荐答案

为了扩展 Eng.Fouad 的答案,您需要使用带有 6 个参数的 drawImage(...) 方法、图像、x 和 y 位置、图像宽度和高度以及图像观察者,并在 JPanel 中像这样绘制它:

To expand on Eng.Fouad's answer, you'll want to use the drawImage(...) method that takes 6 parameters, image, x and y location, image width and height, and image observer, and draw it like so from within a JPanel:

g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

例如,我的sscce:

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 ExpandingImage extends JPanel {
   public static final String GUITAR = "http://duke.kenai.com/Oracle/OracleStrat.png";
   BufferedImage img;

   public ExpandingImage(String imgUrlPath) throws IOException {
      URL imgUrl = new URL(imgUrlPath);
      img = ImageIO.read(imgUrl);
   }

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


   private static void createAndShowGui() {
      ExpandingImage mainPanel;
      try {
         mainPanel = new ExpandingImage(GUITAR);
         JFrame frame = new JFrame("ExpandingImage");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().add(mainPanel);
         frame.pack();
         frame.setLocationByPlatform(true);
         frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
         frame.setVisible(true);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

编辑
我看到您在 contentPane 周围使用了 EmptyBorder.如果您不希望出现此边框,为什么?

Edit
I see that you're using an EmptyBorder around the contentPane. Why if you don't want this border to be present?

相关文章