zoukankan      html  css  js  c++  java
  • JScrollPane中添加JPanel不出现滚动条

    解决方法:

    主要是使用JPanel的setPreferredSize方法进行首选大小的设置,同时保证宽高大于JScrollPane的宽高

    例如:

    import java.awt.Dimension;
    import java.awt.FlowLayout;

    import javax.swing.*;

    public class JScrollPaneAndJPanel extends JFrame {
     public JScrollPaneAndJPanel() {
      super("TestJScrollPane");
      this.setLayout(null);
      this.setBounds(200, 200, 300, 300);
      JPanel panel = new JPanel();
      panel.setPreferredSize(new Dimension(200,100));//主要是这句代码,设置panel的首选大小,同时保证宽高大于JScrollPane的宽高,这样下面的JScrollPane才会出现滚动条
      JButton button1  = new JButton("1");  
      panel.add(button1);
      JButton button2  = new JButton("2");  
      panel.add(button2);
      JButton button3  = new JButton("3");  
      panel.add(button3);
      JButton button4  = new JButton("4");  
      panel.add(button4);
      JButton button5  = new JButton("5");  
      panel.add(button5);
      JButton button6  = new JButton("6");  
      panel.add(button6);
      JButton button7  = new JButton("7");  
      panel.add(button7);
      JScrollPane scrollPane = new JScrollPane(panel);
      scrollPane.setBounds(10, 10, 175, 70);
      this.getContentPane().add(scrollPane);
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     }
     public static void main(String[] args) {
      new JScrollPaneAndJPanel();
     }
    }

  • 相关阅读:
    数据库简介
    计算机网络OSI七层协议
    信息论知识点(绪论)
    Wireshark抓取HTTP数据包
    配置FileZilla FTP服务器
    Redis集群搭建的几种方式
    Redis单个分片高可用&哨兵集群
    Redis哈希一致性&对应API操作
    MapReduce实现好友推荐
    window下使用IDEA远程调试伪分布式hadoop集群
  • 原文地址:https://www.cnblogs.com/tianguook/p/2410807.html
Copyright © 2011-2022 走看看