2016-11-30 38 views
0

我正在寫一個小原型,它應該顯示一些帶有文本的JLabel,並且整個站點應該可以滾動。Java Swing JScrollPane不滾動/縮小

我遇到的問題是,scrollpane不可滾動,文本只是極小的縮小。

Scrolling not working

這是下面的代碼,我已經嘗試過與首選大小和工作的setSize,但沒有奏效。

public History(Controller c, Model m) {   
      new HistoryHandler(this.history).save(); 

    this.setLayout(new BorderLayout()); 

    this.history = new ArrayList<>(); 

    this.history = new ArrayList<>(); 
    this.history = new HistoryHandler(this.history).load(); 

    this.label = new JLabel(); 
    Container c1 = new Container(); 
    c1.setLayout(new GridLayout(this.history.size(),1)); 
    Container c2 = new Container(); 
    c2.setLayout(new GridLayout(this.history.size(),1)); 
    Container c3 = new Container(); 
    c3.setLayout(new GridLayout(this.history.size(),1)); 
    Container c4 = new Container(); 
    c4.setLayout(new GridLayout(this.history.size(),1)); 
    Container c5 = new Container(); 
    c5.setLayout(new GridLayout(this.history.size(),1)); 
    Container c6 = new Container(); 
    c6.setLayout(new GridLayout(this.history.size(),1)); 

    JLabel c0 = new JLabel(); 
    c0.setLayout(new GridLayout(1,6)); 
    for (int i = 0; i < this.history.size(); i++) { 
     c1.add(new JLabel(this.history.get(i).getUser())); 
     c2.add(new JLabel(this.history.get(i).getDate())); 
     c3.add(new JLabel(this.history.get(i).getFilesize())); 
     c4.add(new JLabel(this.history.get(i).getFilename())); 
     c5.add(new JLabel(this.history.get(i).getMessage())); 
     c6.add(new JLabel(""+this.history.get(i).isAccepted())); 

    } 
    this.label.setLayout(new BorderLayout()); 
    c0.add(c2); 
    c0.add(c1); 
    c0.add(c4); 
    c0.add(c3); 
    c0.add(c6); 
    c0.add(c5); 
    this.label.add(c0, BorderLayout.CENTER); 

    this.scroll = new JScrollPane(this.label, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    this.add(this.scroll); 

    this.setVisible(true); 
} 
+0

如果'Container'指向java.awt.Container,則用'new JPanel'替換每個出現的'new Container'。沒有人應該直接實例化Container。您還應該使用JPanel而不是JLabel來保存多個組件;儘管技術上允許,JLabel並不意味着要孩子。 – VGR

+0

我用'new JPanel'取代了所有'新容器',但它仍然無法工作。 – Meister96Fels

+2

您可能會發現將所有數據放在JTable中並將該JTable放置在JScrollPane中會更容易。 – VGR

回答

0

用JTable替換Container和JLabel,現在這是最終的解決方案。

String[] columnNames = { 
      this.translation.getTranslation("date"), 
      this.translation.getTranslation("user"), 
      this.translation.getTranslation("filename"), 
      this.translation.getTranslation("filesize"), 
      this.translation.getTranslation("isAccepted"), 
      this.translation.getTranslation("message") 
    }; 

    Object[][] data = new Object[this.history.size()][6]; 

    for (int i = 0; i < this.history.size(); i++) { 
     for (int j = 0; j < 6; j++) { 
      if (j == 0) data[i][j] = this.history.get(i).getDate(); 
      if (j == 1) data[i][j] = this.history.get(i).getUser(); 
      if (j == 2) data[i][j] = this.history.get(i).getFilename(); 
      if (j == 3) data[i][j] = this.history.get(i).getFilesize(); 
      if (j == 4) data[i][j] = this.history.get(i).isAccepted(); 
      if (j == 5) data[i][j] = this.history.get(i).getMessage(); 
     } 
    } 

    JTable table = new JTable(data, columnNames); 

    this.scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    this.add(this.scroll); 

感謝VGR給我tipp替換爲JTable。