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

📄 graphictextcellrenderer.java

📁 一个用java写的地震分析软件(无源码)-used to write a seismic analysis software (without source)
💻 JAVA
字号:
package org.trinet.util.graphics.table;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import org.trinet.util.*;

    public class GraphicTextCellRenderer extends JPanel implements TableCellRenderer {
	static final int borderWidth = 2;
	final int height;
	final int baseline;
	final int maxCharWidth;

	int width;
	Color defaultBackgroundColor;
	Color selectedBackgroundColor;

	Color background;
	Color foreground = Color.black;

	String text = "";

	public GraphicTextCellRenderer(FontMetrics metrics, Color defaultBackgroundColor, Color selectedBackgroundColor) {
	    super();
	    baseline = metrics.getAscent() + borderWidth;
	    this.height = metrics.getHeight() + (2 * borderWidth);
//	    this.width = height; // not necessary, arbitrary here
	    this.defaultBackgroundColor = defaultBackgroundColor;
	    this.selectedBackgroundColor = selectedBackgroundColor;
	    this.maxCharWidth = metrics.charWidth('M');
	}

	public void setSelectedColor(Color selectedBackgroundColor) {
	    this.selectedBackgroundColor = selectedBackgroundColor;
	}

	public void setBackgroundColor(Color defaultBackgroundColor) {
	    this.defaultBackgroundColor = defaultBackgroundColor;
	}

	public Dimension getPreferredSize() {
	    return new Dimension(width, height);
	}
	
	public void paint(Graphics g) {
//			Debug.println("paint: h,w :" + getWidth()  + " "  + getHeight());
	    try {
		g.setColor(background);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(foreground);
		g.drawString(text, borderWidth, baseline);
	    }
	    catch (Exception ex) {
		 System.err.println("Msg: " + ex.getMessage());
		 ex.printStackTrace();
	    }
	}

	public Component getTableCellRendererComponent(JTable table, Object value, boolean selected,
		 boolean focus, int row, int col) {

	    if (selected) {
		background = selectedBackgroundColor;
	    }
	    else {
		background = defaultBackgroundColor;
	    }

	    setValue(value);

	    return this;
	}

	public void setValue(Object value) {
	    if (value == null)  text = "";
	    else text = value.toString();
	    width = text.length() * maxCharWidth + 2 * borderWidth;
	}
    }

⌨️ 快捷键说明

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