Solved

setLayout .....

Posted on 2004-09-01
8
309 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11953258
In a lot of cases you have different ways to become the same result.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 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:

744 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

15 Experts available now in Live!

Get 1:1 Help Now