2 Replies - 286 Views - Last Post: 19 January 2013 - 07:57 PM Rate Topic: -----

#1 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

GridLayout positions objects horizontally instead of vertically.

Posted 19 January 2013 - 06:20 PM

Okay, so when I use the line "card = new JPanel(new GridLayout(2,1));", it positions objects horizontally instead of vertically.

What I've tried:

switching to 1,2 instead of 2,1 (pretty obviously didn't work).

I don't know what else to do, as I'm fairly new at GUI.

Here's the class which I'm having trouble with (no criticizing extension of JPanel unless it really is the source of the problem, pbl) Anyone else is free to criticize extension of JPanel as they wish.:

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

public class JackClub extends javax.swing.JPanel
{
    JPanel card;
    
    Jack j;
    Club c;
    
    {
        setPreferredSize(new Dimension(41,82));
        makeCard();
    }
    
    public void makeCard()
    {
        card = new JPanel(new GridLayout(2,1));
        card.setPreferredSize(new Dimension(42,83));
        
        card = new JPanel();
        
        j = new Jack();
        c = new Club();
        
        card.add(j);
        card.add(c);
        
        card.setBackground(Color.WHITE);
        
        j.setBackground(Color.WHITE);
        c.setBackground(Color.WHITE);
        
        this.add(card);
    }
    
    public void paint(Graphics g)
    {
        super.paint(g);
        card.setVisible(true);
        
        j.setVisible(true);
        c.setVisible(true);
    }
}



My testing class:

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

public class CardTesting
{
    public static void main(String[] args)
    {
        JFrame frm = new JFrame();
        
        JackClub jc = new JackClub();
        
        frm.setLayout(new GridLayout(1,1));
        
        frm.add(jc);
        
        jc.repaint();
        
        jc.setVisible(true);
        
        frm.setVisible(true);
    }
}



Other useful information:

classes Club and Jack also extend JPanel

Any input whatsoever (unless you're pbl, in which case refer to the one thing) might be helpful. Please help. I promise that I'll double check your help before posting saying that its not fixed.

Is This A Good Question/Topic? 0
  • +

Replies To: GridLayout positions objects horizontally instead of vertically.

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,135
  • Joined: 05-April 11

Re: GridLayout positions objects horizontally instead of vertically.

Posted 19 January 2013 - 06:44 PM

You are instantiating two JPanels
The latest JPanel doesn't have a GridLayout, instead it is using the default layout manager which is FlowLayout for JPanel :)
card = new JPanel(new GridLayout(2,1));
card.setPreferredSize(new Dimension(42,83));	         
card = new JPanel(); //Uses flowlayout. This line show be deleted



I don't think you are ready for GUI yet, but it is your own choice to make
Was This Post Helpful? 1
  • +
  • -

#3 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: GridLayout positions objects horizontally instead of vertically.

Posted 19 January 2013 - 07:57 PM

@CasiOo I feel like an idiot now. But thanks for pointing that out. I have no idea how I failed to catch that. Permission to pound my face into my keyboard over and over?

This post has been edited by extremeblueness: 19 January 2013 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1