-1

I am facing problem in arranging my labels in the frame; I just want someone to guide me in the right direction. What i want to do is to create a JButton and place it the left half of the frame, while the right half will have JTextField in the north and 12 JButtonsat the bottom of the JTextField like the calculator.

this is my code

import java.awt.*;

import javax.swing.*;

public class Code {
JFrame f = new JFrame("The Front View of a Microwave Oven");

JPanel p1 = new JPanel(new BorderLayout());
JPanel p2 = new JPanel();
JPanel p3 = new JPanel(new GridLayout(4,3));
JPanel p4 = new JPanel(new BorderLayout());

JTextField text = new JTextField("Time to be displayed here");

JButton b = new JButton("Food to be placed here");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b0 = new JButton("0");
JButton start = new JButton("Start");
JButton stop = new JButton ("Stop");

public void ui(){
    p2.add(text, BorderLayout.NORTH);
    p2.add(p3, BorderLayout.CENTER);

    p4.add(b, BorderLayout.WEST);
    p4.add(p2, BorderLayout.EAST);

    p3.add(b1);
    p3.add(b2);
    p3.add(b3);
    p3.add(b4);
    p3.add(b5);
    p3.add(b6);
    p3.add(b7);
    p3.add(b8);
    p3.add(b9);
    p3.add(b0);
    p3.add(start);
    p3.add(stop);

    f.add(p4);
    f.setSize(370, 300);
    f.setVisible(true);
    f.setLayout(new BorderLayout());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

it shows me the big button on the right correctly .. but how can i place the 12 buttons with the JTextField on the right of the JFrame?

mazin
  • 234
  • 1
  • 3
  • 13

2 Answers2

1

LayOut Managers are the way to go for these issues. you can also look at this beginner program and you can also look at this Stackoverflow Post - just the code posted in question

Community
  • 1
  • 1
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • i saw the codes .. i know how to place the text field as well the buttons. My actual problem is dividing the label into two, the left half for one big button and the second half for the calculator without using JSplitPane – mazin Jun 13 '14 at 09:45
0

Also you can set layout as null like panel.setLayout(null) and label.setBounds(10,10,20,100) to adjust position anywhere you want using x,y coordinates and hight and width. It is a simple way to do it.

But Layout Manager are mostly used and saves you from playing with pixels.

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55