如何在“fullscreenmode”中删除此栏?

时间:2015-02-26 15:56:44

标签: java swing fullscreen

我的问题是我的屏幕顶部有一个小栏,我想删除(我希望图片全屏)。我不确定这个酒吧来自哪个来源。

picture

到目前为止,这是我的代码:

    lpane = new JLayeredPane();
    lpane.setBackground(Color.BLACK);
    panelBlue = new JPanel();
    panelGreen = new JPanel();
    frame.add(lpane);
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    frame.setUndecorated(true);
    lpane.setBounds(0, 0, 1920, 1080);
    BufferedImage background = null;
    BufferedImage title = null;
    try{
        background = javax.imageio.ImageIO.read(getClass().getResource("resources/background.jpg"));
    }catch(IOException ex){
        sendErrorMessage("Picture couldn't be loaded"); //custom Errormessage Method
    }
    JLabel picLabel = new JLabel(new ImageIcon(background));
    ImageIcon buttonbackground = new ImageIcon(flames);
    panelBlue.add(picLabel);
    panelBlue.setBounds(0, 0, 1920, 1080);
    panelBlue.setOpaque(true);

    frame.pack();
    frame.setVisible(true);

我不确定原因是JLabel还是与未修饰的框架有关。

原因是什么以及如何删除栏? Thx提前

1 个答案:

答案 0 :(得分:1)

请勿使用setBounds。这是完全错误的。正确的方法是使用适当的LayoutManager。另外,如果你使用pack(),之前使用setBounds是没用的。

要将框架设置为全屏,您可以使用以下任何一种方法:

  • frame.setExtendedState(frame.getExtendedState() & JFrame.MAXIMIZED_BOTH);
  • GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);

请参阅此示例说明:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;

public class TestFullScreen {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new TestFullScreen().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static class ImagePanel extends JPanel {
        private ImageIcon imageIcon;

        public ImagePanel(ImageIcon imageIcon) {
            super();
            this.imageIcon = imageIcon;
        };

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(imageIcon.getImage(), 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(imageIcon.getIconWidth(), imageIcon.getIconHeight());
        }

    }

    protected void initUI() throws MalformedURLException {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new ImagePanel(new ImageIcon(new URL("http://blog.timesunion.com/opinion/files/2011/10/brickwall.jpg"))));
        frame.setUndecorated(true);
        frame.pack();
        frame.setExtendedState(frame.getExtendedState() & JFrame.MAXIMIZED_BOTH);
        // GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);
        frame.setVisible(true);
    }

}