将滚动窗格添加到面板中,其中包含另一个面板

时间:2013-12-23 09:02:08

标签: java swing jpanel jscrollbar

我有一个小组drawSomething来绘制其中的内容,我需要它的大小很大,我的框架中有另一个panel将该小组drawSomething放入其中,所以,我需要将scrollpane放在面板上我该怎么做?

这是我的示例代码:

import java.awt.Color;

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


public class myFrame extends JFrame{

    public static void main(String[] args) {
        myFrame mf = new myFrame();
        mf.setSize(400,400);
        mf.setVisible(true);
        mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mf.setLayout(null);

        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.yellow);
        JPanel drawSomething = new JPanel();
        panel1.setBounds(100, 100, 200, 200);
        drawSomething.setSize(500, 500);
        drawSomething.setBackground(Color.BLACK);

        panel1.add(drawSomething);
        JScrollPane scroll = new JScrollPane(panel1);
        mf.add(scroll);

    }
}

0 个答案:

没有答案
相关问题