java - 如何删除 "fullscreenmode"中的这个栏?

标签 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 或者是否与未装饰的框架有关。

原因是什么?如何删除该栏? 提前谢谢

最佳答案

不要使用setBounds。这完全是错误的。正确的方法是使用适当的布局管理器。另外,如果使用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);
    }

}

关于java - 如何删除 "fullscreenmode"中的这个栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28746974/

相关文章:

java - 在不同的类中运行 JButton ActionListener

java - WebView 中视频的全屏按钮不起作用

iphone - 我可以避免在 iPhone 或 android 上使用 HTML5 的原生全屏视频播放器吗?

java - 错误 :Kotlin:Out-projected type 'JComboBox<*>' prohibits the use of '@BeanProperty public open fun setModel(p0: ComboBoxModel<E! >!)

java - 使用延迟加载恢复 JTree 中的扩展/选择状态

java - Spring webflow,仅当语句为 true 时才传输

Java - 在 JPanel 中访问 JLabel

javascript - 使用带有 Firefox 插件的 javascript 切换全屏模式

java - 我可以阻止 javac 从我们的第三方 jar list 访问类路径吗?

java - 排除正则表达式中的匹配字符