簡體   English   中英

如何讓 JPanel 在“Windows 裝飾”保持開啟的情況下填充完整的 JFrame(同時處於最大化狀態)?

[英]How to let the JPanel fill the complete JFrame (while in maximized state) with the "Windows Decoration" kept ON?

我的全圖 window

JFrame灰色邊框可見

問題:

我希望我的應用程序在默認情況下以全屏(最大化)方式運行(但默認情況下最大分辨率因筆記本電腦而異)。 我正在使用根據JPanel上用戶計算機的大小和寬度縮放的背景圖像。 但是隨着裝飾和調整大小功能的“開啟”, JPanel並沒有完全填滿JFrame

我希望我的應用程序:

  1. 允許用戶根據用途調整大小,圖像也隨之縮放
  2. 在最大化視圖中(默認情況下: setExtendedState(JFrame.MAXIMIZED_BOTH); )圖像覆蓋整個JFrame (注意:無論是否使用JFrame ,對適用於所有設備的任何解決方案都很滿意。)
  3. 如果可能的話,我的組件也會調整大小

我正在使用JFrame處於“絕對布局”。 我在絕對布局和BorderLayout (不起作用)上都嘗試過JPanel ,嘗試過pack() (也不起作用),屏幕尺寸的jPanel1.setSize(WIDTH,HEIGHT)也不起作用。 JPanel的布局設置為 NULL 也沒有解決問題:(

Sign_Up.java ( JFrame )

public class Sign_Up extends javax.swing.JFrame {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    double width = screenSize.getWidth();
    double height = screenSize.getHeight();

    /**
     * Creates new form Sign_Up
     */
    public Sign_Up() {
        initComponents();
        Seticon();
        btnSave.setEnabled(false);//save button
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        //setSize(1920,1080);
        setLocationRelativeTo(null);//makes aligned at center of screen
        //jPanel1.setSize((int)width, (int)height);
        //pack();
    }

PanelScale.java

public class PanelScale extends JPanel {
    Image iconbg;

    public PanelScale() {
        iconbg = new ImageIcon(getClass( ).getResource("/images/largesignup.png")).getImage( );
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D gd = (Graphics2D)g.create();
        gd.drawImage(iconbg, 0, 0, getWidth(), getHeight(), this);
        gd.dispose();
    }
}

JPanel中的自定義創建代碼: new Panel.PanelScale();

我發現唯一可行的是將JPanel顯式拉伸到JFrame上的某個額外高度(在 NetBeans 中),但這導致我的應用程序 window 不在屏幕中央,而是向右移動。

將超過 JFrame 的 Jpanel 拉伸到更高的高度

但是當我嘗試使用

setsize(new Dimension(width, height+40));

對於JPanel ,它不起作用。

我也可以使用JLabel完成此操作,但我希望我的圖像在任何設備(更大或更小的筆記本電腦,如 1920x1080 分辨率、1280x720 等)上以最大化或調整大小的視圖工作時覆蓋JFrame到整個區域

如果提供任何解決方案,即使有或沒有JPanel的替代方法,我將不勝感激。 即使該應用程序能夠在任何設備上全屏運行,圖像覆蓋它,我也會感到滿意,暫時可以犧牲調整大小功能

預期的

BorderLayout (默認設置為JFrame )將自動執行您想要的操作,然后您只需根據需要調整背景的大小/位置。

null (“絕對”)布局確實不是一個好主意。

首先看看如何使用 BorderLayout 我還建議查看Working with ImagesGraphics2D的 JavaDocs,它們將為您提供有關如何根據需要調整圖像大小的信息

可運行的例子...

在此處輸入圖像描述

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new BackgroundPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class BackgroundPane extends JPanel {

        private BufferedImage backgroundImage;

        public BackgroundPane() throws IOException {
            backgroundImage = ImageIO.read(getClass().getResource("/images/Mando01.jpeg"));
        }

        @Override
        public Dimension getPreferredSize() {
            if (backgroundImage == null) {
                return new Dimension(400, 400);
            }
            return new Dimension(backgroundImage.getWidth(), backgroundImage.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (backgroundImage == null) {
                return;
            }
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
            g2d.dispose();
        }

    }
}

不使用JPanel.setSize() ,而是將 JFrame 的 LayoutManager 設置為null 每次更改 window 大小時,使用JPanel.setBounds(0,0,windowWidth,windowHeight)方法調整 JPanel 的大小。 您可能必須調用repaint()方法來重新繪制屏幕上的組件。

// Create a window with a size of 300x200
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(300, 200);
frame.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 300, 200);
panel.setBackground(Color.BLACK);
frame.add(panel);
frame.setVisible(true);
// Resize the window to 600x400
frame.setSize(600, 400);
panel.setBounds(0, 0, 600, 400); // Update panel size
panel.repaint(); // Repaint the components

代碼的結果是這樣的: 黑色背景的窗口

如果刪除最后兩行代碼,您會注意到大小沒有改變。

部分填充黑色背景的窗口

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM