The grey border of JFrame
visible
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
.
But with the decorations and the resize features "ON" JPanel
isn't completely filling the 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
.)I am using NetBeans, JFrame
is in "absolute layout". 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. Setting JPanel
s layout to NULL is also not resolving the issue :(
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();
}
}
Custom Creation Code in 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.
Stretching the Jpanel Over JFrame to some more height
But when I try to do that using
setsize(new Dimension(width, height+40));
for the JPanel
, it doesn't work.
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.)
I would be grateful if any solution is provided, even some alternative way with or without 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.
null
("absolute") layouts really aren't a good idea.
Start by taking a look at How to Use 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
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();
}
}
}