從代碼(的Java6)
import com.sun.java.swing.Painter;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ScrollDemo extends JFrame {
private static final long serialVersionUID = 1L;
public ScrollDemo() {
JTextField field = new JTextField();
String[] columnNames = {"Column"};
Object[][] data = {
{"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"},
{"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"},
{"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"},
{"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"},
{"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"},};
add(new JScrollPane(new JTable(data, columnNames)));
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception fail) {
}
UIManager.getLookAndFeelDefaults().put("ScrollBar:ScrollBarThumb[Enabled].backgroundPainter",
new FillPainter(new Color(255, 169, 191)));
UIManager.getLookAndFeelDefaults().put("ScrollBar:ScrollBarThumb[MouseOver].backgroundPainter",
new FillPainter(new Color(127, 255, 191)));
UIManager.getLookAndFeelDefaults().put("ScrollBar:ScrollBarTrack[Enabled].backgroundPainter",
new FillPainter(new Color(0, 212, 255)));
UIManager.getLookAndFeelDefaults().put("ScrollBar:\"ScrollBar.button\".size", 20);
//UIManager.getLookAndFeelDefaults().put("ScrollBar.decrementButtonGap", 10);
//UIManager.getLookAndFeelDefaults().put("ScrollBar.incrementButtonGap", 10);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ScrollDemo scrollDemo = new ScrollDemo();
}
});
}
}
class FillPainter implements Painter<JComponent> {
private final Color color;
public FillPainter(Color c) {
color = c;
}
@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.setColor(color);
g.fillRect(0, 0, width - 1, height - 1);
}
}
組件的名稱使用層次結構「chat.tabbedpane.panel.label」,所以可以這樣做:如果名稱包含「chat」,則繪製scrollbar1。我希望有更好的方法來做到這一點。 – 2012-07-05 15:20:28
在使用[Classic Synth](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/synth.html)的情況下,使用xml文件和[SSCCE]編輯您的問題(http:// sscce /) – mKorbel 2012-07-05 15:32:56