2014-07-08 40 views
0

很新的Java(和一般的編程) 我試圖建立這樣的佈局,我試圖根據從交換機上構建一個JPanel:添加的JPanel構造函數的JPanel顯示了灰色

public class PrimaryDisplay extends JPanel { 
private static final long serialVersionUID = 1L; 
private int option = 0; 

public int getOption() { 
    return option; 
} 

public void setOption(int option) { 
    this.option = option; 
} 

public PrimaryDisplay(int primaryChoice) { 
    setOption(primaryChoice); 

    switch (getOption()) { 
     case 1: //fixate 
      //JPanel primaryScreen = new JPanel(); 
      //primaryScreen.setBackground(Color.BLUE); 
      //primaryScreen.setLayout(new GridLayout(1,1)); 
      JLabel fixate = new JLabel(); 
      fixate.setText("+"); 
      fixate.setFont(new Font("Verdana", 1, 50)); 
      fixate.setForeground(Color.WHITE); 
      fixate.setHorizontalAlignment(SwingConstants.CENTER); 
      fixate.setVerticalAlignment(SwingConstants.CENTER); 
      //.add(fixate); 
      break; 

     case 2: // 

    } 
} 
} 

並從一個主叫這個構造函數:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Frame; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.Toolkit; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class Main { 
public static JFrame fullFrame = new JFrame(); 
public static JPanel primaryContainer = new JPanel(); 
public static JPanel hmdContainer = new JPanel(); 
static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
static int width = (int) screenSize.getWidth(); 
static int height = (int) screenSize.getHeight(); 
public static int primaryChoice = 1; 

public static void main(String[] args) { 
    fullFrame.setExtendedState(Frame.MAXIMIZED_BOTH); 
    fullFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    fullFrame.setUndecorated(true); 
    fullFrame.setLayout(new GridBagLayout()); 

    primaryContainer.setLayout(new GridLayout(2,1)); 
    primaryContainer.setBackground(Color.YELLOW); 

    hmdContainer.setBackground(Color.GREEN); 
    hmdContainer.setLayout(new GridLayout(3,1)); 

    JPanel filler = new JPanel(); 
    filler.setBackground(Color.RED); 
    primaryContainer.add(filler); 

    PrimaryDisplay prim = new PrimaryDisplay(primaryChoice);   
    primaryContainer.add(prim); 

    HMD hmdDisplay = new HMD(); 
    hmdDisplay.setBackground(Color.ORANGE); 
    hmdContainer.add(hmdDisplay); 

    GridBagConstraints m = new GridBagConstraints(); 
    m.fill = GridBagConstraints.BOTH; 
    m.gridwidth = 1; 
    m.gridheight = 1; 
    m.weightx = .7; 
    m.weighty = 1; 
    m.gridx = 0; 
    m.gridy = 0; 
    fullFrame.add(primaryContainer, m); 
    m.gridy = 0; 
    m.gridx = 1; 
    m.weightx = .3; 
    fullFrame.add(hmdContainer, m);  
    fullFrame.setVisible(true); 

} 

} 

它只能顯示爲一個灰色的框....? 可能非常簡單的解決方案,但我目前難住。

謝謝。

+0

開始通過確保您的EDT範圍內啓動您的應用程序,請參見[初始線程(http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)更多細節 – MadProgrammer

+0

是這樣的:'//。add(fixate);'故意? – MadProgrammer

+0

*「它永遠只顯示爲一個灰色框....?」 * - 當我運行代碼,我得到一個紅色和綠色方框中,其中'+顯示'灰色框(如果我去掉' //。add(fixate);'),你的意思是'+'面板是灰色的嗎? – MadProgrammer

回答

2

我「相信」這就是你想達到什麼目的?

Boxes

的主要原因「+」面板是灰色的,因爲在默認情況下JPanel是不透明的,因此,它涵蓋了無論是它背後...

如果您在PrimaryDisplay改變opaque屬性false,它會變得透明允許父容器顯露出來......

public PrimaryDisplay(int primaryChoice) { 
    setOpaque(false); 
+0

嗯哼。讓sense.when我補充一點,行,我看到黃色的背景,而不+ –

+0

嗯,我在你就想加入到它什麼只是猜測) – MadProgrammer

+0

確定。所以,當我擴展JPanel我就可以直接與add.xxx而添加到它不必增加額外的JPanel ,,不知道我在想什麼那裏。感謝一如既往 –