Reputation: 7766
I am trying to achieve this layout
i dont know why i am getting this
This is my code
package testing;
import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
public class Testing
{
public static class GridPanel extends JPanel
{
public GridPanel()
{
setLayout(new GridLayout(4,4));
setBackground(Color.GREEN);
this.setPreferredSize(new Dimension(500,100));
JButton b1 = new JButton ("Button 1");
JButton b2 = new JButton ("Button 2");
JButton b3 = new JButton ("Button 3");
JButton b4 = new JButton ("Button 4");
JButton b5 = new JButton ("Button 5");
JButton b6 = new JButton ("Button 6");
JButton b7 = new JButton ("Button 7");
JButton b8 = new JButton ("Button 8");
JButton b9 = new JButton ("Button 9");
JButton b10 = new JButton ("Button 10");
JButton b11 = new JButton ("Button 11");
JButton b12 = new JButton ("Button 12");
JButton b13 = new JButton ("Button 13");
JButton b14 = new JButton ("Button 14");
JButton b15 = new JButton ("Button 15");
JButton b16 = new JButton ("Button 16");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
add(b16);
}
}
public static void main(String[] args)
{
JPanel primary = new JPanel();
primary.setPreferredSize(new Dimension(500,500));
JPanel jp1 = new JPanel();
jp1.setPreferredSize(new Dimension(500,100));
JTextField jt1 = new JTextField(8);
jp1.add(jt1);
GridPanel gp = new GridPanel();
primary.add(jp1);
primary.add(gp);
JFrame jf = new JFrame();
jf.setPreferredSize(new Dimension(500,500));
jf.add(primary);
jf.pack();
jf.setVisible(true);
}
}
I am not sure how to separate the two panels in the frame , how am i doing wrong ??
Upvotes: 0
Views: 65
Reputation: 1266
Try something like this :
public static void main(String[] args)
{
JPanel primary = new JPanel(new BorderLayout());
primary.setOpaque(true);
JTextField jt1 = new JTextField(8);
jt1.setPreferredSize(new Dimension(0, 30));
primary.add(jt1, BorderLayout.NORTH);
GridPanel gp = new GridPanel();
primary.add(gp, BorderLayout.CENTER);
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setPreferredSize(new Dimension(400,200));
jf.setContentPane(primary);
jf.pack();
jf.setVisible(true);
}
Upvotes: 2
Reputation: 447
Look into using a BorderLayout for the JFrame, placing the panel with the text area you want in the Page_Start section and the GridPanel in the Center section.
Upvotes: 1