简体   繁体   English

如何在JFrame的JComponent上添加滚动功能?

[英]How to add scrolling feature to a JComponent on a JFrame?

My GUI consists of a Diagram class which extends JFrame. 我的GUI包含一个扩展JFrame的Diagram类。 I've created a different class called DrawingTool which extends JComponent. 我创建了另一个名为DrawingTool的类,该类扩展了JComponent。 The DrawingTool class is like a canvas area for users to drop and drag shapes. DrawingTool类就像一个供用户拖放形状的画布区域。 I've also added a button panel at the bottom of the JFrame for the users to click various buttons to choose their desired shape and control actions. 我还在JFrame的底部添加了一个按钮面板,供用户单击各种按钮以选择所需的形状和控制动作。 I've added the button panel and an instance of the DrawingTool class to the Diagram class. 我已经将按钮面板和DrawingTool类的实例添加到了Diagram类。 How do I make the canvas area (DrawingTool) scrollable? 如何使画布区域(DrawingTool)可滚动? The way I have attempted it is not working, I know I am missing something. 我尝试的方法不起作用,我知道我丢失了一些东西。

Here is the Diagram class: 这是图类:

public class Diagram extends JFrame {
JButton serverButton, vipButton, arrowButton, undoButton, dragButton, loadButton, submitButton;
JButton applicationButton;
int currentAction = 1;
Graphics2D graphSettings;
Color strokeColor = Color.BLUE, fillColor = Color.BLACK;

/**
 * Constructor to generate new diagram with empty drawing board and button
 * panel.
 */
public Diagram() {
    // Define the defaults for the JFrame

    this.setSize(1000, 1000);
    this.setTitle("Diagram Tool");
    //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel buttonPanel = new JPanel();

    // Swing box that will hold all the buttons

    Box theBox = Box.createHorizontalBox();

    // Make all the buttons in makeButtons by calling helper function

    serverButton = makeButtons("Server", 2);
    vipButton = makeButtons("VIPs", 3);
    arrowButton = makeButtons("Arrow", 4);
    undoButton = makeButtons("Undo", 5);
    dragButton = makeButtons("Drag", 6);
    loadButton = makeButtons("Load", 11);
    applicationButton = makeButtons("Application", 8);
    submitButton = makeButtons("Submit", 12);

    // Add the buttons to the box

    theBox.add(serverButton);
    theBox.add(vipButton);
    theBox.add(applicationButton);
    theBox.add(arrowButton);
    theBox.add(undoButton);
    theBox.add(dragButton);
    theBox.add(loadButton);
    theBox.add(submitButton);

    // Add the box of buttons to the panel



    buttonPanel.add(theBox);

    // Position the buttons in the bottom of the frame

    JPanel container=new JPanel();
    container.add(new DrawingBoard(),BorderLayout.CENTER);
    JScrollPane jsp=new JScrollPane(container);
    this.add(buttonPanel, BorderLayout.SOUTH);
    this.add(jsp);

    // Make the drawing area take up the rest of the frame

    // Show the frame

    this.setVisible(true);
}

Here is the DrawingBoard class: 这是DrawingBoard类:

private class DrawingBoard extends JComponent implements MouseListener, MouseMotionListener {
    //declare variables

    /**
     * Constructor to initialize the drawing board
     */
    public DrawingBoard() {
        addMouseListener(this);
        addMouseMotionListener(this);

    //  initializeCanvas();
    }
   //Rest of the code for DrawingBoard 
  }

This is how it looks now. 这就是现在的样子。 I'd like to make the gray canvas area scrollable. 我想使灰色画布区域可滚动。

Diagram Image 图表图像

https://i.stack.imgur.com/nF9oL.png

What MadProgrammer said in the comments is just about right. MadProgrammer在评论中说的是正确的。 You need to set some informations so your ScrollPanel knows how to behave. 您需要设置一些信息,以便您的ScrollPanel知道如何运行。 What is it's own size, the size of the components inside it, etc. 它的自身大小是多少,内部的组件大小等等。

So normally you'll have a ContentPane , and inside of it panes with your content. 因此,通常您将拥有一个ContentPane ,并且在其内部具有内容窗格。 To do a scrollable pane you only need to put the ScrollPane inside of your ContentPane and then set a viewport for your ScrollPane. 要执行可滚动窗格,只需将ScrollPane放在ContentPane内,然后为ScrollPane设置视口。 A little code I used fully functional: 我使用了功能齐全的一些代码:

contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);

JScrollPane scrollPane = new JScrollPane();

//Vertical and Horizontal scroll bar policy is set to choose when the scroll will be visible    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(0, 217, 414, 505);
scrollPane.setPreferredSize(new Dimension(414, 414));

JPanel viewport = new JPanel();
viewport.setLayout(null);
viewport.setBounds(0, 0, 414, 505);

//Create your components here, then:
//viewport.add(component)

viewport.setPreferredSize(new Dimension(414, 150));
scrollPane.setViewportView(viewport);
contentPane.add(scrollPane);

Anything you put inside of your ViewPort will be automaticaly scrolable, if it's size is bigger than the PreferredSize. 如果ViewPort的大小大于PreferredSize,则可以自动对其进行滚动。

Note that all the dimensions I've put is only for example. 请注意,我输入的所有尺寸仅作为示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM