如何删除JFrame的边境,以让图像按边缘(How to Remove JFrame Border

2019-08-01 04:43发布

我有一个朋友做的我做了,这样就不会显得那么普通程序的背景下,我想放置的图像将是使一个JLabel,用图像填充它,并将其设置到最好的办法屏幕尺寸。 这工作得很好,除了周围有JFrame的一个边陲,我不能让JLabel以触摸框的边缘。 思考? 我附上一张照片。

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);'

Answer 1:

为了扩大Eng.Fouad的答案,你要使用drawImage(...)方法,它有6个参数,图片,X和Y位置,图像的宽度和高度,图像观察者,并从内部绘制它像这样一个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。 为什么,如果你不想要这个边界是存在?



Answer 2:

这个问题是不是与JFrame ,问题是你的代码。 我们可以猜测什么是错的花我们自然的余生,或者你可以张贴一些示例代码。

现在是你的,我们可以不断尝试在你错误的猜测后抛出错误的猜测,折腾我们所有人,也可以帮助我们来帮助你...

这里有两个例子我做到了。 第一种使用一个JLabel作为用于主内容JPanel ,其中所述子组件被放置在它。 尼斯和简单。

第二使用定制JPanel其描绘所述图像到所述组件的背景。 然后我用这个来代替框架内容窗格。 这是一个涉及多一点,但是它有被容易地更新的额外好处(更换内容窗格不会影响程序的其余部分)

实施例1: JLabel作为背景

public class TestBackground {

    public static final String BACKGROUND_PATH = "/Volumes/Macintosh HD2/Dropbox/MT015.jpg";

    public static void main(String[] args) {
        new TestBackground();
    }

    public TestBackground() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setLayout(new BorderLayout());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new LabelPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });

    }

    protected class LabelPane extends JPanel {

        public LabelPane() {

            BufferedImage bg = null;
            try {
                bg = ImageIO.read(new File(BACKGROUND_PATH));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            JLabel label = new JLabel(new ImageIcon(bg));
            setLayout(new BorderLayout());

            add(label);

            label.setLayout(new GridBagLayout());

            JLabel lblMessage = new JLabel("Look at me!");
            lblMessage.setForeground(Color.WHITE);
            lblMessage.setFont(lblMessage.getFont().deriveFont(Font.BOLD, 48));

            label.add(lblMessage);

        }
    }
}

实施例2:图像用作背景,替换内容窗格...

public class TestBackground {
    public static final String BACKGROUND_PATH = "/Volumes/Macintosh HD2/Dropbox/MT015.jpg";

    public static void main(String[] args) {
        new TestBackground();
    }

    public TestBackground() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setLayout(new BorderLayout());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new BackgroundPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected class BackgroundPane extends JPanel {

        private BufferedImage bg = null;

        public BackgroundPane() {
            try {
                bg = ImageIO.read(new File(BACKGROUND_PATH));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            setLayout(new GridBagLayout());

            JLabel lblMessage = new JLabel("Look at me!");
            lblMessage.setForeground(Color.WHITE);
            lblMessage.setFont(lblMessage.getFont().deriveFont(Font.BOLD, 48));

            add(lblMessage);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1153, 823);
        }

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


Answer 3:

作为替代方案,可以重写方法paintComponent(Graphics g)的的JPanel (的contentPane)和使用drawImage()的图形对象g如在本实施例中 。



Answer 4:

你有没有尝试过的JFrame功能setUndecorated()?



Answer 5:

使框架未装饰。 frame.setUndecorated(true)

如果你想让它移动,你可以使用ComponentMover的Java2S的。

之前,请务必它是可见的,它是未经修饰的。

接下来,使用setContentPane(new JLabel(new ImageIcon("myimage.jpg")));

后,您可以添加内容如常。



文章来源: How to Remove JFrame Border as to Let an Image Touch The Edge