swing 之FlowLayout 实现自动换行和滚动条添加

原文:http://blog.csdn.net/u011393661/article/details/50456022

[java]  view plain  copy
  1. package guitest;  
  2.   
  3. import java.awt.*;  
  4. import java.awt.event.*;  
  5.   
  6. import javax.swing.*;  
  7.   
  8. public class SimpleTest extends JFrame {  
  9.       
  10.       
  11.     private JButton button;  
  12.     private JPanel buttonPanel, imagePanel;  
  13.     private JScrollPane scrollPane;  
  14.     double count = 0;  
  15.   
  16.     public SimpleTest(int xPixels, int yPixels) {  
  17.         super("Add Image");  
  18.   
  19.         button = new JButton("Add Image");  
  20.         button.setPreferredSize(new Dimension(8025));  
  21.         button.setMargin(new Insets(0505));  
  22.   
  23.           
  24.         imagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 55));  
  25.         scrollPane = new JScrollPane(imagePanel);  
  26.         imagePanel.setPreferredSize(new Dimension(xPixels, yPixels));// 这是关键的2句  
  27.         scrollPane.setPreferredSize(new Dimension(xPixels, yPixels));  
  28.         System.out.println(imagePanel.getHeight() + " OOO  "  
  29.                 + imagePanel.getWidth());  
  30.   
  31.         button.addActionListener(new ActionListener() {  
  32.             public void actionPerformed(ActionEvent e) {  
  33.                 count++;  
  34.                 System.out.println("dfdsds");  
  35.                 // label = new JLabel(image);  
  36.                 JButton button = new JButton("bu" + count);  
  37.                 button.setPreferredSize(new Dimension(200200));  
  38.                 imagePanel.add(button);  
  39.                 //validate();  
  40.   
  41.                 System.out.println(imagePanel.getHeight() + " "  
  42.                         + imagePanel.getWidth());  
  43.                 int column = (imagePanel.getWidth()) / 200;  
  44.                 System.out.println("count " + count);  
  45.                 System.out.println("列数  " + column);  
  46.                 double row = count / column;  
  47.                 System.out.println("行数 " + row);  
  48.                 int d = (int) Math.ceil(row);  
  49.                 System.out.println("行数进一 " +d );  
  50.                 imagePanel.setPreferredSize(new Dimension(500,(int) (d * 205)));//  
  51.                 //  
  52.                 imagePanel.revalidate();  
  53.                 imagePanel.repaint();  
  54.                   
  55.             }  
  56.         });  
  57.   
  58.         buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 00));  
  59.         buttonPanel.add(button);  
  60.   
  61.         add(buttonPanel, BorderLayout.NORTH);  
  62.         add(scrollPane, BorderLayout.CENTER);  
  63.   
  64.         setSize(xPixels, yPixels);  
  65.         setVisible(true);  
  66.     }  
  67.   
  68.     public static void main(String[] args) {  
  69.         new SimpleTest(320400);  
  70.     }  
  71. }  

话不多说直接上代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值