Avatar billede fredand Forsker
29. maj 2011 - 18:34 Der er 1 løsning

Problems with JScrollPane

Hello guys!

I'm playing around with a JScrollPane.
Please look at this image at:
http://fredand44.webs.com/bilder/test.png

Look how bad and strange it scrolls my canvas inside the JScrollPane.
I thougt it would scroll the view downwards, but instead it scrolls the canvas upwards and over the border!!

If any one got any solution for this please let me know.

The code is below, compile, run and resize it a bit smaller, then try the scroll:

package test;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

public class TestScroll extends JPanel
{
    public TestScroll(Canvas canvas)
    {
        TitledBorder titledBorder = BorderFactory
                .createTitledBorder("Test scroll");
        titledBorder.setTitleColor(Color.GRAY);
        titledBorder.setTitleJustification(TitledBorder.LEFT);
        setBorder(titledBorder);

        JScrollPane scroll = new JScrollPane(canvas,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        setLayout(new BorderLayout());
        add("Center", scroll);
    }

    public static void main(String[] args)
    {
        JFrame jFrame = new JFrame("Test");
        MyCanvas canvas = new MyCanvas();
        canvas.setBackground(Color.red);
        TestScroll testScroll = new TestScroll(canvas);
        jFrame.getContentPane().add(testScroll);
        jFrame.setSize(400, 400);
        jFrame.setVisible(true);
       
        canvas.repaint();
    }
}

class MyCanvas extends Canvas
{
    public void update(Graphics g)
    {
        g.drawString("Hello Amigos", 10, 10);
    }
}

Best regards
Fredrik
Avatar billede ulrikm Nybegynder
29. maj 2011 - 19:33 #1
Swing and AWT usually don't mix well.

Since you are in a Swing context (JFrame,JPanel,JScrollPane), you should use JPanel in stead of Canvas. In an AWT context, you could use ScrollPane with Canvas.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester