2017-07-01 83 views
0

我給自己一個關於重新學習Java的速成課程。我正在編寫一個非常簡單的程序,當您將它點擊到隨機面板時,它可以簡單地改變按鈕的位置。沒有真正的問題,我幾乎完成了我想要的程序。但是,我想知道爲什麼當我將佈局樣式應用到第一個面板(buttonPanel1)時,它會自動應用於每個面板?爲什麼佈局樣式應用於所有JPanel?

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package buttonswitch; 

import javax.swing.*; 
import java.awt.*; 
import javax.swing.border.*; 
import java.awt.event.*; 
import java.util.*; 

/** 
* 
* @author Supreme Lenova 
*/ 
import java.util.Random; 
public class ButtonWindow extends JFrame{ 

    private JPanel buttonPanel1; 
    private JPanel buttonPanel2; 
    private JPanel buttonPanel3; 
    private JPanel buttonPanel4; 
    private JButton Button; 

    private Border raisedbevel, loweredbevel; 
    private Border compound; 

    public ButtonWindow(){ 
     setTitle("Button Game"); 
     setLocation(600,50); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     GridLayout grid = new GridLayout(2,2); 
     setLayout(grid); 

     raisedbevel = BorderFactory.createRaisedBevelBorder(); 
     loweredbevel = BorderFactory.createLoweredBevelBorder(); 
     compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel); 

     buildPanels(); 

     Button = new JButton("Click!"); 
     Button.setAlignmentX(Component.CENTER_ALIGNMENT); 
     Button.addActionListener(new ButtonListener()); 

     buttonPanel1.add(Button); 
     buttonPanel1.add(Box.createVerticalGlue()); 

     setSize(300,300); 
     setVisible(true); 

} 
    private void buildPanels(){ 
     buttonPanel1 = new JPanel(); 
     buttonPanel2 = new JPanel(); 
     buttonPanel3 = new JPanel(); 
     buttonPanel4 = new JPanel(); 

     buttonPanel1.setLayout(new BoxLayout(buttonPanel1, BoxLayout.PAGE_AXIS)); 
     buttonPanel1.add(Box.createVerticalGlue()); 


     buttonPanel1.setBorder(compound); 
     buttonPanel2.setBorder(compound); 
     buttonPanel3.setBorder(compound); 
     buttonPanel4.setBorder(compound); 


     this.add(buttonPanel1); 
     this.add(buttonPanel2); 
     this.add(buttonPanel3); 
     this.add(buttonPanel4); 
    } 



    private class ButtonListener implements ActionListener{ 

     public void actionPerformed(ActionEvent e){ 

      generateButton(); 

     } 

     private void generateButton(){ 
     int last = 5; 
     int place = 5; 
     Random rand = new Random(); 
     while(place==last){ 
     place = rand.nextInt(4)+1; 
     } 
     last = place; 

     switch (place){ 

      case 1: 
       repaint(); 
       buttonPanel1.add(Button); 
       break; 
      case 2: 
       repaint(); 
       buttonPanel2.add(Button); 
       break; 
      case 3: 
       repaint(); 
       buttonPanel3.add(Button); 
       break; 
      case 4: 
       repaint(); 
       buttonPanel4.add(Button); 
       break; 


     } 
    } 

} 
} 
+3

它不會將佈局應用於其他面板。面板的默認佈局是「FlowLayout」。將所有面板添加到使用「GridLayout」的框架中,所有面板將具有相同的父面板,並且每個面板都將顯示在父面板的不同網格中。 – camickr

+0

我明白你在說什麼。雖然我說它適用於其他面板的原因是因爲每當我更改buttonPanel1時,它都會更改每個面板上的按鈕。如果我使用GridLayout,按鈕會變大,併爲每個面板佔用整個空間。如果我使用BoxLayout,它會重複每個面板的佈局。有什麼我可能不瞭解的嗎?您的描述中的哪個面板是父面板?謝謝btw。 – MadThink

+0

'如果我使用GridLayout,按鈕會變大,併爲每個面板佔用整個空間。 - 是的,GridLayout的工作方式是。每個單元的尺寸與添加到面板的最大組件尺寸相同。 – camickr

回答

0

設置佈局管理器來buttonPanel1

buttonPanel1.setLayout(new BoxLayout(buttonPanel1, BoxLayout.PAGE_AXIS)); 
buttonPanel1.add(Box.createVerticalGlue()); 


佈局管理器不改變,其默認使用FlowLayout其他面板。
它影響按鈕的大小。再次

buttonPanel1.add(Button, BorderLayout.CENTER); 

和打印:打印出

System.out.println(Button.getSize()); //use button, not Button 

現在改變的buttonPanel1

buttonPanel1.setLayout(new BorderLayout()); 

佈局管理器和更改代碼的按鈕添加到面板上。
佈局管理器更改按鈕的初始大小。其他3個面板的管理器FlowLayout不更改大小。

+0

非常感謝!這回答了我的問題。 – MadThink