i'm new in java .
i just learn JPanel and JFrame.
i got this note from java software solutions:
" The pack method of the frame sets its size appropriately based on
its contents—in this case the frame is sized to accommodate the size
of the panel it contains."
so i wrote this code :
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
//frame.setSize(1000, 500);
frame.getContentPane().add(panel);
Color darkBlue = new Color(8,40,94);
panel.setSize(1000, 500);
panel.setBackground(darkBlue);
}
but it the result is a really tiny window that i should maximize it with mouse to see the content
but when i set frame size every thing work great!
and i use Ubuntu.
so what's the reason of this problem?
From the order of your code:
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
You did not add anything into the frame before you pack() it. pack() means let the frame decide its size based on the components being added to it.
Since you have no components added to it before you pack() it, you receive a small window with visually nothing inside (until you resize the window).
When the frame is being resized, paintManager will be consulted to paint the contentPane, hence if you add before pack(), not only the frame will be resized nicely for you, the components within it will be painted as well.
To see the components within the JFrame:
public static void main (String [] args){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.add(label1); //Add label to panel
frame.add(panel); //Add panel (with label) to frame
frame.pack(); //Let the frame adjust its size based on the added components
frame.setVisible(true);
}
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
Color darkBlue = new Color(8,40,94);
panel.setPreferredSize(new Dimension(1000, 500));
panel.setBackground(darkBlue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(1000, 500);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
You should use pack() after setting the sizes.
Furthermore panel.setPreferredSize() works better than setSize() for you :)
call jframe.pack() before jframe.setVisible() method !
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.setSize(1000, 500);
frame.getContentPane().add(panel);
Color darkBlue = new Color(8,40,94);
panel.setBackground(darkBlue);
frame.pack() ;
frame.setVisible(true); }
You will also need to check the default layout of JFrame , which is flow layout !
Related
How can I make the inner size 500x500 pixel?
Or should I hardcode the 28px macOS top-bar for windows?
My simple code:
import javax.swing.JFrame;
public class Hello {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
Don't set the JFrame size. Use a JPanel and add that to the JFrame and set the size of the JPanel.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(500,500));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen.
frame.setVisible(true);
If you are extending JPanel it is best to set the size by overridding the following:
#Override
public Dimenison getPreferredSize() {
return new Dimension(500,500);
}
It is also considered best practice to do most layouts and especially painting inside JPanel(s) and not the JFrame.
I have a Panel which I have made scrollable in my frame.
What I need is to add a button that stays fixed in the lower right corner even when I scroll.
I'm new to Java Swing so would appreciate all and any help that I can get.
mainPanel = new SimulationPanel(); //class SimulationPanel extends JPanel
//making mainPanel scrollable
mainPanel.setPreferredSize(new Dimension(((int)(WIDTH*1.2)), HEIGHT));
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
// Settings for JFrame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(scrollPane);
frame.setSize(screenSize.width, screenSize.height);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
I would use nested panels with the outer one be with BorderLayout. Then one with FlowLayout and align FlowLayout.RIGHT and the button inside it.
public class Example extends JFrame {
public Example() {
super("");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea(10000, 0);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("button");
JPanel panelWithButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panelWithButton.add(button);
add(panelWithButton, BorderLayout.PAGE_END);
setLocationByPlatform(true);
pack();
setSize(600, 600);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Example().setVisible(true);
});
}
}
Result:
I would go for a BoxLayout. Add another panel (metaPanel) in which your first put your scrollingPanel, and then you add a button. Instead of usgin scrollingPanel as contentPane, you use metaPanel. Example (the example works, but you need to modify it to make the interface look nice):
JPanel mainPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
JPanel metaPanel = new JPanel();
BoxLayout boxlayout = new BoxLayout(metaPanel, BoxLayout.Y_AXIS);
metaPanel.setLayout(boxlayout);
metaPanel.add(scrollPane);
metaPanel.add(new JButton("button"));
// Settings for JFrame
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(metaPanel); // Put metaPanel here
frame.setSize(500, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
I want to set location and size of my JTextField, how can I do it ? My code doesn't work.
public static void main(String[] args) {
JTextField txt1;
JFrame frame = new JFrame("Hangman");
frame.setSize(1100, 600);
frame.getContentPane().setLayout(new FlowLayout());
frame.setResizable(false);
frame.getContentPane().setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txt1 = new JTextField(50);
txt1.setSize(200, 199);
txt1.setLocation(400, 200);
frame.add(txt1);
frame.setVisible(true);
}
txt1.setSize and txt1.setLocaiton doesn't work.
Output
Absolute positioning doesn't work with a layout manager.
If you really need it, you will have to set null as a layout manager, i.e :
frame.getContentPane().setLayout(null);
I've created a simple user interface in Swing but the setSize command does not appear to be working. Can anyone tell me what the problem is?
import java.awt.Container;
import javax.swing.*;
public class UserInterface1 {
private JTextField outputArea = new JTextField();
private JTextField errorReportArea = new JTextField();
private JPanel inputPanel = new JPanel();
public UserInterface1() {
JFrame frame = new JFrame("Fuel Station");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputArea.setEditable(false);
errorReportArea.setEditable(false);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(outputArea);
contentPane.add(errorReportArea);
contentPane.add(inputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
UserInterface1 test = new UserInterface1();
}
}
Swing Frame setSize
The right answer to this is don't do that!
Even when we know the size of the content of a frame must be (e.g.) 500x500 px, we should not be guessing the size of the frame, which will vary by platform.
pack() knows how big to make the frame. We don't, so we should use it.
The size of the text fields can be specified using a font size and the number of columns.
The right size for inputPanel can be determined from the content within it.
Replace:
frame.setSize(500, 500);
by
frame.setPreferredSize(new java.awt.Dimension(500, 500));
It's because pack() resizes the frame to fit the dimensions of its contents or the preferred size, so you can remove the pack() call, or add a preferred size with setPreferredSize() method
Please, is there any option in MigLayout to stretch some element 100% of the JPanel size? Like when you add JButton into JFrame using BorderLayout? Thanks.
Code:
Jframe frame = new JFrame();
frame.setLayout(new MigLayout());
JPanel mainPanel = new JPanel(new MigLayout());
mainPanel.add(new JButon());
I want the JButton to fill entire area of JFrame. JButton is only reference object I used to explain what I want to achieve.
You can use "grow" for this. This should do same as border layout adding in centre.
See miglayout cheat sheet below
http://migcalendar.com/miglayout/cheatsheet.html
public class test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel(new MigLayout());
mainPanel.add(new JButton(), "dock center");
// or
//mainPanel.add(new JButton(), "dock center");
frame.getContentPane().add(mainPanel);
frame.setSize(200,200);
frame.setVisible(true);
}
}