JScrollPane and JPanel

拈花ヽ惹草 提交于 2021-02-16 13:44:45

问题


So, I'm making a kind of text editor, and I need a JScrollPane for vertical navigation. But I can't get it to work.

I have read every freaking tutorial on first ten pages of google results, and I can't get it to work.

Lets say I have JFrame (size 1000x800). I want to put a JPanel (1000x2000) in it so that it horizontally alignes with the JFrame. I want to stick a simple scroll bar to the right side of the JPanel so I can get to the rest of it.

I have reduced sizes, I have added JPanel to JScrollBar and vice versa, added one of them to JFrame, both, none, but nothing.

So, at this point, I wouldn't mind a couple of lines of finished code...

EDIT: Fine, here's the code...

mWindow = new JFrame(lang.getString("title"));
mWindow.setSize(1000, 800);
mWindow.setLocationRelativeTo(null);
mWindow.setResizable(false);
mWindow.setLayout(null);
mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mWindow.setVisible(true);

workspace = new JPanel();
workspace.setBounds(0,0, 1000, 1203);
workspace.setBackground(Color.RED);

scroll = new JScrollPane(workspace, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setBounds(0, 20, 600, 600);
//scroll.setLayout(null);
mWindow.getContentPane().add(scroll);
mWindow.repaint();
mWindow.validate();

That shows a part of JPanel (600X600, (JScrollPane size)), and shows scrollbars, but isn't scrollable


回答1:


So, I did this really quick test and it works fine for me...

public class TestPane extends JPanel {

    public TestPane() {

        setBorder(new LineBorder(Color.RED));
        // This is for demonstration purposes only
        // One should always rely on the layout manager
        // to define this value
        // Thank kleopatra 
        setPreferredSize(new Dimension(1000, 1000));

    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        FontMetrics fm = g.getFontMetrics();

        Dimension size = getPreferredSize();
        String text = "Pref: " + size.width + "x" + size.height;
        g.drawString(text, 0, fm.getAscent());

        size = getSize();
        text = "Size: " + size.width + "x" + size.height;
        g.drawString(text, 0, fm.getHeight() + fm.getAscent());

    }

}

And the test frame

public class TestFrame {

    public static void main(String args[]) {

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JScrollPane scroll = new JScrollPane(new TestPane());

        frame.add(scroll);

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

}

Which produces this:

Scroll away

On a side note, I don't know why people insist on using null layouts, they just cause more trouble and heart ache then they're worth. Take the time to find some simple layout managers. I hate VB for a lot of reasons, but layout management is at the top of my list, IMHO




回答2:


Try by applying

setPreferredSize(new Dimension());

method on your panel, instead of setSize() method.

Like this:

import java.awt.Dimension;

import javax.swing.*;
public class Example extends JFrame{

public static void main(String[] args) {
    Example ex = new Example();
    JPanel panel = new JPanel();
    JScrollPane sc = new JScrollPane(panel);
    panel.setPreferredSize(new Dimension(800,600));
    ex.getContentPane().add(sc);
    ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ex.pack();
    ex.setVisible(true);
}

}



回答3:


I would do:

  1. A border layout so the scroll pane fits in all the room of the frane's content pane;
  2. A pack at the end for layouting;
  3. setVisible last;
  4. setPreferredSize if nothing helps.

    mWindow = new JFrame(lang.getString("title"));
    mWindow.setSize(1000, 800);
    mWindow.setLocationRelativeTo(null);
    mWindow.setResizable(false);
    mWindow.setLayout(new BorderLayout());
    mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    workspace = new JPanel();
    workspace.setBounds(0,0, 1000, 1203);
    workspace.setPreferredSize(new Dimension(1000,1203));
    
    workspace.setBackground(Color.RED);
    
    scroll = new JScrollPane(workspace, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setBounds(0, 20, 600, 600);
    mWindow.getContentPane().add(scroll, BorderLayout.CENTER);
    mWindow.pack();
    mWindow.setVisible(true);
    



回答4:


why don't you try something like this and call it from your class that creates your frame.

public JPanel createLPanelWithScroller() {

      Dimension outter_dem = new Dimension(600, 600); //width,height
      Dimension inner_panel_dem = new Dimension(1000, 1203);  
      Dimension scroll_dem = new Dimension(600, 600); 

      JPanel outter_panel = new JPanel(new FlowLayout());
 outter_panel.setPreferredSize(outter_panel_dem);       

 JPanel inner_panel = new JPanel(new FlowLayout());
 inner_panel.setPreferredSize(inner_panel_dem);

 JScrollPane scroller = new JScrollPane(list_panel); 
 scroller.setHorizontalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 scroller.setPreferredSize(scroll_dem);

 outer_panel.add(list_scroller);

 return outer_panel;
}


来源:https://stackoverflow.com/questions/12014427/jscrollpane-and-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!