package garage;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* #author Jela
*/
public class VehicleParts extends JPanel {
public VehicleParts() {
JPanel card0 = new JPanel();
//card0.setPreferredSize(new Dimension(800, 600));
JPanel card1 = new JPanel(new BorderLayout());
JPanel card2 = new JPanel(new BorderLayout());
JPanel card3 = new JPanel(new BorderLayout());
JLabel zero = new JLabel("0");
JLabel one = new JLabel("1");
JLabel two = new JLabel("2");
JLabel three = new JLabel("3");
card0.add(zero);
card1.add(one);
card2.add(two);
card3.add(three);
card1.setVisible(false);
card2.setVisible(false);
card3.setVisible(false);
card0.setVisible(false);
JButton buttonZero = new JButton("Repair");
JButton buttonOne = new JButton("Parts");
JButton buttonTwo = new JButton("Stock");
JButton buttonThree = new JButton("Supplier");
//JButton buttonback = new JButton("Parts");
buttonZero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card0.setVisible(true);
card1.setVisible(false);// shows card1 when button is clicked
card2.setVisible(false);
card3.setVisible(false);
}
});
buttonOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card1.setVisible(true);// shows card1 when button is clicked
card2.setVisible(false);
card3.setVisible(false);
card0.setVisible(false);
}
});
add(buttonZero);
add(buttonOne);
add(buttonTwo);
add(buttonThree);
add(card0);
add(card1);
add(card2);
add(card3);
}
}
Im not sure how i would change this code into CardLayout. If anyone can give me tips of how i could change it, please tell me. This is not my main class. Ive tried cardlayout, however whenever i use it, i cannot see my buttons anymore.
To make sure the buttons are visible using all the cards, you simply divide the panel into two panels - one panel that will contain the buttons, and the other that will contain the cards.
So - your main panel (VehicleParts) will have a BorderLayout.
You add two panels to it. One is the cards panel. It will have a CardLayout. Make the cards panel reference variable final, so that you can access it from the buttons' event listeners (which are anonymous classes). Although the cards panel itself has a card layout, you add it to your main panel using BorderLayout.CENTER.
The other panel is the buttons panel. It can have a GridLayout, for example. Add it to the main panel using, perhaps, BorderLayout.SOUTH to put it at the bottom.
Add your cards to the cards panel. Don't forget to give them names.
Then create the buttons, and in each button's listener, change cards using the CardLayout.show() method. Add each button to the buttons panel.
Related
I added a button into JPanel and tried to change the size and the position of the button. I've tried different lines of codes but they wont work. Also putting in parent.setLayout(null); or panel.setLayout(null); will just completely remove the button and the background
Here is the code:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
public class app {
public static void main(final String[] args) {
final JFrame parent = new JFrame("CPS TEST");
JButton button = new JButton("Button");
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(button);
panel.setBackground(Color.DARK_GRAY);
parent.add(panel, BorderLayout.CENTER);
parent.setSize(500, 300);
parent.setBackground(Color.CYAN);
parent.setLocationRelativeTo(null);
parent.setVisible(true);
parent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Here are three ways to increase the size of a button:
Changing the position of a component is worthy of a separate question, but changing the size of a button is easy.
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test extends JFrame {
private static final long serialVersionUID = 1L;
public Test() {
super("A test");
setSize(360,300);//Size of JFrame
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);//Sets if its visible.
JButton num1 = new JButton("1");//Set the JButton name
JButton num2 = new JButton("2");
JButton num3 = new JButton("3");
num1.setBounds(80,70,50,50);
num2.setBounds(130,70,50,50);
num3.setBounds(180,70,50,50);
add(num1);
add(num2);
add(num3);
}
public static void main (String[] args) {
new Test().setVisible(true);
}
}
Here the num3 button is set as the background, I want the buttons to be aligned. This might be a trivial mistake I'm not sure as I've just started working with JFrame. Thank you.
The Problem
Basically, there are three parts that are causing this problems...
JFrame uses a BorderLayout by default. This means that only the last component add to any one of the five available layout positions will be managed
You call setVisible(true) before adding anything to the frame
You call setBounds on the buttons.
Because the components are generally painted in z-order (in FIFO order generally) and because of the optimisation in the code, the last button is been controlled by the BorderLayout of the frame, but the other two maintain the bounds you set before
Really interesting problem
Solution(s)
Use an appropriate layout manager, maybe a FlowLayout or GridBagLayout
Call setVisible last where ever possible
Check out Laying Out Components Within a Container for details
This is because of Layout Manager. Please check the code below.
i use another JPanel to put all the buttons. i set the panel as it will have 1 row and 3 columns objects.
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
private static final long serialVersionUID = 1L;
JPanel buttonPanel;
public Test() {
super("A test");
setSize(360,300);//Size of JFrame
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);//Sets if its visible.
setLayout(null);
buttonPanel = new JPanel(new GridLayout(1, 3));
buttonPanel.setBounds(80, 70, 150, 50);
JButton num1 = new JButton("1");//Set the JButton name
JButton num2 = new JButton("2");
JButton num3 = new JButton("3");
buttonPanel.add(num1);
buttonPanel.add(num2);
buttonPanel.add(num3);
add(buttonPanel);
}
public static void main (String[] args) {
new Test().setVisible(true);
}
}
Well, never use raw placing and no Layout Manager.
This is your Bible
You can do that with some Layout tricks. For example this one:
First of all import the Layout classes.
import java.awt.GridLayout;
import java.awt.BorderLayout;
Make the frame a BorderLayout frame by copying this in the constructor of it.
setLayout(new BorderLayout());
Then make another JPanel and make it a GridLayout JPanel:
JPanel panel = new JPanel(new GridLayout(1,3));
panel.add(num1);
panel.add(num2);
panel.add(num3);
Arguments 1 and 3 mean "use exactly 1 row and 3 columns to place the widgets in this JPanel".
Finally add the last panel at the center of the frame:
add(panel,BorderLayout.CENTER);
This way you won't deal with dimensions or precise spots and still do what you want...
(to test it copy all the code in "Test" constructor)
I am trying to make a Rock, Paper, Scissors game and I have added 3 buttons to the frame, however when I launch the program two of the buttons don't appear until you hover over them, anyone have any idea why?
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.FlowLayout;
import javax.swing.JOptionPane;
public class RPSFrame extends JFrame {
public static void main(String [] args){
new RPSFrame();
}
public RPSFrame(){
JFrame Frame1 = new JFrame();
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Rock, Paper or Scissors game");
this.setLocationRelativeTo(null);
ClickListener cl1 = new ClickListener();
ClickListener cl2 = new ClickListener();
ClickListener cl3 = new ClickListener();
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("Result:");
panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 25));
this.add(panel1);
this.setVisible(false);
JPanel panel2 = new JPanel();
JButton Rock = new JButton("Rock");
Rock.addActionListener(cl1);
panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
panel2.add(Rock);
this.add(panel2);
this.setVisible(true);
JPanel panel3 = new JPanel();
JButton Paper = new JButton("Paper");
Paper.addActionListener(cl2);
panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
panel3.add(Paper);
this.add(panel3);
this.setVisible(true);
JPanel panel4 = new JPanel();
JButton Scissors = new JButton("Scissors");
Scissors.addActionListener(cl3);
panel4.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel4.add(Scissors);
this.add(panel4);
this.setVisible(true);
}
private class ClickListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == "Rock"){
int AI = new Random().nextInt(3) + 1;
JOptionPane.showMessageDialog(null, "I have been clicked!");
}
}
}
}
The setVisible(true) statement should be invoked AFTER all the components have been added to the frame.
You currently have two setVisible(...) statements, so you need to get rid of the first one.
Edit:
Took a second look at the code. You have multiple setVisible(...) statements. Get rid of them all except for the last one.
Don't create separate panels for each button. Instead you create one panel (called buttonPanel) for all the buttons. In your case you might use a horizontal BoxLayout. Add a button to the panel, then add glue then add a button, then add glue and then add your final button. Then add this buttonPanel to the NORTH of the frame. ie. this.add(buttonPanel, BorderLayout.NORTH). Read the section from the Swing tutorial on How to Use Box Layout for more information on how the layout works and on what glue is.
The problem is that JFrame has a default BorderLayout. When you just add(component) without specifying a BorderLayout.[POSITION] e.g add(panel, BorderLayout.SOUTH), then the component will get added to the CENTER. The problem with that is each POSITION can only have one component. So the only component you see id the last one you add.
Now I don't know after specifying the positions, if you will get your desired result. A BorderLayout may not be the right fit. But just to see a change, you can set the layout to GridLayout(0, 1) and you will see the component.
this.setLayout(new GridLayout(0, 1));
If this is not the result you want, then you should look over Laying out Components within a Container to learn the different layouts available to you.
Also as I pointed out in my comment
if(e.getSource() == "Rock"){
with the above, you are trying to compare an object (ultimately a button) with a String. Instead you will want to compare the actionCommand
String command = e.getActionCommand();
if("Rock".equals(command)){
I can't get my JFrame from main class to display JPanel from another class. Everything compiles without errors.
JFrameTest.java:
package jframetest;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameTest extends JFrame {
public JFrameTest() {
FlowLayout mainLayout = new FlowLayout();
setSize(320, 480);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(mainLayout);
JPanel panelMain = new JPanel(mainLayout);
JButton testButton = new JButton("Test12");
panelMain.add(testButton);
JPanelOne panel = new JPanelOne();
panelMain.add(panel);
panel.setVisible(true);
add(panelMain);
}
public static void main(String[] arguments) {
JFrameTest frame = new JFrameTest();
frame.setVisible(true);
}
}
JPanelOne.java:
package jframetest;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class JPanelOne extends JPanel {
public JPanelOne() {
FlowLayout layoutPanel = new FlowLayout();
JPanel panel = new JPanel(layoutPanel);
JButton button = new JButton("test");
panel.add(button);
panel.setVisible(true);
}
}
Is it good practice to keep diffrent JPanels in their own classes? (Example: Wanting to have JFrame contain few same size JPanels, which will be switched by setting setVisible() to true/false)
EDIT
Thank you for all your answers. Noted. Now back to my question:
Now that I know how to add single GUI elements created in other classes I want to know if it is possible to organize elements with layout manager in one of the classes (maybe in some other container than JPanel), so I can add them as a group organized in a layout (thats why I was asking about creating whole JPanel in other class). As on the picture:
JPanel (that 2nd class) code for this example would be:
package jframetest;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import net.miginfocom.swing.MigLayout;
public class JPanelOne extends JPanel {
private JPanel panelSettingsMain;
private JLabel labelChooseLanguage, labelWordsCollection;
private JButton buttonSelectLanguage, buttonSelectCollection,
buttonStatistics, buttonPrintingOptions, buttonAddNewWordCollection,
buttonInterfaceCustomization;
private JSeparator separatorSettingsOne, separatorSettingsTwo,
separatorSettingsThree, separatorSettingsFour,
separatorSettingsFive;
private Color greenRegular, separatorGreenLight, separatorGreenDark;
public JPanelOne() {
// creating settings main panel
// setting up layout managers
MigLayout layoutSettingsMain = new MigLayout(
"insets 3 0 0 0");
// setting up colors
greenRegular = new Color(30, 165, 145);
separatorGreenLight = new Color(190, 240, 220);
separatorGreenDark = new Color(130, 205, 180);
panelSettingsMain = new JPanel(layoutSettingsMain);
panelSettingsMain.setBackground(Color.WHITE);
// setting up choose language label
labelChooseLanguage = new JLabel("Choose language:");
panelSettingsMain.add(labelChooseLanguage,
"gapleft 10, gaptop 15, width 200");
// setting up select language button
buttonSelectLanguage = new JButton("language");
buttonSelectLanguage.setForeground(greenRegular);
buttonSelectLanguage.setFocusPainted(false);
buttonSelectLanguage.setBorder(null);
buttonSelectLanguage.setContentAreaFilled(false);
buttonSelectLanguage.setCursor(new java.awt.Cursor(
java.awt.Cursor.HAND_CURSOR));
panelSettingsMain.add(buttonSelectLanguage, "gapbottom 15px, wrap");
// setting up light separator
separatorSettingsOne = new JSeparator();
separatorSettingsOne.setForeground(separatorGreenLight);
panelSettingsMain.add(separatorSettingsOne,
"span 2, width 320, gapbottom 15, wrap");
// setting up words collection label
labelWordsCollection = new JLabel("Words collection:");
panelSettingsMain.add(labelWordsCollection, "gapleft 10");
// setting up selectcollection button
buttonSelectCollection = new JButton("collection");
buttonSelectCollection.setForeground(greenRegular);
buttonSelectCollection.setFocusPainted(false);
buttonSelectCollection.setBorder(null);
buttonSelectCollection.setContentAreaFilled(false);
buttonSelectCollection.setCursor(new java.awt.Cursor(
java.awt.Cursor.HAND_CURSOR));
panelSettingsMain.add(buttonSelectCollection,
"gapbottom 15px, wrap");
// setting up dark separator
separatorSettingsTwo = new JSeparator();
separatorSettingsTwo.setForeground(separatorGreenDark);
panelSettingsMain.add(separatorSettingsTwo,
"span 2, width 320, gapbottom 15px, wrap");
// setting up show statistics button
buttonStatistics = new JButton("Show statistics");
buttonStatistics.setForeground(greenRegular);
buttonStatistics.setFocusPainted(false);
buttonStatistics.setBorder(null);
buttonStatistics.setContentAreaFilled(false);
buttonStatistics.setCursor(new java.awt.Cursor(
java.awt.Cursor.HAND_CURSOR));
panelSettingsMain.add(buttonStatistics,
"gapleft 10, gapbottom 15px, , wrap");
// setting up dark separator
separatorSettingsThree = new JSeparator();
separatorSettingsThree.setForeground(separatorGreenDark);
panelSettingsMain.add(separatorSettingsThree,
"span 2, width 320, gapbottom 15px, wrap");
// setting up printing options button
buttonPrintingOptions = new JButton("Printing options");
buttonPrintingOptions.setForeground(greenRegular);
buttonPrintingOptions.setFocusPainted(false);
buttonPrintingOptions.setBorder(null);
buttonPrintingOptions.setContentAreaFilled(false);
buttonPrintingOptions.setCursor(new java.awt.Cursor(
java.awt.Cursor.HAND_CURSOR));
panelSettingsMain.add(buttonPrintingOptions,
"gapleft 10, gapbottom 15px, wrap");
// setting up dark separator
separatorSettingsFour = new JSeparator();
separatorSettingsFour.setForeground(separatorGreenDark);
panelSettingsMain.add(separatorSettingsFour,
"span 2, width 320, gapbottom 15px, wrap");
// setting up add new word collection button
buttonAddNewWordCollection = new JButton("Add new word collection");
buttonAddNewWordCollection.setForeground(greenRegular);
buttonAddNewWordCollection.setFocusPainted(false);
buttonAddNewWordCollection.setBorder(null);
buttonAddNewWordCollection.setContentAreaFilled(false);
buttonAddNewWordCollection.setCursor(new java.awt.Cursor(
java.awt.Cursor.HAND_CURSOR));
panelSettingsMain.add(buttonAddNewWordCollection,
"gapleft 10, gapbottom 15px, , wrap");
// setting up dark separator
separatorSettingsFive = new JSeparator();
separatorSettingsFive.setForeground(separatorGreenDark);
panelSettingsMain.add(separatorSettingsFive,
"span 2, width 320, gapbottom 10px, wrap");
// setting up interface customization button
buttonInterfaceCustomization = new JButton(
"Interface customization");
buttonInterfaceCustomization.setForeground(greenRegular);
buttonInterfaceCustomization.setFocusPainted(false);
buttonInterfaceCustomization.setBorder(null);
buttonInterfaceCustomization.setContentAreaFilled(false);
buttonInterfaceCustomization.setCursor(new java.awt.Cursor(
java.awt.Cursor.HAND_CURSOR));
panelSettingsMain.add(buttonInterfaceCustomization,
"gapleft 10, gapbottom 15px, wrap");
}
}
I was thinking about navigating through the program GUI by setting JPanels (ones like in the example) to visible or not visible.
Is it good way to do it?
Should I split my GUI into few classes, or I should keep everything in one? I am asking, because now with only half of the GUI in the code its about 400 lines long (and it cant do anything than just "look" at this point). As I had said before - I am beginner and its one of the longest application I have written so far (which I am sure it is not that long anyway!).
EDIT
Maybe I am overthinking it, so in the end is it ok to have big GUI classes and to control visibility of different GUI areas by setting them visible or not?
EDIT
I've looked into the CardLayout tutorial at Oracle and it looks like it is good for my task (excluding the creating JPanels from the external classes, but it is ok). I misunderstood it at first and was thinking about CardLayout only in terms of tabbed pane (which I didn't want to implement in my project).
The problem comes from the JPanelOne class. It inherits JPanel but in the constructor, you create a new JPanel and then you add a button to it. If you do this instead:
public class JPanelOne extends JPanel {
public JPanelOne() {
JButton button = new JButton("test");
add(button);
}
}
it should work as you expect it.
First to answer your question, you need to add an instance of your panel to the frame with something like this in your JFrameTest constructor:
add(new JPanelOne());
You also need to add your button directly to JPanelOne itself:
public class JPanelOne extends JPanel {
public JPanelOne() {
JButton button = new JButton("test");
add(button);
}
}
Second, I believe there is a problem with these lines of code:
FlowLayout mainLayout = new FlowLayout();
// snip...
setLayout(mainLayout);
JPanel panelMain = new JPanel(mainLayout);
Each container should have its own instance of a layout manager. Otherwise your GUI will do strange things:
setLayout(new FlowLayout());
JPanel panelMain = new JPanel(mainLayout);
With some more help (user "Hilek" from some other site) I menaged to get the JPanel from another class to be displeyed in main class. Here is the code:
JFrameTest.java:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameTest extends JFrame {
private JButton testButton;
private JPanel panelMain;
private JPanelOne panel;
public JFrameTest() {
// setting up JFrame
setLayout(null);
setPreferredSize(new Dimension(420, 90));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// creating main JPanel (white)
panelMain = new JPanel();
panelMain.setBackground(Color.WHITE);
panelMain.setBounds(0, 0, 420, 90);
panelMain.setPreferredSize(new Dimension(200, 40));
add(panelMain);
// creating JButton in the main JPanel (white)
testButton = new JButton("Button from main class");
panelMain.add(testButton);
// creating new JPanelOne object from JPanelOne class containing black JPanel
panel = new JPanelOne();
// adding black JPanel to main JPanel (white)
panelMain.add(panel);
pack();
}
public static void main(String[] arguments) {
// creating JFrame object and setting it visible
JFrameTest frame = new JFrameTest();
frame.setVisible(true);
}
}
JPanelOne.java:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class JPanelOne extends JPanel
{
public JPanelOne()
{
// setting up black JPanel
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(220, 40));
panel.setBackground(Color.BLACK);
// creating button on external JPanel
JButton button = new JButton("Button (+JPanel) from external class");
// adding button to the black JPanel
panel.add(button);
// adding blackJPanel
add(panel);
}
}
Print screen of working example:
http://i.stack.imgur.com/qKeBp.jpg
Maybe someone will find it helpful in their problem.
Don't call setSize() on your JFrame. Instead let the layouts set the proper sizes of their components and the GUI. After adding all components to your GUI, call pack() on the JFrame, then call setVisble(true). Note that most layouts respect a component's preferredSize more so than its size.
Also, your calling setVisible(true)on your individual components is unnecessary (unless you are changing their visibility after the GUI is up and running for some reason). You'll also want to read up more on using layout managers and will probably use FlowLayout less once you've studied them.
Edit
Regarding your recent Edit:
Maybe I am overthinking it, so in the end is it ok to have big GUI classes and to control visiblity of diffrent GUI areas by setting them visible or not?
I will answer that sometimes this is helpful, for instance if you want to change a standard calculator into a scientific calculator, sometimes it's good to simply show an already created JPanel filled with the advance calculation buttons by using setVisible(true). If on the other hand you want to swap "views" of your GUI to reflect a substantial change in its state, for instance a word processor changing from edit mode to print mode, you could swap JPanels easily to do this using a CardLayout.
I have created a java program that shows a grid of buttons and labels and i want to know how to pit a constraint on the labels and buttons so they dont re-size when window is dragged out.. can anyone help please?
this is my gridlayout class
package JFrameTester;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class layout extends JFrame {
JButton button1,button2,button3;
JLabel label1,Label2,Label3;
public layout (){
setLayout(new GridLayout(2, 2));
button1 = new JButton ("button1");
add(button1);
label1 = new JLabel ("label1");
add(label1);
button2 = new JButton ("button1");
add(button2);
Label2 = new JLabel ("Label2");
add(Label2);
button3 = new JButton ("button1");
add(button3);
Label3 = new JLabel ("Label3");
add(Label3);
}
}
and this is my main class
public class JFrameTester {
/**
* #param args
*/
public static void main(String[] args) {
layout Lay = new layout();
Lay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Lay.setVisible(true);
// JFrame frame = new JFrame("BUY COMPUTER");
Lay.setSize(800, 600);
//frame.pack();
// frame.setVisible(true);
Lay.addWindowListener(new MyWindowListener());
}
}
Place the labels and buttons inside a JPanel, and then add that JPanel to your GridLayout. GridLayout automatically resizes components to fit the size of the cell they are placed in and when a JFrame is resized it will adjust the components size as needed. If you place them in JPanels only the JPanel in that cell will be resized, not the components.
EDIT
In addition to that it's Java practice to name all classes with an uppercase letter, your layout should be Layout. However, the name Layout does not accurately convey the purpose of class Layout as your class is not a layout, it's a JFrame. Perhaps LayoutFrame would be a better class name.
And (this is default Java practice) variables should be camelcase, beginning with a lowercase letter and subsequent words having a capital letter e.g. thisIsAVariable. Alternatively you could use underscores which is not normal Java practice, this_is_a_varaible. Given that in Java classes generally start with a capital letter and variables start with a lowercase letter the statement layout Lay = new layout(); looks very strange and a bit confusing at first glance.