Solved

setLayout .....

Posted on 2004-09-01
8
313 Views
Last Modified: 2010-03-31
Hi Experts,

    How do I change line in the following code ? i.e. after the TextArea, change line, so that 1, 2, 3 are at the same line. Then change line again, so that 4, 5, 6 are at the same line..... I checked the Layout Manager, but couldn't find any info ..... could anyone please give me some helps ... thanks !

--------------------------------------------

import javax.swing.*;
import java.awt.* ;
public class MeowTest{
   
    public static void main(String[] arg) {
    JFrame myFrame = new JFrame("Good Morning ! Meow ~");  // calling the constructor: JFrame(String title)
    JLabel myLabel = new JLabel("How are you today ?"); // calling the constructor: JLabel(String text)
    Container myPane = myFrame.getContentPane();
    myPane.add(myLabel);

    myPane.setLayout(new FlowLayout()) ;
    myPane.add(new JTextArea(1,5)) ;
    myPane.add(new JButton("1")) ;
    myPane.add(new JButton("2")) ;
    myPane.add(new JButton("3")) ;
    myPane.add(new JButton("4")) ;
    myPane.add(new JButton("5")) ;
    myPane.add(new JButton("6")) ;
    myPane.add(new JButton("7")) ;
    myPane.add(new JButton("8")) ;
    myPane.add(new JButton("9")) ;
    myPane.add(new JButton("0")) ;
     
    myFrame.pack();
    myFrame.show();
   }
}
0
Comment
Question by:meow00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11953182
Better use another layout then
0
 
LVL 35

Accepted Solution

by:
TimYates earned 150 total points
ID: 11953186
   myPane.setLayout(new BorderLayout()) ;
    myPane.add(new JTextArea(1,5), BorderLayout.NORTH) ;
    JPanel buttonPanel = new JPanel() ;
    myPane.add( buttonPanel, BorderLayout.CENTER ) ;
    buttonPanel.setLayout( new GridLayout( 4, 3 ) ) ;
    buttonPanel.add(new JButton("1")) ;
    buttonPanel.add(new JButton("2")) ;
    buttonPanel.add(new JButton("3")) ;
    buttonPanel.add(new JButton("4")) ;
    buttonPanel.add(new JButton("5")) ;
    buttonPanel.add(new JButton("6")) ;
    buttonPanel.add(new JButton("7")) ;
    buttonPanel.add(new JButton("8")) ;
    buttonPanel.add(new JButton("9")) ;
    buttonPanel.add(new JButton("0")) ;
     
    myFrame.pack();
    myFrame.show();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11953235
What about this one:

/*
 * MeowTest.java
 *
 */

import javax.swing.*;
import java.awt.*;

public class MeowTest{
   
    public static void main(String[] arg) {
    JFrame myFrame = new JFrame("Good Morning ! Meow ~");  // calling the constructor: JFrame(String title)
    JLabel myLabel = new JLabel("How are you today ?"); // calling the constructor: JLabel(String text)
    Container myPane = myFrame.getContentPane();
    myPane.add(myLabel);

    myPane.setLayout(new BoxLayout(myPane, BoxLayout.Y_AXIS)) ;
    myPane.add(new JTextArea(1,5));
    JPanel panel1 = new JPanel();
    panel1.add(new JButton("1")) ;
    panel1.add(new JButton("2")) ;
    panel1.add(new JButton("3")) ;
    myPane.add(panel1);
   
    JPanel panel2 = new JPanel();
    panel2.add(new JButton("4")) ;
    panel2.add(new JButton("5")) ;
    panel2.add(new JButton("6")) ;
    myPane.add(panel2);
   
    JPanel panel3 = new JPanel();
    panel3.add(new JButton("7")) ;
    panel3.add(new JButton("8")) ;
    panel3.add(new JButton("9")) ;
    myPane.add(panel3);
   
    myPane.add(new JButton("0")) ;
     
    myFrame.pack();
    myFrame.show();
   }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 11953258
In a lot of cases you have different ways to become the same result.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11953264
:-D
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11953317
>> In a lot of cases you have different ways to become the same result.

But as you will see, if you size the window both solutions have a different behaviour.
That's what a layout manager is used for at last.
Up to you to decide which behaviour you want. That determines the layout to be used.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
ID: 11953431
My advice:
Never hesitate to add an extra JPanel.
It will probably give you the layout you want.

Example: If you use my code and make the window much taller, you see that the 0 button isn't centered nicely.

That is solved if you replace

     myPane.add(new JButton("0")) ;

by

    JPanel panel4 = new JPanel();
    panel4.add(new JButton("0")) ;
    myPane.add(panel4);

Try it.

0
 
LVL 37

Expert Comment

by:zzynx
ID: 11954328
Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 60
Way to decrease size of apk file 9 110
Java string replace 11 63
Java Eclipse Loop 3 31
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question