문제

SetBounds (Null Layout)를 사용하여 Swing에서 간단한 Tic TAC Toe 응용 프로그램을 작성하고 있습니다.

내 문제는 결국 내가 추가하는 구성 요소가 프레임에서 보이지 않거나 완전한 GUI를 왜곡한다는 것입니다. 내 코드는 이것을 더 잘 설명 할 것입니다

import java.awt.*;
import javax.swing.*;
class ZeroKata
{
    ButtonGroup p1group,p2group;
    Font f;
    JButton begin,b1,b2,b3,b4,b5,b6,b7,b8,b9;
    JCheckBox p1K,p2K,p1Z,p2Z;
    JFrame frame;
    JLabel player1,player2,p1Name,p2Name,p1Symbol,p2Symbol,status,dummy; //  dummy label for my problem
    JPanel buttons;
    JTextField name1,name2;
    private void addComponents(Container parent,JComponent...c)
    {
        for(JComponent C:c)
            parent.add(C);
    }
    public ZeroKata()
    {
        frame = new JFrame("Tic Tac Toe");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(900,650);
        frame.setResizable(true);
        frame.setVisible(true);

        buttons = new JPanel();
        buttons.setLayout(new GridLayout(3,3,10,10));
        begin = new JButton("START GAME");
        b1 = new JButton(" ");
        b2 = new JButton(" ");
        b3 = new JButton(" ");
        b4 = new JButton(" ");
        b5 = new JButton(" ");
        b6 = new JButton(" ");
        b7 = new JButton(" ");
        b8 = new JButton(" ");
        b9 = new JButton(" ");

        p1K = new JCheckBox("X");
        p1Z = new JCheckBox("O");
        p2K = new JCheckBox("X");
        p2Z = new JCheckBox("O");

        p1group = new ButtonGroup();
        p2group = new ButtonGroup();

        p1group.add(p1K);
        p1group.add(p1Z);
        p2group.add(p2K);
        p2group.add(p2Z);

        name1 = new JTextField(30);
        name2 = new JTextField(30);

        f = new Font("Georgia",Font.PLAIN,38);
        player1 = new JLabel (" << PLAYER 1 >>");
        p1Name = new JLabel ("NAME : ");
        p1Symbol = new JLabel ("SYMBOL : ");
        player2  = new JLabel ("<< PLAYER 2 >>");
        p2Name = new JLabel ("NAME : ");
        p2Symbol = new JLabel ("SYMBOL : ");
        status = new JLabel ("GAME STATUS -->> ");
        dummy = new JLabel ("   ");

        addComponents(buttons,b1,b2,b3,b4,b5,b6,b7,b8,b9);

        player1.setBounds(100,100,100,30);
        p1Name.setBounds(120,150,100,30);
        p1Symbol.setBounds(120,200,60,30);
        player2.setBounds(100,250,100,30);
        p2Name.setBounds(120,300,100,30);
        p2Symbol.setBounds(120,350,50,30);
        name1.setBounds(200,150,150,30);
        p1K.setBounds(200,200,50,30);
        p1Z.setBounds(250,200,50,30);
        name2.setBounds(200,300,150,30);
        p2K.setBounds(200,350,50,30);
        p2Z.setBounds(250,350,50,30);
        buttons.setBounds(500,100,250,250);
        dummy.setBounds(20,20,30,30);
        begin.setBounds(200,500,150,30);

        frame.add(player1);
        frame.add(p1Name);
        frame.add(p1Symbol);
        frame.add(player2);
        frame.add(p2Name);
        frame.add(p2Symbol);
        frame.add(name1);
        frame.add(name2);
        frame.add(begin);
        frame.add(buttons);
        frame.add(p1K);
        frame.add(p1Z);
        frame.add(p2K);
        frame.add(p2Z);

        /*  u need to add a dummy label also to let GUI work correctly, dont know whyx !
        frame.add(dummy); */
    }
    public static void main(String...args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ZeroKata();
            }
        });
    }
}

그것은 간단한 코드입니다. 나는 내가 어디에서 잘못 될지 모르겠습니다. 미리 감사드립니다!

도움이 되었습니까?

해결책

기본 레이아웃 관리자는 a입니다 BorderLayout (보다 JFrame 개요), 그것을 설정하십시오 null 절대 포지셔닝을 허용하기 위해 :

frame.setLayout(null);

다른 팁

내 문제는 결국 내가 추가하는 구성 요소가 프레임에서 보이지 않거나 완전한 GUI를 왜곡한다는 것입니다. 내 코드는 이것을 더 잘 설명 할 것입니다

  • 당신은 추가했습니다 JComponent이미 눈에 띄는 것 JFrame,

  • 이동하다 frame.setVisible(true); 결국 마지막 코드 라인으로 JComponents 추가됩니다

  • 스윙 GUI를 시작해야합니다 초기 스레드

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top