hi, this is my code to display one textarea, textfield and label in the flowlayout.
and 2*2 button

/**
 * @(#)Gui.java
 *
 *
 * @author 
 * @version 1.00 2010/10/5
 */

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

public class Gui extends JFrame{
JButton b1=new JButton("one");
JButton b2=new JButton("two");
JButton b3=new JButton("three");
JButton b4=new JButton("four");
JTextArea textarea1=new JTextArea(10,30);
JLabel label1=new JLabel();
JTextField textfield1=new JTextField(30);  

    public Gui() {
JFrame f=new JFrame();
f.setLayout(new BorderLayout());
Container c=f.getContentPane();

JPanel p1=new JPanel();
p1.setLayout(new GridLayout(2,2));
JPanel p2=new JPanel();
p2.setLayout(new FlowLayout());
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p2.add(textarea1);
p2.add(label1);
p2.add(textfield1);
c.add(p1);
c.add(p2);
f.show();
f.setSize(200,300);
    }
public static void main(String args[]){
    new Gui();

}    

}

Recommended Answers

All 10 Replies

Are you having a problem? Can you describe it?

Yes.I am having a problems. is that the components are not shown accordingly

How are they shown?
What do you want to change about the way they are shown?
Please explain what the problem is.

the buttons is not shown why? have u rum the code?
only textarea,textfield,and label is shown but buttons is not .
y?

I was waiting until you could tell me what to look for.

What layout manager is being used by container that you are adding the panel with the buttons? What directions/instructions/constraints are you providing to the layout manager when you add the panels to it?

I want the textfield,label and textarea to b in a panel
then the other panel where i put all the buttons.

container is a must else the panels wont shown if the program is being executed

public Gui() {
JFrame f=new JFrame();
f.setLayout(new BorderLayout());
Container c=f.getContentPane();

JPanel p1=new JPanel();
p1.setLayout(new GridLayout(2,2));
JPanel p2=new JPanel();
p2.setLayout(new FlowLayout());
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p2.add(textarea1);
p2.add(label1);
p2.add(textfield1);
c.add(p1);
c.add(p2);
f.show();

There are three containers and three layout managers. Did you read about how to use the add method for each one?

Maybe if you put the java textbook under your pillow tonight, it will be absorbed somehow.

(1) The default layout manager for JFrame is BorderLayout where 5 blocks are defined as NORTH,WEST,SOUTH,NORTH,CENTER. So the code: f.setLayout(new BorderLayout()); is redundant.

(2) Since the f container is controlled by a BorderLayout when you add any sub-containers into f you have to indicate which block each container goes to. Thus the code, for example, should be:

c.add(p1, BorderLayout.NORTH); // the container p1 having 4 buttons goes to the NORTH block
c.add(p2, BorderLayout.CENTER); // the container p2 having textarea1,label1, and textfield1 goes to the CENTER block

(3) Since you have created an instance of JFrame in the constructor, the class Gui has no need to extend JFrame any more.

(4) Use f.setVisible(true); instead of f.show() which is deprecated

(5) The size of the frame might not be proper to show all the components. You may have to make it bigger/wider perhaps, such as altered with f.setSize(500,300);

(6) Inser the following line of code by the end of the constructor to register the closing button:

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

(7) Use code tag to post your code so that one may discuss the code by referring a specific line number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.