2012-05-20 5 views
0

null 레이아웃을 사용하여 JButton을 배치하려하지만 표시되지 않습니다. gridlayout으로 전환하면 변경 사항에 상관없이 중간에 줄이 쳐져 있습니다. 버튼의 위치와 크기는 어떻게 설정합니까? 실행기절대 위치 지정 JButton

에서 프레임

package Run; 

import javax.swing.*; 

import ThreeD.Display; 
import ThreeD.Launcher; 
import TowerDefence.Window; 


import java.awt.*; 
import java.awt.image.BufferedImage; 

public class Frame extends JFrame{ 

    public static String title = "Game";   

    /*public static int GetScreenWorkingWidth() { 
     return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width; 
    }*/ 

    /*public static int GetScreenWorkingHeight() { 
     return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height; 
    }*/ 

    //public static Dimension size = new Dimension(GetScreenWorkingWidth(), GetScreenWorkingHeight()); 
    public static Dimension size = new Dimension(1280, 774); 


    public static void main(String args[]) { 
     Frame frame = new Frame(); 

     System.out.println("Width of the Frame Size is "+size.width+" pixels"); 
     System.out.println("Height of the Frame Size is "+size.height+" pixels"); 
    } 

    public Frame() { 
     setTitle(title); 
     setSize(size); 
     setResizable(false); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     ThreeDLauncher(); 
    } 

    public void ThreeDLauncher() { 
     //setLayout(new GridLayout(1, 1, 0, 0)); 
     setLayout(null); 

     Launcher launcher = new Launcher(); 
     add(launcher); 

     setVisible(true);  
    } 

    public void TowerDefence() { 
     setLayout(new GridLayout(1, 1, 0, 0)); 

     Window window = new Window(this); 
     add(window); 

     setVisible(true); 
    } 

    public void ThreeD() { 
     BufferedImage cursor = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); 
     Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank"); 

     getContentPane().setCursor(blank); 

     Display display = new Display(); 
     add(display); 

     setVisible(true); 

     display.start(); 
    } 

} 

에서

package ThreeD; 

import java.awt.Rectangle; 

import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 

public class Launcher extends JPanel{ 
    private JButton play, options, help, mainMenu; 
    private Rectangle rplay, roptions, rhelp, rmainMenu; 

    public Launcher() { 
     drawButtons(); 
    } 

    private void drawButtons() { 
     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 

     play = new JButton("Play"); 
     options = new JButton("Options"); 
     help = new JButton("Help"); 
     mainMenu = new JButton("Main Menu"); 

     rplay = new Rectangle(20, 50, 80, 40); 
     roptions = new Rectangle(20, 50, 80, 40); 
     rhelp = new Rectangle(20, 50, 80, 40); 
     rmainMenu = new Rectangle(20, 50, 80, 40); 

     play.setBounds(rplay); 
     options.setBounds(roptions); 
     help.setBounds(rhelp); 
     mainMenu.setBounds(rmainMenu); 

     add(play); 
     add(options); 
     add(help); 
     add(mainMenu); 
    } 
} 
+0

어떻게 'JPanel'의 레이아웃이나 크기를 설정하고 있습니까? –

+0

프레임에 대한 코드를 업데이트했습니다. 거기에 설정되어 있습니다. – core16

+0

내 대답을 확인하고 작동하는지 확인하십시오. 아마도 'JFrame'크기를 설정하고 있지만 레이아웃을 null로 설정하면 ' Launcher' ('JPanel')에는 버튼이 (0, 0, 0, 0) –

답변

1

자식 컨테이너를 작동하는 경우

는 참조 LayoutManagers이 구성 요소 레이아웃,하지만 무엇 내부에 구성 요소. 그렇기 때문에 프레임의 레이아웃을 설정하는 것만으로는 충분하지 않습니다.

0

내 생각 엔 당신이 JPanel 실행기를 추가하기 전에 크기 나 레이아웃을 설정하지 않는 것입니다. 따라서 버튼은 잘 표시되지만 JPanel은 그렇지 않습니다. 다음 자료를 입력 해보십시오.

launcher.setBounds(new Rectangle(0, 0, 200, 200)); 

또는 필요한 크기

public Launcher() { 
    this.setLayout(null); 
    //this.setLayout(new GroupLayout(2, 2)); // I strongly recommend you try this though and get rid of the setBounds and rects 
    drawButtons(); 
} 

편집 : 그게 내가 추천 실행기의 생성자에서, 그래서 부모의 LayoutManager의를 상속하지 않습니다

+0

크기로 표시되어 있지만 행에 줄 지어 있습니다. – core16