java에서 GridBagLayout으로 화면 분할이 안됩니다.

dltkddyd의 이미지

java 배운지 얼마 안 되는 초보입문자입니다. 지금 레이아웃 관리자에 배우고 있습니다. GridBagLayout이라는 것은 여러 셀에 하나의 컴포넌트를 표시할 수 있다고 해서 lb라는 레이블 컴포넌트를 배열로 4개를 만들었습니다. 프레임을 0~1행 0~2열로 구성하고

lb{0]은 빨간색
lb[1]은 초록색
lb[2]는 파란색
lb[3]은 하늘색

으로 설정했습니다. 소스는 다음과 같습니다.

import java.awt.*;
 
class GridBag extends Frame {
  private Label[] lb=new Label[4];
  private GridBagLayout gb;
  private GridBagConstraints gbcon;
  public GridBag() {
    lb[0]=new Label("label1");
    lb[1]=new Label("label2");
    lb[2]=new Label("label3");
    lb[3]=new Label("label4");
    lb[0].setBackground(Color.RED);
    lb[1].setBackground(Color.GREEN);
    lb[2].setBackground(Color.BLUE);
    lb[3].setBackground(Color.CYAN);
    gb=new GridBagLayout();
    setLayout(gb);
    gbcon=new GridBagConstraints();
    gbcon.fill=GridBagConstraints.BOTH;
    gbcon.weightx=1.0;
    gbcon.weighty=1.0;
 
    makeCell(0,0,1,1,lb[0]);
    makeCell(1,0,2,1,lb[1]);
    makeCell(0,1,2,1,lb[2]);
    makeCell(2,1,1,1,lb[3]);
    setBounds(300,300,400,200);
    setVisible(true);
    }
  public void makeCell(int x, int y, int width, int height, Label l) {
    gbcon.gridx=x;
    gbcon.gridy=y;
    gbcon.gridwidth=width;
    gbcon.gridheight=height;
    gb.setConstraints(l,gbcon);
    add(l);
    }
}
 
public class ExecuteGridBag {
  public static void main(String[] args) {
    GridBag mygridbag=new GridBag();
    }
}

위의 코드대로라면 lb[1]과 lb[2]가 두 개의 셀을 차지해야 하는데, 생각과 다르게 컴포넌트들이 셀을 채우지 않습니다. 이 문제 어떻게 해결해야 하나요? 자바에 대해 문의할 곳이 많지 않네요... 도움 부탁드립니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.