⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glightbox.java

📁 用Swing实现的CHM制作工具
💻 JAVA
字号:
package g2w.app.gchm.lib;

import g2w.app.gchm.GChm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * A box with a title bar and a hide button.
 * 
 * @author GreatGhoul
 * @version 006 2009-3-20 17:31:43
 */
public class GLightBox extends JPanel {
	private JLabel title;
	private JButton hide;
	
	/** The panel is used to fix the border style. */
	private JPanel contentPane;
	
	public GLightBox(String title) {
		super();
		initComponents(title, true);
	}
	
	public GLightBox(String title, boolean showHideButton) {
		super();
		initComponents(title, showHideButton);
	}
	
	public void setTitle(String title) {
		this.title.setText(title);
	}

	public String getTitle() {
		return title.getText();
	}
	
	private void initComponents(String title, boolean showHideButton) {
		// Configure the component.
		setLayout(new BorderLayout());
		
		contentPane = new JPanel();
		contentPane.setBorder(BorderFactory.createLineBorder(new Color(182, 193, 201), 1));
		contentPane.setLayout(new BorderLayout());
		// Add the title bar.
		contentPane.add(createTitleBar(title, showHideButton), BorderLayout.NORTH);
		add(contentPane, BorderLayout.CENTER);
	}
	
	/**
	 * Set the content view.
	 * 
	 * @param view The content view.
	 */
	public void setView(Component view) {
		BorderLayout layout = (BorderLayout) contentPane.getLayout();
		Component oldView = layout.getLayoutComponent(BorderLayout.CENTER);
		if (oldView != null) {
			contentPane.remove(oldView);
		}
		if (view != null)
			contentPane.add(view, BorderLayout.CENTER);
		
		oldView = null;
		contentPane.revalidate();
		contentPane.repaint();
	}
	
	/**
	 * Get the content view.
	 * 
	 * @return The content view component, null if empty.
	 */
	public Component getView() {
		BorderLayout layout = (BorderLayout) contentPane.getLayout();
		return layout.getLayoutComponent(BorderLayout.CENTER);
	}
	
	public void setHideButtonVisible(boolean aFlag) {
		hide.setVisible(aFlag);
	}
	
	/**
	 * Create the title bar.
	 * 
	 * @param showHideButton Indicate whether to show the hide button or not.
	 * @param title          The default title of the light box.
	 * 
	 * @return The title bar.
	 */
	private JPanel createTitleBar(String title, boolean showHideButton) {
		JPanel titleBar = new JPanel();
		
		// Configure the title bar.
		titleBar.setLayout(new BoxLayout(titleBar, BoxLayout.X_AXIS));
//		titleBar.setBorder(BorderFactory.createEtchedBorder());
		titleBar.setBackground(new Color(206, 218, 227));
		
		// Add the title label.
		this.title = new JLabel(title, SwingConstants.LEFT);
		titleBar.add(this.title);
		
		titleBar.add(Box.createHorizontalStrut(5));
		titleBar.add(Box.createHorizontalGlue());
		
		// Add the hide button.
		hide = new JButton(new AbstractAction(null, 
				GChm.getImage("/resources/module_close.png")) {
			@Override
			public void actionPerformed(ActionEvent e) {
				GLightBox.this.setVisible(false);
			}
		});
		hide.setPreferredSize(new Dimension(20, 20));
		hide.setVisible(showHideButton);
		titleBar.add(hide);
		
		return titleBar;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -