[英]How to let the JPanel fill the complete JFrame (while in maximized state) with the "Windows Decoration" kept ON?
Full image of my window我的全图 window
The grey border of JFrame
visible JFrame
灰色边框可见
I want my application to run on full screen (maximized) by default (but the maximum resolution varies by default from laptop to laptop).我希望我的应用程序在默认情况下以全屏(最大化)方式运行(但默认情况下最大分辨率因笔记本电脑而异)。 I am using a background image which scales according to size and width of user's computer on a
JPanel
.我正在使用根据
JPanel
上用户计算机的大小和宽度缩放的背景图像。 But with the decorations and the resize features "ON" JPanel
isn't completely filling the JFrame
.但是随着装饰和调整大小功能的“开启”,
JPanel
并没有完全填满JFrame
。
I wanted my application to:我希望我的应用程序:
setExtendedState(JFrame.MAXIMIZED_BOTH);
) the image covers the entire JFrame
(Note: Happy with any solution that works on all devices with or without using the JFrame
.)setExtendedState(JFrame.MAXIMIZED_BOTH);
)图像覆盖整个JFrame
(注意:无论是否使用JFrame
,对适用于所有设备的任何解决方案都很满意。)I am using NetBeans, JFrame
is in "absolute layout".我正在使用
JFrame
处于“绝对布局”。 I tried with JPanel
both on absolute layout as well as BorderLayout
(not working), tried pack()
(also not working), jPanel1.setSize(WIDTH,HEIGHT)
with the dimensions of the screen is also not working.我在绝对布局和
BorderLayout
(不起作用)上都尝试过JPanel
,尝试过pack()
(也不起作用),屏幕尺寸的jPanel1.setSize(WIDTH,HEIGHT)
也不起作用。 Setting JPanel
s layout to NULL is also not resolving the issue:(将
JPanel
的布局设置为 NULL 也没有解决问题:(
Sign_Up.java ( JFrame
) 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 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();
}
}
Custom Creation Code in JPanel
: new Panel.PanelScale();
JPanel
中的自定义创建代码: new Panel.PanelScale();
The only thing that I found working was explicitly stretching the JPanel
over the JFrame
to some extra height (in NetBeans) but that resulted in my application window not at the center of the screen but shifted to the right.我发现唯一可行的是将
JPanel
显式拉伸到JFrame
上的某个额外高度(在 NetBeans 中),但这导致我的应用程序 window 不在屏幕中央,而是向右移动。
Stretching the Jpanel Over JFrame to some more height将超过 JFrame 的 Jpanel 拉伸到更高的高度
But when I try to do that using但是当我尝试使用
setsize(new Dimension(width, height+40));
for the JPanel
, it doesn't work.对于
JPanel
,它不起作用。
Also I could have done this using JLabel
but I want my image to cover the JFrame
to full area while working in maximized or resized view on any device (larger or smaller Laptop like 1920x1080 resolution, 1280x720, etc.)我也可以使用
JLabel
完成此操作,但我希望我的图像在任何设备(更大或更小的笔记本电脑,如 1920x1080 分辨率、1280x720 等)上以最大化或调整大小的视图工作时覆盖JFrame
到整个区域
I would be grateful if any solution is provided, even some alternative way with or without JPanel
.如果提供任何解决方案,即使有或没有
JPanel
的替代方法,我将不胜感激。 Even if the application is able to work on Full Screen on any device with the image covering it full I will be satisfied, resizing feature can be sacrificed for the time being即使该应用程序能够在任何设备上全屏运行,图像覆盖它,我也会感到满意,暂时可以牺牲调整大小功能
BorderLayout
(which is set by default for JFrame
) will do exactly what you want automatically, you just then need to resize/position the background based on your needs. BorderLayout
(默认设置为JFrame
)将自动执行您想要的操作,然后您只需根据需要调整背景的大小/位置。
null
("absolute") layouts really aren't a good idea. null
(“绝对”)布局确实不是一个好主意。
Start by taking a look at How to Use BorderLayout .首先看看如何使用 BorderLayout 。 I would also recommend looking at Working with Images and the JavaDocs for
Graphics2D
which will provide you with the information you need on how to resize the image to your needs我还建议查看Working with Images和
Graphics2D
的 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();
}
}
}
Instead of using JPanel.setSize()
, set the LayoutManager of the JFrame to null
.不使用
JPanel.setSize()
,而是将 JFrame 的 LayoutManager 设置为null
。 Every time the window size is changed, resize the JPanel with the method JPanel.setBounds(0,0,windowWidth,windowHeight)
.每次更改 window 大小时,使用
JPanel.setBounds(0,0,windowWidth,windowHeight)
方法调整 JPanel 的大小。 You may have to call the repaint()
method to redraw the components on the screen.您可能必须调用
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
The result of the code is this:代码的结果是这样的:
If you remove the last two lines of the code, you'll notice that the size does not change.如果删除最后两行代码,您会注意到大小没有改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.