2012-05-20 75 views
0

我想用一個空佈局來定位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

子容器不繼承父母的佈局管理,所以在啓動的構造,我會建議:

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(); 
} 

編輯:佈局管理佈局的組成部分,但不是裏面是什麼一個組件。這就是爲什麼僅僅設置框架的佈局是不夠的。

0

我的猜測是你不加入JPanel啓動前設置的大小或佈局。所以按鈕顯示正常,但JPanel不是。嘗試投入:

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

或您需要的尺寸。

看看是否能工程

+0

按鈕顯示,但它們只排成一排。 – core16