【问题标题】:How i can see all on JScrollPane我如何在 JScrollPane 上看到所有内容
【发布时间】:2023-04-03 20:33:01
【问题描述】:

我确实更新、绘制、重新绘制图形,但我看不到所有...我已检查视口,但它限制了我的视图面板...!
我不知道我该怎么办... 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);

'view' 只是一个 Panel(null); 和~ 内部类 Board = board

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 - 面板 - 面板
面板定义尺寸方法:setPreferredSize(new Dimension(x,y))


我在绘制时只使用图形。

【问题讨论】:

    标签: java graphics panel jscrollpane


    【解决方案1】:

    永远不要调用paintpaintComponent 方法。

    • 只需按照您提供的方法调用repaint即可。
    • 确保您在开始时调用了board.setOpaque(true);

    祝你好运。

    【讨论】:

    • 嗯...重绘的方法是什么...?只是重绘()?我试了一下,但我看不到解决这个问题...而且我从不在那里使用 setOpaque 方法 =_=...
    • stateChanged方法中调用board.repaint();,在构造函数中调用board.setOpaque(true);
    • 我试试你的方法,但我可以看到相同的视图...... T-T
    • 在这种情况下,如果您可以发布更多代码会更好。 (你试过的地方)
    • 1) 取消注释 board.repaint(); 2) 覆盖 paintComponent 而不是 Board 类中的 paint。 3)直接在g上调用paint stuff,而不是创建图像和创建g2。 4)不要抓住NullPointerException,让它崩溃(你现​​在知道错误)。 5) 将System.out.println("test 1") 等放入侦听器和绘制方法中,以查看它们是否实际被调用。祝你好运
    【解决方案2】:

    我解决了这个问题我如何创建一个新的 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 );
        }
    }
    

    我在现有资源上找不到问题,所以我创建了一些看起来像这样的新东西... =_=

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 1970-01-01
      • 2023-03-30
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 2013-08-01
      相关资源
      最近更新 更多