1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import static java.awt.EventQueue.invokeLater;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.EventQueue;
import java.text.MessageFormat;
import java.lang.reflect.InvocationTargetException;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalButtonUI;
import javax.swing.table.TableCellRenderer;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 21-Oct-2006
* Time: 10:23:43
*
* THIS IS A REALLY REALLY DIRTY CODE !
* ONLY FOR TEST PURPOSE !
*
*/
public class MyButtonUI extends MetalButtonUI {
private final static MyButtonUI buttonUI = new MyButtonUI();
public static ComponentUI createUI(JComponent c) {
return buttonUI;
}
public void paint(Graphics g, JComponent c) {
Graphics2D g2D = (Graphics2D) g;
Rectangle rec1 = new Rectangle(0, 0, c.getWidth(), c.getHeight());
g2D.setColor(Color.WHITE);
g2D.fill(rec1);
super.paintText(g, c, rec1, ((AbstractButton) c).getText());
}
/**
* @param args
*/
public static void main(String[] args) {
/* First put the following try/catch in comment. (stop your comment just
before next comment).
See what's happen. (you should see the default OceanTheme in action :-)) ).
Uncomment these lines again, and you should see the button UI (MyButtonUI) in action.
*/
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
UIManager.put("ButtonUI", "MyButtonUI");
}
});
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
/* comment till here */
invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame("This is my frame");
Object[][]o = {
{"Hello", "The", "World"},
{"I'm", "dizzy", "this morning"}
};
String[] columnNames = {"1", "2", "3"};
JTable myTable = new JTable(o, columnNames) {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
return new JButton(MessageFormat.format("row {0}, col {1}", new Object[]{row, column}));
}
};
myFrame.add(new JScrollPane(myTable));
myFrame.add(new JButton("I am a button"), BorderLayout.SOUTH);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
});
}
} |
Partager