Solved

setLayout .....

Posted on 2004-09-01
8
311 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 35
windows explorer path to command prompt 5 42
servlet example 11 40
runtime exception 2 27
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now