2015-04-01 99 views
-1

我試圖用java創建一個GUI。我的gui會很簡單。你可以從這裏看到我想要的:http://sketchtoy.com/64839370使用邊框佈局創建GUI Swing

爲了做到這一點,我決定使用網絡上建議的BorderLayout。我有兩個Jpanel對象,並將它們放入jFrame,其佈局是borderlayout。你可以看到我下面的簡化代碼:

private Display display= new Display(); // Display extends JPanel 
public Simulation() 
    { 
     super(); 
     // frame settings 
     setTitle("Label of JFrame "); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setBounds(100,100,1094,560);  
     contentPane=this.getContentPane(); 
     setResizable(false); 

     contentPane.setLayout(new BorderLayout()); 

     try { 
      LeftPanelLogo=ImageIO.read(new File("logo.png")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // generate left panel (information panel) 
     leftPanel=new JPanel(){ 
      @Override 
      public void paintComponent(Graphics g) 
      { 
       super.paintComponent(g); 
       Graphics2D g2d=(Graphics2D)g; 
       g2d.drawImage(LeftPanelLogo, 10, 250, null); 
      } 
     }; 
     //leftPanel.setLayout(null); 

     // add panels to contentPane 


     leftPanel.setBackground(Color.WHITE); 
     display.setBackground(Color.BLACK); 

     contentPane.add(leftPanel,BorderLayout.WEST); 
     contentPane.add(display,BorderLayout.CENTER); 
} 

在顯示類的構造函數我只有下面的代碼:

try 
     { 
      bgPicture = ImageIO.read(new File("bg.jpg")); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

當我運行的代碼,我看到幾乎所有的屏幕滿足用面板在中間,我看不到左面板,(換句話說,因爲我把顯示面板的背景設置爲黑色,所有的屏幕都是黑色的)

那麼,我該如何解決它?

+0

'leftPanel.setLayout(NULL);'Java的圖形用戶界面有不同的OS」,屏幕大小,屏幕分辨率等。這樣的工作,他們是不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。使用佈局也可能是解決這個問題的方法。 – 2015-04-01 13:40:39

+0

@AndrewThompson感謝您的信息,但儘管我把它發表評論,它並沒有改變任何東西 – 2015-04-01 13:41:50

+0

1)爲了更好地幫助更快,發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證例子)或[SSCCE](http://www.sscce.org/)(簡短的,獨立的,正確的例子)。 2)獲取圖像的一種方法是通過[本問答](http://stackoverflow.com/q/19209650/418556)中的圖像進行熱鏈接。 3)請學習常用的Java命名規則(命名約定 - 例如'EachWordUpperCaseClass','firstWordLowerCaseMethod()','firstWordLowerCaseAttribute',除非它是'UPPER_CASE_CONSTANT')並且一致地使用它。 – 2015-04-01 13:42:52

回答

1

enter image description here

import java.awt.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class LogoLayout { 

    private JComponent ui = null; 

    LogoLayout() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui!=null) return; 

     ui = new JPanel(new BorderLayout(4,4)); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     BufferedImage logo = new BufferedImage(
       276,560,BufferedImage.TYPE_INT_RGB); 

     /* All that's needed */ 
     ui.add(new JLabel(new ImageIcon(logo)), BorderLayout.LINE_START); 
     ui.add(new JTextArea("Display", 3, 44)); 
     /* All that's needed */ 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       LogoLayout o = new LogoLayout(); 

       JFrame f = new JFrame("Logo Layout"); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

@Andrew_Thompson對不起,這是我的錯,系統故障 – 2015-04-01 13:58:12

+0

請[接受答案](http://meta.stackexchange.com/a/5235/155831)如果它幫助解決問題。 – 2015-04-01 13:58:47

+0

@Andrew_Thompson我認爲你誤解了我,在我的問題中,我問了2個面板,第一個面板包含一個徽標和一些文本,第二個面板(顯示面板)我將在您的解決方案中放置一個背景圖像,我無法做到這一點 – 2015-04-01 14:11:05