2014-02-11 76 views
0

這裏是我與工作完全相關代碼:擴展框架,以填補空當窗口大小改變

JPanel songDetails = new JPanel(new GridBagLayout()); 

    GridBagConstraints constraints = new GridBagConstraints(); 
     constraints.fill = GridBagConstraints.BOTH; 
     constraints.weightx = 1.0; 
     constraints.gridx = 0; 
     constraints.gridy = 0; 

    for(int i = 0; i < songOptions.length; i++, constraints.gridx++){ 
     songDetails.add(this.buttons[i],constraints); 
    } 

    constraints.gridy = 1; 

    for(int i = 0; i < songInfo.length; i++, constraints.gridy++){ 

     constraints.gridwidth = 1; 
     constraints.gridx = 0; 
     songDetails.add(this.labels[i],constraints); 

     constraints.gridwidth = GridBagConstraints.REMAINDER; 
     constraints.gridx = 1; 
     songDetails.add(this.fields[i],constraints); 
    } 


    JList<Song> list = new JList<Song>(this.lib); //data has type Object[] 
     list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     list.setLayoutOrientation(JList.VERTICAL); 
     list.setVisibleRowCount(5); 


    JScrollPane listScroller = new JScrollPane(); 
     listScroller.setViewportView(list); 

    constraints.fill = GridBagConstraints.BOTH; 
    constraints.weightx = 1.0; 
    constraints.gridheight = GridBagConstraints.RELATIVE; 
    constraints.gridwidth = GridBagConstraints.REMAINDER; 
    constraints.gridx = 0; 
    constraints.gridy = 0; 
    constraints.anchor = GridBagConstraints.NORTHWEST; 
    setLayout(new GridBagLayout()); 
     add(listScroller,constraints); 
     constraints.gridy++; 
     add(songDetails, constraints); 

在主我初始化框架如下:

SongLib myLibrary = new SongLib("Song Library", pathName); 
     myLibrary.setVisible(true); 
     myLibrary.setSize(300, 250);// Sets the Frame Size 
     myLibrary.setMinimumSize(new Dimension(300,250)); 
     myLibrary.setResizable(true);//Makes the size not fixed 
     myLibrary.setLocationRelativeTo(null); // Make the Frame appear centered 
     myLibrary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Tells program to exit when the 'x'(close) button is pressed 

我的問題是當我展開框架時,它沿水平方向展開(寬度),但不完全展開。我認爲改變填充值會爲我做,但事實並非如此。 此外,如果發生此更改時可以更改字體大小。

如果它不會太複雜,我還想讓用戶能夠更改每個幀的相對大小。我正在考慮需要某種動作偵聽器。如果是這樣,我會稍後再看看,但如果有什麼我可以忽略它,將不勝感激!在你的代碼

回答

3

注意:

GridBagConstraints constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.weightx = 1.0; // ********* 
    constraints.gridx = 0; 
    constraints.gridy = 0; 

你不設置份量爲您的GridBagConstraints,剛剛的weightx。設置它,它會垂直擴展。

例如,

GridBagConstraints constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.weightx = 1.0; 
    constraints.weighty = 1.0; // ***** added ***** 
    constraints.gridx = 0; 
    constraints.gridy = 0; 
0

有了解決的,我仍然可以告訴你,改變字體就需要這樣的監聽器的擴展問題:

myLibrary.addComponentListener(new java.awt.event.ComponentAdapter() 
    { 
     public void componentResized(ComponentEvent e) 
     { 
      //Call method that changes the font of each object 
     } 
    }); 

我'不太清楚你的意思改變每個框架(?)的相對大小,但如果您指的是框架內的每個組件,我會建議先做更多的研究,然後(如有必要)發佈一個新問題。

相關問題