2016-08-31 36 views
0

我正在嘗試創建Pair Game。我創建了2個JPanel:第一個用於遊戲地圖(100個按鈕),第二個用於統計。問題是我有像左圖片一樣的計時器和計數器的部署,但我想讓它像在正確的圖片(我在繪畫中編輯它)。我應該使用哪種佈局?我嘗試了很多解決方案,但沒有爲我工作。另外,如果我設置爲null經理的JPanel dissapear在Jpanel中設置組件

public class PairGame extends JFrame implements ActionListener{ 

JLabel counterLabel, timerLabel; 
JMenuBar menuBar; 
JMenu file, help; 
JMenuItem fileNew, fileExit, helpAbout; 
JPanel gamePanel, statisticsPanel; 
JToggleButton buttons[]; 

ArrayList <Integer> values; 

int temp=0, counter=0; 

void createMenuBar() 
{   
    file = new JMenu("File"); 
     fileNew = new JMenuItem("New"); 
     fileNew.addActionListener(this); 
     fileNew.setAccelerator(KeyStroke.getKeyStroke("ctrl N")); 
     fileExit = new JMenuItem("Exit"); 
     fileExit.addActionListener(this); 
     fileExit.setAccelerator(KeyStroke.getKeyStroke("ctrl Q")); 
     file.add(fileNew); 
     file.add(fileExit); 
     file.setMnemonic('f'); 

    help = new JMenu("Help"); 
     helpAbout = new JMenuItem("About"); 
     helpAbout.addActionListener(this); 
     helpAbout.setAccelerator(KeyStroke.getKeyStroke("ctrl H")); 
     help.add(helpAbout); 
     help.setMnemonic('h'); 

    menuBar = new JMenuBar(); 
     menuBar.add(file); 
     menuBar.add(help); 
     setJMenuBar(menuBar); 
} 

public void createGameMap() 
{  
    gamePanel = new JPanel(new GridLayout(10,10)); 

    values = new ArrayList<Integer>(); 
    for(int i=1; i<=50; i++) 
     values.add(i); 
    for(int i=51; i<=100; i++) 
     values.add(i-50); 

    Collections.shuffle(values);  

    buttons = new JToggleButton[100]; 
    for(int i=0; i<100; i++) 
    { 
     buttons[i] = new JToggleButton(""+(i+1)); 
     buttons[i].setName(String.valueOf(values.toArray()[i])); 
     buttons[i].addActionListener(this); 
     gamePanel.add(buttons[i]); 
    } 

    gamePanel.setBounds(0,0,550,400); 
    add(gamePanel); 
} 

public void createStatisticsPanel() 
{ 
    statisticsPanel = new JPanel(); 
    counterLabel = new JLabel("Counter: "+counter); 
    timerLabel = new JLabel("Timer: "); 
    statisticsPanel.add(counterLabel); 
    statisticsPanel.add(timerLabel); 
    statisticsPanel.setBounds(0,410,540,400); 
    add(statisticsPanel); 
} 


PairGame() 
{ 
    setTitle("Pair Game"); 
    setSize(565,600); 
    setLocation(400,100);   
    setLayout(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE);  

    createMenuBar(); 
    createGameMap(); 
    createStatisticsPanel(); 

    setVisible(true); 
} 

public static void main(String[] args) 
{ 
    new PairGame(); 
} 

IMAGE

+1

對於'statisticsPanel',您可以使用['BoxLayout'](https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html),['GridLayout'](https:// docs.oracle.com/javase/tutorial/uiswing/layout/grid.html),['GridBagLayout'](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html)甚至[ 'BorderLayout'](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html)。在這種情況下,由我個人的喜好來命名。 –

+0

考慮發佈[MCVE] – c0der

+0

請參閱http://stackoverflow.com/help/someone-answers – c0der

回答

2

這裏有一整各種不同的佈局管理器,允許你把你的「問候」的對象彼此。

如果你看看來自Oracle的BoxLayout的第二個例子......驚喜:該面板顯示「兩個元素」,上面一個是左對齊的標籤。可能正是你在找什麼。

或者;右邊第一個例子GridLayout也有對齊的標籤。

+0

我無法爲我的statisticsPanel設置BoxLayout,因爲我收到編譯錯誤:「BoxLayout無法共享」。另外GridLayout不能給我我想要的。 我只想要有一列有一些位置(現在是2:計數器和計時器,也許還有一些),即左邊界30左右,上層JPanel 30(與遊戲地圖)。我嘗試使用GridLayout與1列和兩行,它不工作,它只適用於如果我設置20列(然後我有1大聲笑)。另外我不能設置我的JLabels,setAligment只是不工作(沒有任何變化) – Kurczaksky

+0

a。您需要正確設置「BoxLayout」。灣您可以將不同的佈局管理器應用於不同的面板查看我發佈的代碼。 – c0der

2

我的回答的目的不僅是爲了演示一個可能的解決方案,使用GhostCat建議的佈局管理器,而且還展示了一個MCVE的想法,使其更容易幫助並獲得幫助。

請參閱評論作出解釋:

//post imports 
import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JToggleButton; 

public class PairGame extends JFrame { 

    //removed fields to keep code as short as possible 
    JLabel counterLabel, timerLabel; 
    JPanel gamePanel, statisticsPanel; 

    //menu bar removed to keep the code as short as possible 
    //void createMenuBar() 

    public void createGameMap() 
    { 
     //simplify as much as possible 
     gamePanel = new JPanel(new GridLayout(1,1)); 

     JToggleButton button = new JToggleButton("1"); 
     gamePanel.add(button); 

     //use layout managers is preferred practice over setting bounds 
     add(gamePanel, BorderLayout.CENTER); 
    } 

    public void createStatisticsPanel() 
    { 
     counterLabel = new JLabel("Counter: "); 
     timerLabel = new JLabel("Timer: "); 

     statisticsPanel = new JPanel(); 

     //use layout managers is preferred practice over setting bounds 
     //the layout you want can be achieved with various managers 
     //such as BorderLayout, BoxLayout, Gridlayout 

     //Implementation of BorderLayout 
     statisticsPanel.setLayout(new BorderLayout()); 
     statisticsPanel.add(counterLabel, BorderLayout.NORTH); 
     statisticsPanel.add(timerLabel, BorderLayout.SOUTH); 

     //to test box layout REPLACE the 3 statements above with 
     //Implementation of BoxLayout 
     //BoxLayout boxLayout = new BoxLayout(statisticsPanel,BoxLayout.Y_AXIS); 
     //statisticsPanel.setLayout(boxLayout); 
     //statisticsPanel.add(counterLabel); 
     //statisticsPanel.add(timerLabel); 

     add(statisticsPanel, BorderLayout.SOUTH); 
    } 


    PairGame() 
    { 
     setTitle("Pair Game"); 
     setSize(565,600); 
     setLocation(400,100); 

     //use layout managers is preferred practice over setting bounds 
     getContentPane().setLayout(new BorderLayout()); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     createGameMap(); 
     createStatisticsPanel(); 

     setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     new PairGame(); 
    } 
} 

不要猶豫根據需要,要求澄清。