這裏是我與工作完全相關代碼:擴展框架,以填補空當窗口大小改變
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
我的問題是當我展開框架時,它沿水平方向展開(寬度),但不完全展開。我認爲改變填充值會爲我做,但事實並非如此。 此外,如果發生此更改時可以更改字體大小。
如果它不會太複雜,我還想讓用戶能夠更改每個幀的相對大小。我正在考慮需要某種動作偵聽器。如果是這樣,我會稍後再看看,但如果有什麼我可以忽略它,將不勝感激!在你的代碼