JScrollPane滚动问题

时间:2012-08-28 12:18:10

标签: java swing layout jscrollpane

我创建了一个JScrollPane的程序。但是,JScrollPane似乎没有做到应该做的事情。虽然组件的大小比JScrollPane大。

,但它不会滚动

从我的程序中产生一个SSCCE将是痛苦的,所以我将给你整个程序与重要的地方的评论标记。 (重要和重要的结果)评论中的任何内容都不应影响该问题。

启动程序时,单击文件>新建,然后单击对话框中的确定。这将绘制有问题的GUI。

P.S。这里只有高度很重要,宽度方面的一切都很有效。

package core;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

//IMPORTANT!
@SuppressWarnings("serial")
class Page extends JPanel
{
public int content;

public Page(JPanel panel, int index)
{
    setMaximumSize(new Dimension(794, 1123));
    setAlignmentX(Component.CENTER_ALIGNMENT);
    setBackground(Color.WHITE);

    panel.add(this, index);

    content = 0;
}
}
/END OF IMPORTANT!

public class Main
{

public static Properties config;
public static Locale currentLocale;
public static ResourceBundle lang;

public static JFrame mWindow;

public static JMenuBar menu;
public static JMenu menuFile, menuEdit;
public static JMenuItem itmNew, itmClose, itmLoad, itmSave, itmSaveAs, itmExit, itmCut, itmCopy, itmPaste, itmProperties; 

public static JDialog newDoc;
public static JLabel newDocText1, newDocText2;
public static JRadioButton questions, list;
public static ButtonGroup newDocButtons;
public static JButton newDocOk;
public static Boolean firstSegment;

public static JPanel workspace;
public static JScrollPane scroll;
public static ArrayList<Page> pages;

//IGNORE WHOLE METHOD, HAS NOTHING TO DO WITH THE PROBLEM
public static void newDocumentForm()
{
    //new document dialog
    mWindow.setEnabled(false);

    newDoc = new JDialog(mWindow, lang.getString("newDoc"));
    newDoc.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    newDoc.addWindowListener(new WindowListener ()
    {
        public void windowActivated(WindowEvent arg0) {}
        public void windowClosing(WindowEvent arg0)
        {
            mWindow.toFront();

            newDocText1 = null;
            newDocText2 = null;
            questions = null;
            list = null;
            newDocButtons = null;
            newDocOk = null;
            newDoc.dispose();

            mWindow.setEnabled(true);               
        }

        public void windowClosed(WindowEvent arg0) {}
        public void windowDeactivated(WindowEvent arg0) {}
        public void windowDeiconified(WindowEvent arg0) {}
        public void windowIconified(WindowEvent arg0) {}
        public void windowOpened(WindowEvent arg0) {}   
    });

    newDoc.setSize(400, 200);
    newDoc.setLocationRelativeTo(null);
    newDoc.setResizable(false);
    newDoc.setLayout(null);
    newDoc.setVisible(true);

    newDocText1 = new JLabel(lang.getString("newDocText1"));
    newDocText1.setBounds(5, 0, 400, 20);

    newDocText2 = new JLabel(lang.getString("newDocText2"));
    newDocText2.setBounds(5, 20, 400, 20);

    newDoc.add(newDocText1);
    newDoc.add(newDocText2);

    firstSegment = true;

    questions = new JRadioButton(lang.getString("questions"));
    questions.setSelected(true);
    questions.setFocusPainted(false);
    questions.setBounds(10, 60, 400, 20);
    questions.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            firstSegment = true;
        }
    });

    list = new JRadioButton(lang.getString("list"));
    list.setFocusPainted(false);
    list.setBounds(10, 80, 400, 20);
    list.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            firstSegment = false;
        }
    });

    newDoc.add(questions);
    newDoc.add(list);

    newDocButtons = new ButtonGroup();
    newDocButtons.add(questions);
    newDocButtons.add(list);

    newDocOk = new JButton(lang.getString("ok"));
    newDocOk.addKeyListener(new KeyListener()
    {
        public void keyPressed(KeyEvent e)
        {
            if (e.getKeyCode() == KeyEvent.VK_ENTER)
            {
                newDocOk.doClick();
            }
        }

        public void keyReleased(KeyEvent e) {}
        public void keyTyped(KeyEvent e) {}
    });

    newDocOk.setFocusPainted(false);
    newDocOk.setBounds(160, 120, 80, 40);
    newDocOk.setMnemonic(KeyEvent.VK_ACCEPT);
    newDocOk.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            createNewDocument();
        }
    });

    newDoc.add(newDocOk);
    newDocOk.requestFocus();
}

public static void createNewDocument()
{
    //dispose of new document dialog
    mWindow.toFront();

    newDocText1 = null;
    newDocText2 = null;
    questions = null;
    list = null;
    newDocButtons = null;
    newDocOk = null;
    newDoc.dispose();

    mWindow.setEnabled(true);

            //IMPORTANT!
    //create document display               
    workspace = new JPanel();
    workspace.setLayout(new BoxLayout(workspace, BoxLayout.PAGE_AXIS));
    workspace.add(new Box.Filler(new Dimension(0, 40), new Dimension(0, 40), new Dimension(0, 40)));
    workspace.setBackground(Color.BLACK);

    scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.getVerticalScrollBar().setUnitIncrement(20);
    scroll.setViewportView(workspace);

    pages = new ArrayList<Page>();

    @SuppressWarnings("unused")
    Page p = new Page(workspace, 1);


    mWindow.add(scroll, BorderLayout.CENTER);
    mWindow.repaint();
    mWindow.validate();
}
    //END OF IMPORTANT

public static void main(String[] args) throws FileNotFoundException, IOException
{   
    //load properties
    config = new Properties();
    config.load(new FileInputStream("src\\config.properties"));

    //load language
    currentLocale = new Locale(config.getProperty("language"), config.getProperty("country"));
    lang = ResourceBundle.getBundle("language", currentLocale);

            //IMPORTANT!
    //create main window
    mWindow = new JFrame(lang.getString("title"));
    mWindow.setSize(1000, 800);
    mWindow.setMinimumSize(new Dimension(1000, 800));
    mWindow.setLocationRelativeTo(null);
    mWindow.setLayout(new BorderLayout());
    mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    mWindow.setVisible(true);
            //END OF IMPORTANT

    //create menu bar
    menu = new JMenuBar();

    menuFile = new JMenu(lang.getString("file"));
    menuEdit = new JMenu(lang.getString("edit"));

    itmNew = new JMenuItem(lang.getString("new"));
    itmNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    itmNew.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            newDocumentForm();
        }
    });

    itmClose = new JMenuItem(lang.getString("close"));
    itmClose.setActionCommand("Close");
    itmClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));

    itmLoad = new JMenuItem(lang.getString("load"));
    itmLoad.setActionCommand("Load");
    itmLoad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));

    itmSave = new JMenuItem(lang.getString("save"));
    itmSave.setActionCommand("Save");
    itmSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));

    itmSaveAs = new JMenuItem(lang.getString("saveAs"));
    itmSaveAs.setActionCommand("SaveAs");
    itmExit = new JMenuItem(lang.getString("exit"));
    itmExit.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //Add confirmation window!
            System.exit(0);
        }
    });


    itmCut = new JMenuItem(lang.getString("cut"));
    itmCut.setActionCommand("Cut");
    itmCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));

    itmCopy = new JMenuItem(lang.getString("copy"));
    itmCopy.setActionCommand("Copy");
    itmCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));

    itmPaste = new JMenuItem(lang.getString("paste"));
    itmPaste.setActionCommand("Paste");
    itmPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));

    itmProperties = new JMenuItem(lang.getString("properties"));
    itmProperties.setActionCommand("properties");
    itmProperties.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));

    menuFile.add(itmNew);
    menuFile.add(itmClose);
    menuFile.addSeparator();
    menuFile.add(itmLoad);
    menuFile.addSeparator();
    menuFile.add(itmSave);
    menuFile.add(itmSaveAs);
    menuFile.addSeparator();
    menuFile.add(itmExit);

    menuEdit.add(itmCut);
    menuEdit.add(itmCopy);
    menuEdit.add(itmPaste);
    menuEdit.addSeparator();
    menuEdit.add(itmProperties);

    menu.add(menuFile);
    menu.add(menuEdit);

    //create actionListener for menus



    mWindow.add(menu, BorderLayout.NORTH);

    mWindow.repaint();
    mWindow.validate();
}
}

1 个答案:

答案 0 :(得分:0)

JScollPane检查preferred size时,您可以设置视口面板的最大大小。

setPreferredSize(new Dimension(794, 1123));