Seh ich das richtig, dass JPanels nicht als solche Komponenten zählen, weswegen ein Fenster die Größe ändert. Also ich mein das so:
Ich hab noch was rum probiert und dann ist mir aufgefallen, egal wie groß das Panel war was im ScrollPane sitzt, nie wurde gescrollt. Sobald aber Buttons drin waren, kam der Scrollbalken.
Beispiel:
[code=Java]
Frame:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Phenix
*/
public class Frame extends JFrame implements ScrollPaneConstants{
private JScrollPane scroll;
private Panel1 panel;
private JButton[] buttons=new JButton[51];
public Frame(){
panel=new Panel1();
panel.setSize(700,700);
panel.setBackground(Color.BLUE);
for(int i=0; i<50; i++){
buttons[i]=new JButton();
buttons[i].setBounds(0, i*32, 30, 30);
panel.add(buttons[i]);
}
scroll=new JScrollPane(panel);
scroll.setBounds(0,0,300,300);
scroll.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
this.setVisible(true);
this.setLayout(null);
this.setBackground(Color.RED);
this.setSize(400,400);
this.add(scroll);
System.out.println(scroll.getWidth());
}
}
//Hier kommt ein Scrollbalken
[/code]
Scrollt nicht:
[code=Java]
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Phenix
*/
public class Frame extends JFrame implements ScrollPaneConstants{
private JScrollPane scroll;
private Panel1 panel;
private JButton[] buttons=new JButton[51];
public Frame(){
panel=new Panel1();
panel.setSize(700,700);
panel.setBackground(Color.BLUE);
//ohne Buttons
scroll=new JScrollPane(panel);
scroll.setBounds(0,0,300,300);
scroll.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
this.setVisible(true);
this.setLayout(null);
this.setBackground(Color.RED);
this.setSize(400,400);
this.add(scroll);
System.out.println(scroll.getWidth());
}
}
//Scrollt nicht
[/code]