2015-08-25 37 views
0

我正在嘗試製作可變大小的接口。但是,目前,如果所有元件的尺寸大於窗口的尺寸,那麼那些開始變形的就是
!因此,我想添加一個JScrollPane,以便在窗口大小不足時顯示,以避免這種失真。 我剛剛開始添加一個JScrollPane(如下面的代碼所示),但它似乎不起作用,我不知道現在要更改什麼。添加JScrollPane以避免組件變形

JScrollPane myScrollPane = new JScrollPane(); 
myWindow.getContentPane().add(myScrollPane, BorderLayout.CENTER); 

編輯1:這裏是什麼,我都試過一個簡單的例子:

static void one(String[] ari, JLabel sess_nb, Box boxy, long[] res){ 
    int p; 
    int length = ari.length; 
    Border cadre = BorderFactory.createLineBorder(Color.black); 
    Font police = new Font("Monospaced 13", Font.BOLD, 18); 
    Font police1 = new Font("Monospaced 13", Font.BOLD, 15); 
    Font font = new Font("Monospaced 13", Font.ITALIC, 13); 
    for (p=0;p<length;p++){ 
     Box boxz = Box.createVerticalBox(); 
     JLabel Rate = new JLabel("Rating Group "+r); 
     JLabel rg_reser = new JLabel(); 
     JLabel switsh = new JLabel("1"); 
     JLabel reser = new JLabel(); //in this label the resrvation for each update and each rating group will be stocked 
     JLabel consump = new JLabel(); //in this label will be stocked the consumption 
     //---------------------------------------------------- 

     //choice of MB or kB 
     JComboBox combo = new JComboBox(); 
     combo.addItem("Megabytes"); 
     combo.addItem("Bytes"); 
     combo.addItem("kilobytes"); 
     combo.addItem("Gigabytes"); 
     JLabel upload = new JLabel("Upload consumption:"); 
     JTextField upload_entry = new JTextField("7.5"); 
     JLabel download = new JLabel("Download consumption:"); 
     JTextField download_entry = new JTextField("7.5"); 
     JTextField total_entry = new JTextField(); 
     JLabel total = new JLabel("Total consumption:"); 
     JButton rg = new JButton("Next"); 
     JLabel update = new JLabel("Result here"); 
     boxz.add(Rate); 
     boxz.add(total); 
     boxz.add(total_entry); 
     boxz.add(upload); 
     boxz.add(upload_entry); 
     boxz.add(download); 
     boxz.add(download_entry); 
     boxz.add(combo); 
     boxz.add(rg); 
     boxz.add(update); 
     boxy.add(boxz); 
     scrollPane1.add(boxy);   
    } 

編輯2:這裏是一個新的測試代碼,但它也不起作用:

public Fenetre(){ 
    this.setTitle("Data Simulator"); 
    this.setSize(300, 300); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 
    String hello = "hello"; 
    int number = 69; 
    JPanel content = new JPanel(); 
    content.setBackground(Color.LIGHT_GRAY); 
    //Box imad = Box.createHorizontalBox(); 
    JTextArea textArea = new JTextArea(10, 10); 
    JLabel imad = new JLabel(); 
    imad.setText(hello + " your favorite number is " + number + "\nRight?"); 
    JScrollPane scrollPane = new JScrollPane(); 
    setPreferredSize(new Dimension(450, 110)); 

    scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS); 
    scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS); 
    scrollPane.setEnabled(true); 
    scrollPane.setWheelScrollingEnabled(true); 
    scrollPane.setViewportView(textArea); 
    scrollPane.setViewportView(imad); 
    add(scrollPane, BorderLayout.CENTER); 
    //--------------------------------------------- 
    //On ajoute le conteneur 
    content.add(imad); 
    content.add(textArea); 
    content.add(scrollPane); 
    this.setContentPane(content); 
    this.setVisible(true); 
    this.setResizable(false); 
} 

出現一個小小的白色恐慌(我認爲它是滾動條或滾動面板?),並且窗口中的組件超過了窗口s當我嘗試滾動時(使用鼠標或敲擊白色恐慌),沒有任何反應。我不知道什麼是錯的。

+0

請用[minimal,complete example](http://stackoverflow.com/help/mcve)更新您的問題,以證明您所看到的行爲。 – Radiodef

+0

@Radiodef確定完成。 – eLearner

回答

1

請勿使用scrollPanel.add(boxy) - 您需要將boxy添加到scrollPanel的視口,而不是scrollPanel本身。改爲使用scrollPanel.setViewportView(boxy),或者更好地創建ScrollPanel AFTER之後創建boxy並使用帶有Component參數的構造函數。

+0

它不起作用,我嘗試了一個簡單的代碼來查看它是否有效(請參閱我的問題上的新代碼,我已將其編輯)。 出現一個小小的白色恐慌(我認爲它是滾動條或滾動面板?),並且窗口中的組件超過了窗口大小,但是當我嘗試滾動時(使用鼠標或叮噹響白色恐慌)時沒有任何反應。我不知道什麼是錯的!請參閱我的問題上的新代碼。 – eLearner