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);
   }
}
 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值