2015-10-18 100 views
1

我想創建一個應用程序,它將垂直列出一些基本的github信息的組件。如何在Swing中創建此組件?

我本來這個

Current progress

我想,以取代點擊幀按鈕(或者用鈕至少幀)看起來像這樣:

____________________________________ 
|_____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]); 
} 

所以現在看起來是這樣

Little better

如何實際上可以做標題和作者是在同一行?在問題之間添加換行符並使其可以滾動也很好。

感謝, erip

+0

添加[MCVE](http://stackoverflow.com/help/mcve)。 – user1803551

回答

2

有幾種方式來實現你想要的佈局。這裏我使用GridBagLayout作爲內部面板。我添加了顏色只是爲了顯示組件所需的空間。 包含面板使用GridLayout並被添加到滾動窗格。

enter image description here

public class Test extends JFrame { 

    Test() { 

     JPanel main = new JPanel(new GridLayout(0, 1)); 
     for (int i = 0; i < 20; i ++) 
      main.add(new IssuePanel()); 

     getContentPane().add(new JScrollPane(main)); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    private class IssuePanel extends JPanel { 

     IssuePanel() { 

      setBorder(new LineBorder(Color.BLUE)); 
      setLayout(new GridBagLayout()); 
      GridBagConstraints c = new GridBagConstraints(); 

      JLabel topLeft = new JLabel("Top Left"); 
      topLeft.setOpaque(true); 
      topLeft.setBackground(Color.RED); 
      c.gridx = 0; 
      c.gridy = 0; 
      add(topLeft, c); 

      JLabel topRight = new JLabel("Top Right"); 
      topRight.setOpaque(true); 
      topRight.setBackground(Color.GREEN); 
      c.gridx = 1; 
      c.gridy = 0; 
      add(topRight, c); 

      JLabel middle = new JLabel("Middle"); 
      middle.setOpaque(true); 
      middle.setBackground(Color.YELLOW); 
      c.gridwidth = GridBagConstraints.REMAINDER; 
      c.gridx = 0; 
      c.gridy = 1; 
      add(middle, c); 

      JButton button = new JButton("Button"); 
      button.setOpaque(true); 
      button.setBackground(Color.ORANGE); 
      c.gridx = 0; 
      c.gridy = 2; 
      add(button, c); 
     } 
    } 

    public static void main(String[] args) { 

     new Test(); 
    } 
} 
+0

非常有幫助,更乾淨。謝謝! – erip