【问题标题】:How to add scrollbar in JFrame with null layout?如何在具有空布局的 JFrame 中添加滚动条?
【发布时间】:2013-09-19 11:27:00
【问题描述】:

我想在我的JFrame 上添加一个垂直滚动条null 布局。

有没有可能?请帮忙!

【问题讨论】:

  • 是的,没有问题阅读官方 Oracle 教程 How to use ScrollPanes for working example,很好地解释了 JScrollPane 是如何工作的,何时何地...... JScrollPane 工作(JScrollbar 可见)
  • 从使用布局管理器开始...
  • Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。

标签: java swing jframe jscrollbar null-layout-manager


【解决方案1】:

只需将JScrollPane 设置为ContentPaneJFrame,正如here 所描述的那样:

public class TabbedPaneTest {
    public static void main(String [] a) {
        final JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        frame.setContentPane(pane);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.setVisible(true);
            }
        });
   }
}

【讨论】:

    【解决方案2】:

    在 Eclipse IDE 中,您可以使用以下代码

    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JLabel;
    
    public class Test {
    
        private JFrame frame;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Test window = new Test();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public Test() {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel container = new JPanel();
            JScrollPane jsp = new JScrollPane(container);
            container.setPreferredSize(new Dimension(500, 250));
            container.setLayout(null);
    
            JLabel lblHelloWorld = new JLabel("Hello World");
            lblHelloWorld.setBounds(10, 11, 101, 14);
            container.add(lblHelloWorld);
    
            frame.getContentPane().add(jsp);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      相关资源
      最近更新 更多