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

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

本文介绍了如何删除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的答案,您需要使用 drawImage(...)方法,该方法需要6个参数,图像,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);

例如,我的:

For example, my 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?

这篇关于如何删除JFrame边框以让图像触摸边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 17:36