阅读背景:

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

来源:互联网 

package guitest;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SimpleTest extends JFrame {
	
	
	private JButton button;
	private JPanel buttonPanel, imagePanel;
	private JScrollPane scrollPane;
	double count = 0;

	public SimpleTest(int xPixels, int yPixels) {
		super("Add Image");

		button = new JButton("Add Image");
		button.setPreferredSize(new Dimension(80, 25));
		button.setMargin(new Insets(0, 5, 0, 5));

		
		imagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
		scrollPane = new JScrollPane(imagePanel);
		imagePanel.setPreferredSize(new Dimension(xPixels, yPixels));// 这是关键的2句
		scrollPane.setPreferredSize(new Dimension(xPixels, yPixels));
		System.out.println(imagePanel.getHeight() + " OOO  "
				+ imagePanel.getWidth());

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				count++;
				System.out.println("dfdsds");
				// label = new JLabel(image);
				JButton button = new JButton("bu" + count);
				button.setPreferredSize(new Dimension(200, 200));
				imagePanel.add(button);
				//validate();

				System.out.println(imagePanel.getHeight() + " "
						+ imagePanel.getWidth());
				int column = (imagePanel.getWidth()) / 200;
				System.out.println("count " + count);
				System.out.println("列数  " + column);
				double row = count / column;
				System.out.println("行数 " + row);
				int d = (int) Math.ceil(row);
			    System.out.println("行数进一 " +d );
				imagePanel.setPreferredSize(new Dimension(500,(int) (d * 205)));//
				//
				imagePanel.revalidate();
				imagePanel.repaint();
				
			}
		});

		buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		buttonPanel.add(button);

		add(buttonPanel, BorderLayout.NORTH);
		add(scrollPane, BorderLayout.CENTER);

		setSize(xPixels, yPixels);
		setVisible(true);
	}

	public static void main(String[] args) {
		new SimpleTest(320, 400);
	}
}package guitest;

import java.awt.*;
import 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: