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

📄 jtips.java

📁 一个关于SWING学习的源代码. 大家过来看一看
💻 JAVA
字号:
package com.pepsan.framework;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

/**
 *	Implements a tips/hints system for  SwingStarter.
 *
 *	The properties of the tips are saved and restored by the parent.
 *
 *	This code is based on code published by the Java Developer's Journal
 *	<http://www.sys-con.com/java/index2.html>
 *
 *	Modifications by Pepsan:
 *	- Changed constructor a let creator specify icon file and heading to use.
 *	- All comments added by Pepsan.
 *	- Code to save and restore the Properties created by Pepsan (in parent).
 *	- Also changed the JTextArea call to setText to replaceRange since it seems
 *	  the correct way to use the class.
 *	- Added a call to repaint() in nextTip(). Without this call, in certain
 *	  conditions JTextArea failed to repaint correctly and text on some lines
 *	  would be invisible.
 */

public class JTips extends JFrame 
				implements ActionListener { 
	protected String filename; 
	protected Properties properties; 
	protected Vector tips; 
	protected JButton next, close; 
	protected JCheckBox show; 
	protected JTextArea text;
	protected JPanel tipsPanel, centerPanel;
	 
	/*
	 * filename is the file containing the tips text to be displayed
	 *
	 * properties is a Properties object used to store the index
	 * and state of the "show tips" flag. The parent is responsible for
	 * creating the Properties object or loading it from a file.
	 * The parent should save the Properties object to a file so the
	 * configuration is persistent. It's OK if the Properties object is
	 * empty (e.g. the first time a user runs the program).
	 *
	 * iconFile is a file with a valid graphic to use as the little
	 * icon 
	 *
	 * heading is a string to display at the top of the box, such as
	 * "Did you know?"
	 */
	public JTips(String filename, Properties properties, String iconFile,
		String heading) { 
		super("Tip of the Day");
		
		// Center tips on screen
		int w = 425;
		int h = 280;
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
		int x = (screen.width - w) / 2; 
		int y = (screen.height - h) / 2; 
		setBounds(x, y, w, h); 
		
		this.filename = filename; 
		this.properties = properties; 
		getContentPane().setLayout(new BorderLayout()); 
		tips = new Vector(); 
		readTipFile(); 
		
		JPanel iconPanel = new JPanel(); 
		iconPanel.setLayout(new BorderLayout()); 
		iconPanel.setBackground(Color.gray); 
		iconPanel.setPreferredSize(new Dimension(53, 53)); 
		JLabel icon = new JLabel(new ImageIcon(iconFile));

		icon.setVerticalAlignment(JLabel.CENTER); 
		icon.setHorizontalAlignment(JLabel.CENTER); 
		icon.setPreferredSize(new Dimension(53, 53)); 
		iconPanel.add("North", icon); 
		
		JPanel titlePanel = new JPanel(); 
		JLabel title = new JLabel(heading); 
		title.setBorder(new EmptyBorder(10, 10, 0, 0)); 
		title.setFont(new Font("Helvetica", Font.PLAIN, 18)); 
		titlePanel.setLayout(new BorderLayout()); 
		titlePanel.setBorder(new EdgeBorder(EdgeBorder.SOUTH)); 
		titlePanel.setPreferredSize(new Dimension(46, 46)); 
		titlePanel.add("Center", title); 
		
		text = new TipTextArea(); 		
		text.setBackground(getBackground()); 
		centerPanel = new JPanel(); 
		centerPanel.setLayout(new BorderLayout()); 
		centerPanel.add("North", titlePanel); 
		centerPanel.add("Center", text); 
		
		tipsPanel = new JPanel(); 
		tipsPanel.setLayout(new BorderLayout()); 
		tipsPanel.setBorder( new CompoundBorder( new EmptyBorder(10, 10, 0, 10), 
			new BevelBorder(BevelBorder.LOWERED))); 
		tipsPanel.add("Center", centerPanel); 
		tipsPanel.add("West", iconPanel); 
		getContentPane().add("Center", tipsPanel); 
		
		JPanel buttonPanel = new JPanel(); 
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10)); 
		buttonPanel.add(next = new JButton("Next Tip")); 
		buttonPanel.add(close = new JButton("Close")); 
		
		JPanel showPanel = new JPanel(); 
		showPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10)); 
		showPanel.add(show = new JCheckBox("Show tips at startup", getShow())); 
		
		JPanel navPanel = new JPanel(); 
		navPanel.setLayout(new BorderLayout()); 
		navPanel.add("East", buttonPanel); 
		navPanel.add("West", showPanel); 
		getContentPane().add("South", navPanel); 
		
		next.addActionListener(this); 
		close.addActionListener(this); 
		show.addActionListener(this); 
		nextTip(); 
	} 

	private void increment() { 
		int current = getNext() + 1; 
		if (current >= tips.size()) current = 0; 
		setNext(current); 
	} 

	private void nextTip() { 
		// Either replaceRange or setText can be used
		text.replaceRange((String)tips.elementAt(getNext()), 0, text.getText().length()); 
		//text.setText((String)tips.elementAt(getNext())); 
		
		// I don't think this repaint should be required. However
		// the full text area is not always repainted, for example in certain
		// cases where the number of rows of text changes significantly the first
		// line of text is shown but not the rest
		text.repaint();
		increment(); 
	} 
	
	private int getNext() { 
		String prop = properties.getProperty("tips.index"); 
		if (prop == null) { 
			setNext(0); 
			return 0; 
		} 
		int next = Integer.parseInt(prop); 
		return next; 
	} 
	
	private void setNext(int next) { 
		properties.put("tips.index", "" + next); 
	} 
	
	public boolean getShow() { 
		String prop = properties.getProperty("tips.show"); 
		if (prop == null) { 
			setShow(true); 
			return true; 
		} 
		return prop.equalsIgnoreCase("true"); 
	} 
	
	public void setShow(boolean show) { 
		properties.put("tips.show", "" + show); 
	} 
	
	public void readTipFile() { 
		tips.removeAllElements(); 
		try { 
			FileReader file = new FileReader(filename); 
			BufferedReader input = new BufferedReader(file); 
			String line; 
			while ((line = input.readLine()) != null) { 
				line = replaceParagraphMarkers(line); 
				tips.addElement(line); 
			} 
			input.close(); 
			file.close(); 
		} catch (FileNotFoundException e) { 
			tips.addElement("Tip file '" + filename + "' not found!"); 
		} catch (IOException e) { 
			tips.addElement("Error reading '" + filename + "'"); 
		} 
	} 
	
	public String replaceParagraphMarkers(String line) { 
		StringBuffer buffer = new StringBuffer(line); 
		int pos; 
		while ((pos = line.indexOf("\\p")) > -1) { 
			buffer.setCharAt(pos, '\n'); 
			buffer.setCharAt(pos + 1, '\n'); 
			line = buffer.toString(); 
		} 
		return line; 
	} 
	
	public void actionPerformed(ActionEvent event) { 
		Object source = event.getSource(); 
		if (source == close) { 
			setVisible(false); 
		} 
		if (source == next) { 
			nextTip(); 
		} 
		if (source == show) { 
			setShow(show.isSelected()); 
		} 
	} 
	
	public void startup() {
		if (getShow()) setVisible(true); 
	} 
} 

public class EdgeBorder implements Border, SwingConstants {
	public static final int RAISED = 1; 
	public static final int LOWERED = 2; 
	protected int edge = NORTH; 
	protected int lift = LOWERED; 
	
	public EdgeBorder() { 
		this(NORTH); 
	} 
	
	public EdgeBorder(int edge) { 
	this.edge = edge; 
	} 
	
	public Insets getBorderInsets(Component component) { 
		switch (edge) { 
			case SOUTH: return new Insets(0, 0, 2, 0); 
			case EAST: return new Insets(0, 2, 0, 0); 
			case WEST: return new Insets(0, 0, 0, 2); 
			default: return new Insets(2, 0, 0, 0); 
		} 
	} 
	
	public boolean isBorderOpaque() { 
		return true; 
	} 
	
	public void paintBorder(Component component, 
		Graphics g, int x, int y, int w, int h) { 
		if (lift == RAISED) 
			g.setColor(component.getBackground().brighter()); 
		else 
			g.setColor(component.getBackground().darker()); 
		switch (edge) { 
			case SOUTH: g.drawLine(x, y + h - 2, w, y + h - 2); 
				break; 
			case EAST: g.drawLine(x + w - 2, y, x + w - 2, y + h); 
				break; 
			case WEST: g.drawLine(x + 1, y, x + 1, y + h); 
				break; 
			default: g.drawLine(x, y, x + w, y); 
		} 
		if (lift == RAISED) 
			g.setColor(component.getBackground().darker()); 
		else 
			g.setColor(component.getBackground().brighter()); 
		switch (edge) { 
			case SOUTH: g.drawLine(x, y + h - 1, w, y + h - 1); 
				break; 
			case EAST: g.drawLine(x + w - 1, y, x + w - 1, y + h); 
				break; 
			case WEST: g.drawLine(x + 1, y, x + 1, y + h); 
				break; 
			default: g.drawLine(x, y + 1, x + w, y + 1); 
		} 
	} 
} 

public class TipTextArea extends JTextArea { 
	public TipTextArea() { 
		super(); 
		setFont(new Font("Helvetica", Font.PLAIN, 12)); 
		setBorder(new EmptyBorder(10, 10, 10, 10)); 
		setWrapStyleWord(true); 
		setEditable(false); 
		setLineWrap(true); 
	} 
	
	public boolean isFocusTraversable() { 
		return false; 
	} 
} 

public class BackgroundPanel extends JPanel 
{ 
	public BackgroundPanel() { 
		super(); 
		setOpaque(true); 
	}
	
	public void paintComponent(Graphics g) { 
		int w = getSize().width - 1; 
		int h = getSize().height - 1; 
		for (int y = 0; y < h; y += 10) { 
			g.setColor(Color.blue); 
			g.fillRect(0, y, w, y + 5); 
			g.setColor(Color.blue.darker()); 
			g.fillRect(0, y + 5, w, y + 10); 
		} 
	} 
} 

⌨️ 快捷键说明

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