1
我想創建一個應用程序,它將垂直列出一些基本的github信息的組件。如何在Swing中創建此組件?
我本來這個
我想,以取代點擊幀按鈕(或者用鈕至少幀)看起來像這樣:
____________________________________
|_____JLabel_____|_____JLabel______|
| |
|_________Fixed-length label_______|
| |
|_______________Button_____________|
我敢Swing開發新手,所以我不知道任何設計習語。請原諒那些混亂的代碼。這是我嘗試過的。
JPanel [] panels;
private void createStubs(PriorityQueue<Issue> issues) {
int i = 0;
while(!issues.isEmpty()) {
Issue issue = issues.poll();
// Create components
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panTop = new JPanel(new GridBagLayout());
JPanel panTopRight = new JPanel(new BorderLayout());
JPanel panTopLeft = new JPanel(new BorderLayout());
JPanel panMiddle = new JPanel(new BorderLayout());
JPanel panBottom = new JPanel();
panTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panTopLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panTopRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panMiddle.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Add components to content panel
panOuter.add(panMiddle, BorderLayout.CENTER);
panOuter.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panTop, BorderLayout.NORTH);
JLabel lblTopLeft = new JLabel(issue.getTitle(), JLabel.CENTER);
JLabel lblTopRight = new JLabel(issue.getAuthor().getLogin(), JLabel.CENTER);
JLabel lblCenter = new JLabel(issue.getBodyShort(), JLabel.CENTER);
JButton btnBottom = new JButton("Check out this issue");
panMiddle.add(lblCenter);
panBottom.add(btnBottom);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panTopLeft.add(lblTopLeft, BorderLayout.NORTH);
panTopRight.add(lblTopRight, BorderLayout.NORTH);
panTop.add(panTopLeft, gbc);
panTop.add(panTopRight, gbc);
panOuter.add(panMiddle, BorderLayout.CENTER);
panOuter.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panTop, BorderLayout.NORTH);
addPanels(panOuter, i);
++i;
}
}
private void addPanels(JPanel panel, int i) {
panels[i] = panel;
add(panels[i]);
}
所以現在看起來是這樣
如何實際上可以做標題和作者是在同一行?在問題之間添加換行符並使其可以滾動也很好。
感謝, erip
添加[MCVE](http://stackoverflow.com/help/mcve)。 – user1803551