我如何在JScrollPane上看到所有内容

时间:2014-03-18 14:03:42

标签: java graphics panel jscrollpane

我做了更新,绘画,重新绘制图形,但我无法看到所有...我被检查了视口但是我的视图面板有限......! 我不知道该怎么办... T-T
当滚动移动时我确实更新了

    top.add(tf1);
    top.add(new Label("×"));
    top.add(tf2);
    Button set = new Button("SET!!");
    set.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            try{
                view.remove(board);
            }catch(NullPointerException e111){
            }
            int x=Integer.parseInt(tf1.getText()), y=Integer.parseInt(tf2.getText());
            board = new Board(x,y);
            view.add(board);
            view.setPreferredSize(new Dimension(x*20, y*20));
            view.setBounds(0, 0, x*20, y*20);
        }
    });
    top.add(set);
    rp.add(top);
    editView.setBounds(0, 100, 500, 600);
    editView.getViewport().addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            try{
            //  board.update(board.getGraphics());
            //  board.repaint();
            //  board.paint(board.getGraphics());
            //  board.paintComponents(getGraphics());
                /*view.remove(board);
                view.add(board);*/
            }catch(NullPointerException e2){
            }
        }
    });
    rp.add(editView);

'视图'只是一个小组(null); 和〜内类董事会=董事会

class Board extends JPanel{
    public Image board;
    public int y,x;
    Board(int x, int y){
        this.x = x;
        this.y = y;
        setBounds(0, 0, x*20, y*20);
        setPreferredSize(new Dimension(x*20, y*20));
        addMouseListener(this);
        addMouseMotionListener(this);
        setOpaque(true);
    }
    @Override
    public void paint(Graphics g) {
        board = createImage(x*20, y*20);
        Graphics g2 = board.getGraphics();
        g2.setColor(Color.white);
        g2.fillRect(0, 0, x*20, y*20);
        g2.setColor(Color.black);
        for(int i=0;i<y;i++){
            if(i%10==0){
                g2.setColor(Color.red);
            }
            g2.drawLine(0, i*20, x*20, i*20);
            if(i%10==0){
                g2.setColor(Color.black);
            }
        }
        for(int i=0;i<x;i++){
            if(i%10==0){
                g2.setColor(Color.red);
            }
            g2.drawLine(i*20, 0, i*20, y*20);
            if(i%10==0){
                g2.setColor(Color.black);
            }
        }
        g.drawImage(board, 0, 0, x*20, y*20, null);
    }

我确实喜欢这些东西......我尝试了很多方法......但同样我无法修复它... = _ = ...
请帮帮我!! ~~

结构:JScrollPane - 面板 - 面板
panel define size方法:setPreferredSize(new Dimension(x,y))


我画画时只使用图形。

2 个答案:

答案 0 :(得分:1)

切勿调用paintpaintComponent方法。

  • 只需在您显示的方法中调用repaint即可。
  • 确保您在开始时致电board.setOpaque(true);
祝你好运。

答案 1 :(得分:0)

我解决了这个问题我如何创建一个新的ScrollPane ... = _ =
像这样

class ComponentScroller extends Container {
    private static final long serialVersionUID = -4959363258095925908L;
    Scrollbar horizontal, vertical;
    Panel scrollarea = new Panel();
    Component component;
    int orgX, orgY;
    public ComponentScroller() {
        scrollarea.setLayout( null );
        horizontal = new Scrollbar( Scrollbar.HORIZONTAL );
        vertical = new Scrollbar( Scrollbar.VERTICAL );
        setLayout( new BorderLayout() );
        add( "Center", scrollarea );
        add( "South", horizontal );
        add( "East", vertical );
    }
    ComponentScroller( Component comp ) {
        scrollarea.setLayout( null );  // We'll handle the layout
        scrollarea.add( component = comp );
        horizontal = new Scrollbar( Scrollbar.HORIZONTAL );
        horizontal.setMaximum( component.getSize().width);
        horizontal.addAdjustmentListener( new AdjustmentListener() {
            public void adjustmentValueChanged(AdjustmentEvent e) {
                component.setLocation( orgX = -e.getValue(), orgY ); 
            } 
        } );
        vertical = new Scrollbar( Scrollbar.VERTICAL );
        vertical.setMaximum( component.getSize().height);
        vertical.addAdjustmentListener( new AdjustmentListener() {
            public void adjustmentValueChanged(AdjustmentEvent e) {
                component.setLocation( orgX, orgY = -e.getValue() ); 
            } 
        } );
        setLayout( new BorderLayout() );
        add( "Center", scrollarea );
        add( "South", horizontal );
        add( "East", vertical );
    }
    public void add_e(Component comp){
        scrollarea.add( component = comp );
        horizontal.setMaximum( component.getSize().width );
        horizontal.addAdjustmentListener( new AdjustmentListener() {
            public void adjustmentValueChanged(AdjustmentEvent e) {
                component.setLocation( orgX = -e.getValue(), orgY ); 
            }
        } );
        vertical.setMaximum( component.getSize().height);
        vertical.addAdjustmentListener( new AdjustmentListener() {
            public void adjustmentValueChanged(AdjustmentEvent e) {
                component.setLocation( orgX, orgY = -e.getValue() ); 
            } 
        } );
        doLayout();
    }
    public void doLayout() {
        super.doLayout();
        horizontal.setVisibleAmount( scrollarea.getSize().width );
        vertical.setVisibleAmount( scrollarea.getSize().height );
    }
}

我无法在现有资源上发现问题所以我创建了新的东西...... = _ =