JScrollPane in JPanel

1

1993maik1993

Huhu,

ich habe ein programm geschrieben, welches komplett fertig ist, nur eine sache muss ich noch machen, und zwar etwas am gui.
ich möchte JScrollPane auf ein JPanel adden damit ich darin scrollen kann.
ich habe ein kleines beispielprogramm geschrieben, welches exakt mein problem wiederspiegelt.

Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Test2 extends JFrame {
	
	private JPanel frame0 = new JPanel(null);
	private JPanel frame1 = new JPanel(new GridLayout(4, 6));
	private JPanel frame2 = new JPanel(null);
	private JButton btn;
	private int x = 0;
	private JScrollPane jScrollPane;
	
	Test2() {
		jScrollPane = new JScrollPane(frame2);
		
		setBounds(250, 250, 600, 600);
		frame1.setBounds(50, 50, 500, 500);
		frame2.setPreferredSize(new Dimension(300, 5000));
		addTestComponents();
		
		frame1.add(jScrollPane);
		frame1.add(frame2);
		frame0.add(frame1);
		frame0.add(new JButton("Test131")).setBounds(0, 0, 200, 30);
		add(frame0);
	}
	
	public void addTestComponents() {
		for (int i = 0; i < 5; i++) {
			frame1.add(btn = new JButton("TEST: " + i));
		}
		btn.setPreferredSize(new Dimension(200, 50));
		for (int i = 0; i < 30; i++) {
			frame2.add(btn = new JButton("TEST2: " + i));
			btn.setBounds(5, x, 150, 20);
			btn.setPreferredSize(new Dimension(150, 20));
			x = x + 25;
		}
		frame0.setBackground(Color.RED);
		frame1.setBackground(Color.GREEN);
		frame2.setBackground(Color.PINK);
		frame0.setOpaque(true);
		frame1.setOpaque(true);
		frame2.setOpaque(true);
	}
	
	public static void main(String[] args) {
		Test2 t = new Test2();
		t.setDefaultCloseOperation(EXIT_ON_CLOSE);
		t.setVisible(true);
	}
}

Bild: http://root-space.eu/file/xmwr9rzx
mein problem ist kurz und knapp das pinke soll ins weiße =D

mfg maik
 
Zuletzt bearbeitet von einem Moderator:
(btw. ich bin maik639 und 1993maik1993, kam in den 1993.. nicht mehr rein..=
huhu,

wenns noch jemand brauchen sollte, hier der code, die notwendige änderung habe ich dann hier auskommentiert.

Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
 
public class Test2 extends JFrame {
    
    private JPanel frame0 = new JPanel(null);
    private JPanel frame1 = new JPanel(new GridLayout(4, 6));
    private JPanel frame2 = new JPanel(null);
    private JButton btn;
    private int x = 0;
    private JScrollPane jScrollPane;
    
    Test2() {
        jScrollPane = new JScrollPane(frame2);
        
        setBounds(250, 250, 600, 600);
        frame1.setBounds(50, 50, 500, 500);
        frame2.setPreferredSize(new Dimension(300, 5000));
        addTestComponents();
        
        frame1.add(jScrollPane);
        // frame1.add(frame2);
        frame0.add(frame1);
        frame0.add(new JButton("Test131")).setBounds(0, 0, 200, 30);
        add(frame0);
    }
    
    public void addTestComponents() {
        for (int i = 0; i < 5; i++) {
            frame1.add(btn = new JButton("TEST: " + i));
        }
        btn.setPreferredSize(new Dimension(200, 50));
        for (int i = 0; i < 30; i++) {
            frame2.add(btn = new JButton("TEST2: " + i));
            btn.setBounds(5, x, 150, 20);
            btn.setPreferredSize(new Dimension(150, 20));
            x = x + 25;
        }
        frame0.setBackground(Color.RED);
        frame1.setBackground(Color.GREEN);
        frame2.setBackground(Color.PINK);
        frame0.setOpaque(true);
        frame1.setOpaque(true);
        frame2.setOpaque(true);
    }
    
    public static void main(String[] args) {
        Test2 t = new Test2();
        t.setDefaultCloseOperation(EXIT_ON_CLOSE);
        t.setVisible(true);
    }
}
 
Zurück