I want to have JFrame with size that fits my screen perfectly. I used getScreenSize(); to get resolution of my computer and set it to JFrame's size. However i found that Jframe's size is actually bigger than my computer's resolution because of the titlebar. (which mean u will find bottom of the jframe is behind window taskbar)
The following code demostrate my problem :
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Titlebar extends JFrame {
private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public void run(){
this.setTitle("TitleBar");
this.setSize(_screenSize);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Titlebar test = new Titlebar();
test.run();
}
}
is that possible to set the jframe size minus titlebar's size ?
Two ways are there:
1. Just "Maximize" the window using the code i.e. add the following:
this.setExtendedState( this.getExtendedState()|JFrame.MAXIMIZED_BOTH );
Your code will look like this:
public class Titlebar extends JFrame {
private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public void run(){
this.setTitle("TitleBar");
this.setSize(_screenSize);
this.setExtendedState( this.getExtendedState()|JFrame.MAXIMIZED_BOTH );
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Titlebar test = new Titlebar();
test.run();
}
}
2. This option is to workaround i.e. by keeping always at the top then you will see this over the task bar i.e. adding the following:
this.setAlwaysOnTop(true);
Your code will be like this:
public class Titlebar extends JFrame {
private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public void run(){
this.setTitle("TitleBar");
this.setSize(_screenSize);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Titlebar test = new Titlebar();
test.run();
}
}
Depending upon your need you can choose the option. But I believe option 1 will be good for you.
Related
I was working on this lab in class and when I tried changing the background color it would stay at its default of white can someone please explain where I my programming went wrong.
import javax.swing.*;
import java.awt.*;
public class DemoPoly extends JFrame {
// constructor
public DemoPoly() {
// defines Frame characteristics
int size = 300;
setSize(size,size);
setTitle("a random window");
getContentPane().setBackground(Color.red);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main (String[] args){
// instantiates a JFrame
// exits on close (opional)
JFrame object = new DemoPoly();
}
public void paint (Graphics g){
// This provides the Graphics object g, where
// you are going to use you graphics primitive
// to paint on the content pane of the frame.
int[] arr = {0,100,100,0};
int[] yarr = {0,0,100,100};
Square object = new Square(arr,yarr,Color.red);
AbstractPolygon randSquare = new Square(arr, yarr, Color.red);
}
I see a couple of problems in your code:
Extending JFrame is like saying your class is a JFrame, JFrame is a rigid container, instead create your GUI based on JPanels. See Java Swing extends JFrame vs calling it inside of class for more information.
You're breaking the paint chain by removing the super.paint(g) call on the paint(...) method. When changing your GUI to extend JPanel instead of JFrame you should use the paintComponent(...) method instead. Take the Lesson: Performing Custom Painting in Swing.
You forgot to add #Override notation on the paint(...) method.
You're not placing your program on the Event Dispatch Thread (EDT) which could cause threading issues.
You can solve this by changing your main() method like this:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Your constructor here
}
});
}
Instead of setting the JFrame size, override the getPreferredSize() method and call pack(). See Should I setPreferred|Maximum|MiniumSize in Java Swing?. The general consensus says yes.
Your problem gets solved by adding
super.paint(g);
on the paint(...) method:
#Override
public void paint(Graphics g) {
super.paint(g); //Never remove this
//Your code goes here
}
With all the above recommendations taken into account, your code should look like this now:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class DemoPoly {
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new DemoPoly().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
CustomPanel cp = new CustomPanel();
cp.setBackground(Color.RED);
frame.add(cp);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class CustomPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
Which produces this output (and is the same output that you'll get with your current code but better because it gives you more control over your components)
I'm don't understand your question. But here is code for change your background to RED;
public class DemoPoly extends JFrame {
public DemoPoly() {
// defines Frame characteristics
int size = 300;
setSize(size, size);
setTitle("a random window");
//change background here
getContentPane().setBackground(Color.red);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// instantiates a JFrame
// exits on close (opional)
JFrame object = new DemoPoly();
}
}
Your code is well. Maybe use #override in your paint method.
This my code :
import java.awt.event.*;
import javax.swing.*;
class CalcFrame extends JFrame {
private JButton btnClear;
private JButton btnEquals;
private JLabel lblDisplay;
private JButton[] btnOps = new JButton[4];
public CalcFrame(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(122,444 ,322,123);
}
}
following image shows, the result that i have :
frame image
As I suspected, and #jchamp pointed out in the comments, your main code reads:
public static void main(String[] args) {
JFrame mx = new JFrame();
mx.setVisible();
}
The code in the CalcFrame constructor is not executed, so any setBounds() or setSize() call is not executed and will therefore have no effect. This also explains the close button not terminating the application, because the setDefaultCloseOperation is also never called.
Instead, create and show your CalcFrame:
public static void main(String[] args) {
CalcFrame mx = new CalcFrame();
mx.setVisible();
}
I'm following a tutorial and made a JFrame, but it's very tiny. Iv'e searched for this problem on this site, but nothing has helped. Does anybody know the problem? I did it exactly like the tutorial, and it worked for him! I'll post a picture of it also.
Here is the code:
package net.trails.std;
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.JFrame;
public class Core extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
private static JFrame frame;
public static double dY = 0, dX = 0;
public static final int res = 1;
public static int dir = 0;
public static boolean moving = false;
public static boolean run = false;
private Image screen;
public static Dimension screenSize = new Dimension(700, 560);
public static Dimension pixel = new Dimension(screenSize.width, screenSize.height);
public static Dimension size;
public static String name = "Trails";
public Core(){
}
public static void main(String[] args) {
Core core = new Core();
frame = new JFrame();
frame.add(core);
size = new Dimension(frame.getWidth(), frame.getHeight());
frame.setSize(700, 560);
frame.setTitle(name);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
core.start();
}
public void run() {
}
}
Get rid of the line with frame.pack().
This packs the frame in as small as possible while still fitting everything inside it. You have nothing inside it yet (since core is nothing).
You're calling frame.pack() with no sized components in your JFrame. This is going to squish the whole thing down to the tiny window you currently have.
Look at the docs for pack():
http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack()
Basically, you need to call setSize() in your Core class (which is an applet).
I have problems with understanding the behavior of my application. I want to create a simple window (1000x700px), divided into two parts (250px and 750px width respectively). I tried the following code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JFrame
{
private static final long serialVersionUID = 1L;
public Example()
{
this.setSize(1000, 700);
this.setTitle("Example");
this.setResizable(false);
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
JPanel navigation_panel_wrap = new JPanel();
JPanel content_panel_wrap = new JPanel();
navigation_panel_wrap.setPreferredSize(new Dimension(250, 700));
content_panel_wrap.setPreferredSize(new Dimension(750, 700));
content_panel_wrap.setBackground(Color.green);
navigation_panel_wrap.setBackground(Color.red);
this.getContentPane().add(navigation_panel_wrap);
this.getContentPane().add(content_panel_wrap);
}
public static void main(String[] args)
{
Example example = new Example();
example.setVisible(true);
}
}
As you can see I manually set layout manager for JFrame (FlowLayout instead of BorderLayout with zero horizontal and vertical gaps). Of course, I can just use BorderLayout and than use add() method with BorderLayout.EAST and BorderLayout.WEST parameters, but I want to understand what's wrong with FlowLayout.
When I run my application, I get the following (no green JPanel):
If I decrease width of, for example, content_panel_wrap and make it 744px instead of 750px, everything works correctly.
So the question is - what are these strange 6 pixels? I'm not sure this value is constant for all operating systems, so I want to understand its origin.
There's nothing wrong with FlowLayout but you will need to call pack() for all components to be sized.
As for your codes problem (+1 to #Reimeus) calling pack() is the solution.
as per docs:
Causes this Window to be sized to fit the preferred size and layouts
of its subcomponents. If the window and/or its owner are not yet
displayable, both are made displayable before calculating the
preferred size. The Window will be validated after the preferredSize
is calculated.
Tips:
Dont extend JFrame unnecessarily.
Use Event Dispatch Thread when creating and changing UI components:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
// create UI components etc here
}
});
Dont call setPreferredSize() rather override getPrefferedSize() of component.
Dont call setSize(...) on JFrame rather call JFrame#pack() before setting it visible.
Dont forget to call JFrame#defaultCloseOperation(..) or your initial/EDT thread will not be terminated when JFrame is closed.
Here is an example combining my advice and your code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Example {
private final JFrame frame;
public Example() {
frame = new JFrame();
frame.setTitle("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//app exited when frame closes
frame.setResizable(false);
frame.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
JPanel navigation_panel_wrap = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(250, 700);
}
};
JPanel content_panel_wrap = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(750, 700);
}
};
content_panel_wrap.setBackground(Color.green);
navigation_panel_wrap.setBackground(Color.red);
frame.add(navigation_panel_wrap);
frame.add(content_panel_wrap);
//pack frame (size components to preferred size)
frame.pack();
frame.setVisible(true);//make frame visible
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
}
Im very new to java. I don't know what's wrong with my frame. I set the size to 300 and 200.
What Im seeing is a short and fat stick like thing.
Below is my code:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class BicycleDemo extends JFrame {
/**
* The serialVersionUID.
*/
private static final long serialVersionUID = -4541236176053545919L;
public static void createGUI () {
JFrame jFrame = new JFrame("JFrame Demo");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = jFrame.getContentPane();
container.setLayout(new FlowLayout());
container.setBackground(Color.BLACK);
jFrame.setSize(300, 200);
jFrame.setResizable(false);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI ();
}
});
}
}
Please help.
You are calling pack(). pack() method resizes the frame to the smallest possible size to hold all the elements. So in fact you set the size to 200 x 300 and then resize the frame once more with pack().
Be aware however, that "hold all elements" is calculated by their preferred size, which can be just 0x0 pixels in a lot of cases.