2016-11-24 137 views
1

我想將JPanel類型的對象添加到JFrame中。如何將帶有組件的JPanel對象添加到JFrame中

我想這個,但沒有添加Jpanel。

這個想法是:將P5添加到P2中,該P5具有在P5類中定義的組件。

會發生什麼?我不想在類First_view中創建所有的JPanel,因爲代碼會混亂很多。

CODE:

import javax.swing.*; 
import java.awt.*; 

public class First_view extends JFrame { 

    Container Frame; 

    public First_view() { 
     super("title"); 
     Frame = this.getContentPane(); 
     Frame.setLayout(new BorderLayout()); 

     Frame.add((new P2()), BorderLayout.WEST); 

     setSize(900, 500); 
     setLocationRelativeTo(null); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 
} 

class P2 extends JPanel { 

    public P2() { 
     this.setLayout(new BorderLayout()); 

     add((new P5()), BorderLayout.NORTH); 
    } 
} 

class P5 extends JPanel { 

    JScrollPane informacion = new JScrollPane(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    JTextArea T1 = new JTextArea(); 

    public P5() { 
     setLayout(new FlowLayout()); 
     setBorder(BorderFactory.createEmptyBorder(20, 10, 0, 0)); 
     add(setInformacion()); 
    } 

    private JScrollPane setInformacion() { 
     T1.append("Information, bla bla bla"); 

     T1.setEditable(false); 
     T1.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     T1.setLineWrap(true); 
     informacion.add(T1); 
     informacion.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     return informacion; 
    } 
} 

PIC:

enter image description here

+0

並且沒有創建'JScrollPane'的異常嗎? –

+1

一般建議:1)除非有必要,否則不要擴展'JFrame'或'JPanel'。這些課程都沒有必要。 2)使用類的描述性名稱,學習常用的Java命名規則(命名約定 - 例如'EachWordUpperCaseClass','firstWordLowerCaseMethod()','firstWordLowerCaseAttribute',除非它是'UPPER_CASE_CONSTANT')並且一致地使用它。 'First_view'應該是'FirstView'。 'P2'應該像'ApplicationContainer'一樣。 3)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 .. –

+1

.. 4)以最小尺寸提供ASCII圖形或簡單的*圖形佈局圖,如果可調整大小,則寬度和高度更大。 5)源代碼中的單個空白行是需要的。 '{'之後或'}'之前的空行通常也是多餘的。 –

回答

3

該組件在JScrollPane顯示應該被添加,使用setViewportView代替。

private JScrollPane setInformacion() { 
    T1.append("Information, bla bla bla"); 
    ... 

    informacion.setViewportView(T1); 

    ... 
    informacion.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    return informacion; 
} 

觀測數據:傳遞給JScrollPane構造函數的參數是在錯誤的順序,也就是垂直警察至上:

JScrollPane informacion = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
              JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 

編輯:安德魯評論它是擴展類只是爲了使用它(JFrame,JPanel)不是一個好主意。例如,我試圖不要改變太多的原始流程: package cfh.test;

import javax.swing.*; 
import java.awt.*; 


public class FirstView { 

    private JFrame frame; 
    private JTextArea informacionText; // not sure if that is needed as field 

    public FirstView() { 
     informacionText = new JTextArea(); 
     informacionText.setEditable(false); 
     informacionText.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     informacionText.setLineWrap(true); 
     informacionText.append("Information, bla bla bla"); 


     JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
               JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     scrollPane.setViewportView(informacionText); 


     JPanel infoPanel = new JPanel(); 
     infoPanel.setLayout(new FlowLayout()); 
     infoPanel.add(scrollPane); 


     JPanel leftPanel = new JPanel(); 
     leftPanel.setLayout(new BorderLayout()); 
     leftPanel.add(infoPanel, BorderLayout.NORTH); 
     // TODO consider moving above code to an own method returning the left panel 

     frame = new JFrame("title"); 

     frame.setLayout(new BorderLayout()); 
     frame.add(leftPanel, BorderLayout.WEST); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(900,500); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
}