简体   繁体   English

在Java Swing中,当我将JScrollPane添加到我的面板时,没有任何内容出现

[英]In Java Swing when I add my JScrollPane to my panel nothing appears

After some years working in Java in the back end I ended up working in a project to build a GUI and Java Swing is driving me crazy. 在后端使用Java工作了几年后,我最终在一个构建GUI的项目中工作,Java Swing让我发疯。

So I am doing some tests with the JScrollPane, because I have a JPanel that is too big to fit in the screen. 所以我正在使用JScrollPane进行一些测试,因为我有一个太大而不适合屏幕的JPanel。 In the following example I am adding couple buttons to a JPanel and then creating a JScrollPane with the JPanel, but nothing appears in the screen. 在下面的示例中,我将几个按钮添加到JPanel,然后使用JPanel创建一个JScrollPane,但屏幕上没有任何内容。

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestScrollPane extends JDialog {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            TestScrollPane dialog = new TestScrollPane();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public TestScrollPane() {
        setBounds(100, 100, 857, 541);
        getContentPane().setLayout(null);
        {
            JPanel panel = new JPanel();
            panel.setBounds(131, 167, 141, 221);
            getContentPane().add(panel);
            panel.setLayout(null);
            {
                JButton btnNewButton = new JButton("New button");
                btnNewButton.setBounds(0, 0, 115, 29);
                panel.add(btnNewButton);
            }
            {
                JButton btnNewButton_1 = new JButton("New button");
                btnNewButton_1.setBounds(26, 192, 115, 29);
                panel.add(btnNewButton_1);
            }

             JScrollPane jsp = new JScrollPane(panel);
             getContentPane().add(jsp);

        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setBounds(0, 446, 835, 39);
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }       
    }

}

I don't understand why is not appearing. 我不明白为什么不出现。 I create the JPanel I add the buttons, and the the JScrollPane, which I add to the window. 我创建了JPanel我添加了按钮,以及我添加到窗口的JScrollPane。 I am using WindwBuilder Pro that's why the code looks so weird. 我正在使用WindwBuilder Pro,这就是为什么代码看起来很奇怪。

Thanks. 谢谢。

I have changed 我变了

getContentPane().setLayout(null);
panel.setLayout(null);

to

getContentPane().setLayout(new FlowLayout());
panel.setLayout(new FlowLayout());

Now I see all 4 buttons. 现在我看到所有4个按钮。

The components of a panel must be lay out by code if not using a layout manager, that is, when using setLayout(null) . 如果不使用布局管理器,即使用setLayout(null)时,面板的组件必须按代码布局。 Most components start with dimension zero and will therefore not be displayed. 大多数组件从零开始,因此不会显示。

In above code there is missing the position and dimension of the scroll pane: jsp.setBounds(...) like done with the other components. 在上面的代码中,缺少滚动窗格的位置和尺寸: jsp.setBounds(...)就像使用其他组件一样。

Normally it is not recommend to lay out the components yourself, better use a LayoutManager (eg GridBagLayout, BorderLayout,...) for that. 通常不建议自己布置组件,更好地使用LayoutManager(例如GridBagLayout,BorderLayout,...)。

See Oracle's tutorial: Lesson: Laying Out Components Within a Container 请参阅Oracle教程: 课程:在容器中布置组件

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

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