————————————在JPanel中加进背景图片————————————
//import javax swing
import javax.swing.JPanel;
import javax.swing.ImageIcon;
//import java awt
import java.awt.Graphics;
import java.awt.Dimension;
/**
* This class defines a panel that can draw an image in the background.
* To do this,I overrid the paintCompponent method,that's the
* only thing necessery.
*/
public class ImageBackgroundPanel extends JPanel {
private ImageIcon image = null;
public ImageBackgroundPanel(ImageIcon image) {
this.image = image;
}
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Dimension d = getSize();
for( int x = 0; x < d.width; x += image.getIconWidth() )
for( int y = 0; y < d.height; y += image.getIconHeight() )
g.drawImage( image.getImage(), x, y, null, null );
}
}
————————————自己load一个主页————————————
package org.PhotoResizer.test;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class HyperLink
extends JFrame {
JEditorPane jEditorPane1 = new JEditorPane();
JScrollPane jScrollPane1 = new JScrollPane();
public HyperLink() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
HyperLink HyperLink = new HyperLink();
HyperLink.show();
}
private void jbInit() throws Exception {
jEditorPane1.setText("Hello");
// String url="file:"+System.getProperty("user.dir")+System.getProperty("file.separator")+
// "java.util.Date.html";
// try {
// jEditorPane1.setPage(url);
// }
// catch (Exception ex) {
// throw new Exception(ex);
// }
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jEditorPane1, null);
jEditorPane1.setEditable(false);
Runtime rt = Runtime.getRuntime();
try {
//change here of the URL
jEditorPane1.setPage("http://www.163.com");
}
catch (Exception ex) {
}
jEditorPane1.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent hle) {
if (hle.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
Runtime rt = Runtime.getRuntime();
try {
jEditorPane1.setPage(hle.getURL());
//如果你想要连接系统浏览器的话。。。
// rt.exec("cmd /c start " + hle.getURL());
}
catch (IOException e1) {
System.err.println(e1);
}
}
}
});
}
}
————————————让窗口始终在前————————————
1,The upstair tell you a best way. 2,You can extends JWindow
override show() like this:
public void show()
{
super.show();
this.requestFocus();
}
and then add a window listener for
the focus lost event:
addFocusListener (new java.awt.event.FocusAdapter () {
public void focusLost (java.awt.event.FocusEvent evt) {
this.toFront();
}
}
);
It seems to work for me...
3.You can also try to use thread, I recommend this way.
/**
* Call this from class consructor
*/
public void initialize() {
TopThread top = new TopThread();
top.start();
}
/**
* Keep JWindow on top (inner class)
*/
class TopThread extends Thread {
public void run() {
while(true) {
toFront();
/**
* Let 10 milliseconds for other code to execute
*/
try {
Thread.sleep(10);
}
catch(Exception e) {
// do what you wanna do
}
}
}
}
You can see:http://forum.java.sun.com/thread.jsp?forum=57&thread=166992
That's some others discuss it.
————————————带行号的jtable类————————————
用另一个可以显示行号的JTable或JList做JScrollPane的setRowHeaderView(Component view)
下面是我临时做的一个最简单的例子
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class LineNumberTable extends JFrame {
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e){
}
LineNumberTable jl=new LineNumberTable();
jl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jl.setBounds(100,100,600,400);
jl.show();
}
public LineNumberTable() {
Container co = super.getContentPane();
co.setLayout(new BorderLayout());
JTable table = new JTable(100,5);
JScrollPane sp = new JScrollPane(table);
RowHeader rowHeader=new RowHeader(table);
sp.setRowHeaderView(rowHeader);
sp.getRowHeader().setPreferredSize(new Dimension(35,10));
co.add(sp,BorderLayout.CENTER);
}
}
class RowHeader extends JTable{
public RowHeader(JTable table) {
LineNumberModel listModel=new LineNumberModel(table.getModel());
this.setModel(listModel);
this.setRowHeight(table.getRowHeight());
this.getColumn("").setCellRenderer(new RowHeaderRenderer());
this.setAutoscrolls(false);
}
}
class LineNumberModel extends AbstractTableModel{
TableModel model;
public LineNumberModel(TableModel mod){
this.model=mod;
}
public int getRowCount(){
return model.getRowCount();
}
public int getColumnCount(){
return 1;
}
public String getColumnName(int columnIndex){
return "";
}
public Object getValueAt(int rowIndex,
int columnIndex){
return "";
}
public boolean isCellEditable(int row, int column) {
return false;
}
}
class RowHeaderRenderer extends JButton implements TableCellRenderer {
Insets zeroInsets = new Insets(0,0,0,0);
RowHeaderRenderer() {
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
}
public Insets getInsets(){
return zeroInsets;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
this.setText(""+(row+1));
return this;
}
}
———————————— ————————————
———————————— ————————————
———————————— ————————————
———————————— ————————————