Solved

setLayout .....

Posted on 2004-09-01
8
315 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 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