JDesktopPane 滚动条

对于现实JDesktopPane 现实滚动条有如下方法:

 

package test;

import javax.swing.*;

public class TJInternalFrame extends JFrame {
	Container container;
	JButton button;
	JDesktopPane desktop;
	JInternalFrame internalFrame;

	static int frameCount = 0;
	static final int xOffSet = 1;
	static final int yOffSet = 150;

	// 1.Constructor of the frame class.
	public TJInternalFrame() {
		// 2.Give title to the frame and get its content pane
		super("TJInternalFrame");
		container = this.getContentPane();

		// 3.Create a button and add it at the lower portion of frame;
		// also add an action listener.
		button = new JButton("Click to Create More Internal Frames");
		button.addActionListener(new ButtonListener());
		container.add(button, BorderLayout.SOUTH);

		// 方法一:JDesktopPane显示滚动条!!!!!!
		// desktop = new JDesktopPane();// holds the internal frame
		// JScrollPane scrollPane = new JScrollPane();
		// scrollPane.getViewport().setView(desktop);
		//
		// desktop.setPreferredSize(new Dimension(1600, 2200)); // very
		// important
		//
		// container.add(scrollPane);// add the desktop to the main frame

		// 方法二:JDesktopPane显示滚动条!需要新建一个JDesktopPane的继承类JDesktop。!!!!!
		// ================================================================
		desktop = new JDesktop();
		JScrollPane scrollpane = new JScrollPane(desktop,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		container.add(scrollpane);
		// ================================================================
		createInternalFrame();// create an internal frame

		// 5.Add the window listener, set the frame size, default close
		// operation and make the frame visible.
		addWindowListener(new WindowEventHandler());
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setSize(1060, 708);
		setVisible(true);
	}

	// 6.Creates an internal frame and adds it to the desktop pane.
	// Takes care of displaying frames with overlap offsets when called
	// multiple times.
	public void createInternalFrame() {

		// 7.Use a suitable internal frame constructor.
		JInternalFrame iFrame = new JInternalFrame("Internal Frame - "
				+ frameCount, false, false, false, false);
		iFrame.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		((BasicInternalFrameUI) iFrame.getUI()).setNorthPane(null);
		// 8.Set the location and size, and add it to the desktop pane.
		iFrame.setLocation(xOffSet * frameCount, yOffSet * frameCount);
		frameCount++;
		iFrame.setSize(800, 150);
		iFrame.setVisible(true);
		desktop.add(iFrame);
		// 9.Let the frame be selected.
		try {
			iFrame.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {
			System.out.println("Exception while selecting an internal frame");
		}
	}

	// 10.The button(action) listener.
	class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			createInternalFrame();
		}
	}

	// 11.The listener class to handle closing of the frame.
	class WindowEventHandler extends WindowAdapter {
		public void windowClosing(WindowEvent evt) {
			System.exit(0);
		}
	}

	// 12.The main method.
	public static void main(String[] args) {
		String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(lookAndFeel);

		} catch (Exception e) {
			e.printStackTrace();
		}
		TJInternalFrame frame = new TJInternalFrame();
	}
}

 

 JDesktopPane的继承类JDesktop

package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JDesktop extends JDesktopPane
{
   public void paint(Graphics g)
   {
       super.paint(g);
       Dimension d = preferredSizeOfAllFrames();
       this.setPreferredSize(d);
       this.revalidate();
   }
 /**
  * @return 返回最佳desktop尺寸..
  */
   public Dimension preferredSizeOfAllFrames()
   {
       JInternalFrame [] array = getAllFrames();
       int maxX = 0;
       int maxY = 0;
       for (int i = 0; i < array.length; i++)
       {
           if ( array[ i ].isVisible() )
           {
               int cx;
               cx = array[i].getX();
               int x = cx + array[i].getWidth();
               if (x > maxX) maxX = x;
               int cy;
               cy = array[i].getY();
               int y = cy + array[i].getHeight();
               if (y > maxY) maxY = y;
           }
       }
       return new Dimension(maxX, maxY);
   }
}
 

 

你可能感兴趣的:(swing)