Java Swing JLayeredPane not showing up - java

I seem to be having some major issues with JLayeredPane. I have a BorderLayout() pane, and I'd like for the West-side element to contain a few JLayeredPane's on top of each other, so I can switch between them to show the right information.
The west pane should be 200 pixels wide and should be as long as the total window is. In my sample code I have added two layers to the JLayeredPanel, but they don't show up. They should be in the west pane.
Here is my code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Main {
private static JFrame mainFrame = new JFrame();
private static JPanel mainPane = new JPanel();
public Main(){}
public static void initGui(){
JLayeredPane westPanel = new JLayeredPane();
westPanel.setPreferredSize(new Dimension(200,0));
westPanel.setBackground(Color.blue);
JPanel layerOne = new JPanel();
layerOne.add(new JLabel("This is layer 1"));
westPanel.add(layerOne, new Integer(0), 0);
JPanel layerTwo = new JPanel();
layerTwo.add(new JLabel("This si layer 2"));
westPanel.add(layerTwo, new Integer(1), 0);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.yellow);
JPanel eastPanel = new JPanel();
eastPanel.setPreferredSize(new Dimension(200,0));
eastPanel.setBackground(Color.red);
mainPane = new JPanel(new BorderLayout());
mainPane.add(westPanel, BorderLayout.WEST);
mainPane.add(centerPanel, BorderLayout.CENTER);
mainPane.add(eastPanel, BorderLayout.EAST);
mainFrame = new JFrame("Learning to use JLayeredPane");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(200, 200, 800, 500);
mainFrame.setContentPane(mainPane);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGui();
}
}
What this results in:

JLayeredPane uses a null layout and so you are responsible for stating the size and location of all components added to it. If not they will default to a location of [0, 0] and a size of [0, 0].

try this, its working
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Main {
private static JFrame mainFrame = new JFrame();
private static JPanel mainPane = new JPanel();
public Main(){}
public static void initGui(){
JLayeredPane westPanel = new JLayeredPane();
westPanel.setLayout(null);
westPanel.setPreferredSize(new Dimension(200,0));
westPanel.setBackground(Color.blue);
JPanel layerOne = new JPanel();
layerOne.add(new JLabel("This is layer 1"));
layerOne.setBounds(0, 0, 100, 100);
westPanel.add(layerOne, new Integer(0), 0);
JPanel layerTwo = new JPanel();
layerTwo.add(new JLabel("This si layer 2"));
layerTwo.setBounds(0, 100, 100, 100);
westPanel.add(layerTwo, new Integer(1), 0);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.yellow);
JPanel eastPanel = new JPanel();
eastPanel.setPreferredSize(new Dimension(200,0));
eastPanel.setBackground(Color.red);
mainPane = new JPanel();
mainPane.setLayout(new BorderLayout());
mainPane.add(westPanel, BorderLayout.WEST);
mainPane.add(centerPanel, BorderLayout.CENTER);
mainPane.add(eastPanel, BorderLayout.EAST);
mainFrame = new JFrame("Learning to use JLayeredPane");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(200, 200, 800, 500);
mainFrame.setContentPane(mainPane);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGui();
}
}

Related

Java JFrame: Windows Layout & Embed

I need to define a layout for a Jframe Window, as in the picture above.
Below is my approach.
A Picture from my resources folder (/resources/...jpg) embed inside the middle(main).
Top, Bottom, Left and Right divided in four parts, whereas their content is a labeled button stretched, so I can map some methods on it later, that change the picture inside the main container.
I tried to display the picture, but I get the result you see in my screenshot. I can't see it inside my main container and I receive no error message.
I don't know if this is because of my wrong approach of using JFrame.
Below you can see my code, I'd be happy if you could help me solving my wrong design layout pattern too.
MyFrame.java
package ms0.gui;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class MyFrame extends JFrame {
public MyFrame () {
setTitle("This is an example title");
setSize(600,600);
setLocation(750,640);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Main Container
Container mainContainer = this.getContentPane();
mainContainer.setLayout(new BorderLayout(8,6));
mainContainer.setBackground(Color.BLUE);
this.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.green));
//JButton Positions
JButton topButton = new JButton("Oben");
JButton bottomButton = new JButton("Unten");
JButton leftButton = new JButton("Links");
JButton rightButton = new JButton("Rechts");
//Panel Top
JPanel topPanel = new JPanel();
topPanel.setBorder(new LineBorder(Color.BLACK, 3));
topPanel.setBackground(Color.ORANGE);
topPanel.setLayout(new FlowLayout(5));
topPanel.add(topButton);
mainContainer.add(topPanel, BorderLayout.NORTH);
//Panel Middle
JPanel middlePanel = new JPanel();
middlePanel.setBorder(new LineBorder(Color.black, 3));
middlePanel.setLayout(new FlowLayout(4,4,4));
middlePanel.setBackground(Color.cyan);
//Grid Panel Right
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new FlowLayout(4,4,4));
rightPanel.setBorder(new LineBorder(Color.black, 3));
rightPanel.setBackground(Color.GREEN);
rightPanel.add(rightButton);
//Grid Panel Left
JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(4,1,5,5));
gridPanel.setBorder(new LineBorder(Color.black, 3));
gridPanel.setBackground(Color.red);
gridPanel.add(leftButton);
//Center Box
JLabel label = new JLabel("Center Box", SwingConstants.CENTER);
label.setOpaque(true);
label.setBorder(new LineBorder(Color.black,3));
middlePanel.add(gridPanel);
mainContainer.add(label);
mainContainer.add(middlePanel, BorderLayout.WEST);
mainContainer.add(rightPanel, BorderLayout.EAST);
//Panel Bottom
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(3));
bottomPanel.add(bottomButton);
bottomPanel.setBackground(Color.magenta);
bottomPanel.setBorder(new LineBorder(Color.BLUE, 3));
mainContainer.add(bottomPanel, BorderLayout.SOUTH);
//Siegel
String filepath = "/resources/siegel.jpg";
int picWidth = 150;
int picHeight = 150;
ImageIcon image1 = new ImageIcon(getClass().getResource(filepath));
//Image scaledImage = img.getScaledInstance(picWidth, picHeight, Image.SCALE_DEFAULT);
//ImageIcon icon = new ImageIcon(scaledImage);
mainContainer.add(new JButton(image1));
}
}
So, as a very basic example, nothing but BorderLayout
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MyFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame frame = new MyFrame();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public MyFrame() {
setTitle("This is an example title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(new JButton("Top button (stretched)"), BorderLayout.NORTH);
add(new JButton("Left button (stretched)"), BorderLayout.WEST);
add(new JButton("Right button (stretched)"), BorderLayout.EAST);
add(new JButton("Bottom button (stretched)"), BorderLayout.SOUTH);
JLabel label = new JLabel("Picture");
label.setBorder(new EmptyBorder(100, 100, 100, 100));
add(label);
}
}
Remember, simple is often best.
Now, if you absolutely, positively must have the label/picture in another container, you can simply make use of GridBagLayout, as it will centre the child component(s) by default, for example...
JLabel label = new JLabel("Picture");
label.setBorder(new EmptyBorder(100, 100, 100, 100));
// Automatic center position
JPanel mainPane = new JPanel(new GridBagLayout());
mainPane.add(label);
add(mainPane);
And you don't have to use EmptyBorder. GridBagLayout will allow to supply insets which will do the same thing

Resizing jpanel according to the components present in it

In the below example, in the west side of the border layout, there is a parent panel which has a BoxLayout and couple of panels inside. The problem is that the west panel covers the whole area from top to bottom. The FlowLayout used for child panels inside the parent panel consume a lot of area. Is it possible to compress each JPanel according to the components? Also, it should remain the same even when the window is maximized?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class Sample extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample frame = new Sample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.WEST);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);
textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
}
}
One approach is to add panel_1 to an enclosing panel. The default FlowLayout conforms itself to the preferred size of the enclosed components when you pack() the enclosing Window. I've added a gray panel to CENTER as a placeholder; resize the frame to see the effect.
JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
As tested:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class Sample extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Sample frame = new Sample();
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);
textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
contentPane.add(flowPanel, BorderLayout.WEST);
contentPane.add(new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
#Override
public Color getBackground() {
return Color.lightGray;
}
}, BorderLayout.CENTER);
}
}

Java - Add two tabs into JPanel

I have problem with tabs in the JPanel. I know how to make new tabs in Mainframe, but I don't know how to make tabs into JPanel which is located in Mainframe.
Here are the pictures:
I have program looking like this -
http://www.bildites.lv/viewer.php?file=vklfhvfdfpwpcxllfqv.png
But I want to make it look like this -
http://www.bildites.lv/viewer.php?file=bvbrp4qfx2krn9bkx30j.png
And Here is my code of the blue JPanel:
package gui;
import java.awt.Color;
import javax.swing.JPanel;
public class CallsPanel extends JPanel {
private MainFrame frame;
Color color = new Color(99, 184, 255); // steelblue
public CallsPanel(MainFrame frame) {
this.frame = frame;
this.setLocation(0, 0);
this.setSize(300, 380);
this.setLayout(null);
this.setBackground(color);
this.initContent();
}
// -------------------------------------------------------------------------
// Declare New Things
private void initContent() {
// Add New Things
}
// -------------------------------------------------------------------------
}
Thanks a lot to people that will help!
JTabbedPane tabPane = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel label1 = new JLabel("Tab 1");
JLabel label2 = new JLabel("Tab 2");
panel1.add(label1);
panel2.add(label2);
tabPane.add("Tab 1", panel1);
tabPane.add("Tab 2", panel2);
this.add(tabPane);
Play around with the size/color/shape of the tabPane and see what works for you. But this is the basic of a tabPane.
See this simple runnable example
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MyPanel extends JPanel {
JButton button = new JButton("Button");
JTabbedPane tabPane = new JTabbedPane();
public MyPanel(){
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
tabPane.add("Panel 1", panel1);
tabPane.add("Panel 2", panel2);
tabPane.setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout());
add(tabPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setSize(300, 300);
frame.setVisible(true);
}
});
}
}

add controls vertically instead of horizontally using flow layout

I am adding checkboxes on JPanel in FlowLayout the checkboxes are being added horizontally.
I want to add checkboxes vertically on the Panel. What is the possible solution?
I hope what you are trying to achieve is like this. For this please use Box layout.
package com.kcing.kailas.sample.client;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class Testing extends JFrame {
private JPanel jContentPane = null;
public Testing() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(61, 11, 81, 140);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
jContentPane.add(panel);
JCheckBox c1 = new JCheckBox("Check1");
panel.add(c1);
c1 = new JCheckBox("Check2");
panel.add(c1);
c1 = new JCheckBox("Check3");
panel.add(c1);
c1 = new JCheckBox("Check4");
panel.add(c1);
}
return jContentPane;
}
public static void main(String[] args) throws Exception {
Testing frame = new Testing();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
I used a BoxLayout and set its second parameter as BoxLayout.Y_AXIS and it worked for me:
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
As I stated in comment i would use a box layout for this.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout());
JButton button = new JButton("Button1");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
button = new JButton("Button2");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
button = new JButton("Button3");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
add(panel);
JPanel testPanel = new JPanel();
testPanel.setLayout(new BoxLayout(testPanel, BoxLayout.Y_AXIS));
/*add variables here and add them to testPanel
e,g`enter code here`
testPanel.add(nameLabel);
testPanel.add(textName);
*/
testPanel.setVisible(true);

Resizable Swing layout with buttons arranged according to variable dimensions

I would like to make a layout using Java Swing which looks like the following drawing.
(source: braun-abstatt.de)
On the left is a JPanel which is drawn through paintComponent() in a way that the graphics automatically scale when the window is resized. (The question isn't about that panel. That one's already done.)
Now I need some buttons (the black boxes, added in Photoshop for the drawing) to the right of the JPanel mentioned before. The height of the reddish areas at the top and bottom, next to which there should be just empty space, is calculated along the lines of CONSTANT_FACTOR * getHeight(). Next to each compartment on the left, there should be a group of buttons, lined up to the center of the respective compartment (see the blue lines).
The JPanel containing the buttons knows about the CONSTANT_FACTOR and the number of compartments, so it should be possible to feed this information into a layout manager.
Which layout manager would I best use to achieve this layout? I've read about all the different layout managers, but I can't quite figure out which one or which combination of them best fits in this case.
For example, by use of a different LayoutManager, a very easy and simple container, takes no more than 15-20 minutes:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class ThinLineFrame {
private JFrame frame = new JFrame();
private JScrollPane scrollPane;
private JPanel panel = new JPanel();
private JPanel panelNorth = new JPanel();
private JPanel panelCenter = new JPanel();
private JPanel panelCenterCh1 = new JPanel();
private JPanel panelCenterCh2 = new JPanel();
private JPanel panelCenterCh3 = new JPanel();
private JPanel panelCenterCh4 = new JPanel();
private JPanel panelCenterCh5 = new JPanel();
private JPanel panelSouth = new JPanel();
public ThinLineFrame() {
panelNorth.setBackground(Color.red.darker());
panelNorth.setPreferredSize(new Dimension(80, 30));
//
panelCenter.setBackground(Color.darkGray);
panelCenter.setLayout(new GridLayout(5, 1, 2, 2));
//
panelCenterCh1.setLayout(new BorderLayout());
JButton panelCenterCh1Button = new JButton();
panelCenterCh1.add(panelCenterCh1Button, BorderLayout.CENTER);
//
JButton panelCenterCh2Button1 = new JButton();
JButton panelCenterCh2Button2 = new JButton();
panelCenterCh2.setLayout(new GridLayout(2, 1, 2, 2));
panelCenterCh2.add(panelCenterCh2Button1);
panelCenterCh2.add(panelCenterCh2Button2);
//
JButton panelCenterCh3Button1 = new JButton();
JButton panelCenterCh3Button2 = new JButton();
panelCenterCh3.setLayout(new GridLayout(2, 1, 2, 2));
panelCenterCh3.add(panelCenterCh3Button1);
panelCenterCh3.add(panelCenterCh3Button2);
//
JButton panelCenterCh4Button1 = new JButton();
JButton panelCenterCh4Button2 = new JButton();
panelCenterCh4.setLayout(new GridLayout(2, 1, 2, 2));
panelCenterCh4.add(panelCenterCh4Button1);
panelCenterCh4.add(panelCenterCh4Button2);
//
panelCenterCh5.setLayout(new BorderLayout());
JButton panelCenterCh5Button = new JButton();
panelCenterCh5.add(panelCenterCh5Button, BorderLayout.CENTER);
//
panelCenter.add(panelCenterCh1);
panelCenter.add(panelCenterCh2);
panelCenter.add(panelCenterCh3);
panelCenter.add(panelCenterCh4);
panelCenter.add(panelCenterCh5);
//
panelSouth.setBackground(Color.red.darker());
panelSouth.setPreferredSize(new Dimension(80, 30));
//
panel.setLayout(new BorderLayout(2, 2));
panel.add(panelNorth, BorderLayout.NORTH);
panel.add(panelCenter, BorderLayout.CENTER);
panel.add(panelSouth, BorderLayout.SOUTH);
//
scrollPane = new JScrollPane(panel);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(80, 600));
frame.setLocation(100, 150);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ThinLineFrame dlg = new ThinLineFrame();
}
});
}
}
You should try looking at MigLayout. It's a super flexible LayoutManager that is also very simple.
The code would look something like:
MigLayout layout = new MigLayout("flowy");
panel.setLayoutManager(layout);
panel.add(button1);
panel.adD(button2);
etc..
Try adding debug, flowy to the constructor to get a visual idea of what is going on.
GBC without an anchor, just with plain vanilla GridBagConstraints and preferred size.
Centered JButton with fixed size:
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainWithFixSize {
public static void main(String[] argv) throws Exception {
JFrame frame = new JFrame();
Container container = frame.getContentPane();
GridBagLayout gbl = new GridBagLayout();
container.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
JButton component = new JButton();
component.setPreferredSize(new Dimension(25, 25));
gbl.setConstraints(component, gbc);
container.add(component);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(40, 90));
frame.pack();
frame.setVisible(true);
}
private MainWithFixSize() {
}
}

Categories

Resources