Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

setLayout .....

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
meow00
Asked:
meow00
  • 6
  • 2
2 Solutions
 
zzynxSoftware engineerCommented:
Better use another layout then
0
 
TimYatesCommented:
   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
 
zzynxSoftware engineerCommented:
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
Industry Leaders: 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!

 
zzynxSoftware engineerCommented:
In a lot of cases you have different ways to become the same result.
0
 
TimYatesCommented:
:-D
0
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now