下面列出了javax.swing.JPanel#setUI ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void formatControlRow(JTabbedPane tabs, JPanel tabsContainer) {
Border border;
Paint paint = createBorderGradient(tabs.getTabPlacement());
if (tabs.getTabPlacement() == SwingConstants.BOTTOM) {
border = new PartialLineBorder(paint, true, true,
tabs.getTabCount() == 0, true);
} else if (tabs.getTabPlacement() == SwingConstants.LEFT) {
border = new PartialLineBorder(paint, true,
tabs.getTabCount() == 0, true, true);
} else if (tabs.getTabPlacement() == SwingConstants.RIGHT) {
border = new PartialLineBorder(paint, true, true, true,
tabs.getTabCount() == 0);
} else {
border = new PartialLineBorder(paint, tabs.getTabCount() == 0,
true, true, true);
}
tabsContainer.setUI(new GradientPanelUI(createContentGradient(tabs,
false)));
tabsContainer.setBorder(border);
}
public CustomizedToolbarDemo() {
for (int a = 0; a < list.length; a++) {
list[a].setName("" + a); // give every component a unique name
list[a].setOpaque(false);
}
ActionListener showCustomizeAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
toolbar.displayDialog(JVM.isMac ? 350 : 280);
}
};
customize.addActionListener(showCustomizeAction);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 0;
c.fill = GridBagConstraints.HORIZONTAL;
add(toolbar, c);
c.gridy++;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
JPanel fluff = new JPanel();
fluff.setBorder(new PartialLineBorder(Color.lightGray, new Insets(1, 0,
0, 0)));
fluff.setUI(new TexturePaintPanelUI(PlafPaintUtils.getCheckerBoard(3)));
fluff.setOpaque(false);
add(fluff, c);
}
/**
* Create the application icon.
*
* This should be called on the EDT, because it creates/renders JComponents.
*/
private static BufferedImage createAppImage() {
BufferedImage bi = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
JPanel p = new JPanel();
p.setOpaque(false);
QPanelUI qui = new QPanelUI();
qui.setCalloutSize(10);
qui.setCornerSize(10);
qui.setCalloutType(CalloutType.BOTTOM_CENTER);
qui.setFillColor1(Color.white);
qui.setFillColor2(new Color(0xececec));
qui.setShadowSize(5);
qui.setStrokeColor(new Color(0x787878));
p.setUI(qui);
JSwitchButton switchButton1 = new JSwitchButton(true);
JSwitchButton switchButton2 = new JSwitchButton(false);
p.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(4, 4, 4, 4);
p.add(switchButton1, c);
c.gridy++;
p.add(switchButton2, c);
Dimension d = p.getPreferredSize();
d.width = Math.max(d.width, d.height);
d.height = Math.max(d.width, d.height);
p.setSize(d);
p.getLayout().layoutContainer(p);
p.paint(g);
g.dispose();
return bi;
}
private JComponent createDebugPanel() {
BufferedImage img = (BufferedImage) getClientProperty("debug.ghost.image");
if (img == null)
return this;
final CardLayout cardLayout = new CardLayout();
final JPanel panel = new JPanel(cardLayout);
JPanel imagePanel = new JPanel();
imagePanel.setUI(new PanelImageUI(img));
JPanel paneWrapper = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
paneWrapper.add(this, c);
panel.add(paneWrapper, "real");
panel.add(imagePanel, "debug");
Timer timer = new Timer(2000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
String mode = (System.currentTimeMillis() % 4000) > 2000 ? "real"
: "debug";
cardLayout.show(panel, mode);
}
});
timer.start();
return panel;
}
public JPanel createGrid(JComponent... components) {
JPanel panel = new JPanel();
panel.setUI(new TopBaselinePanelUI());
panel.putClientProperty(Inspector.PROPERTY_WRAPPED, Boolean.TRUE);
addContainer(panel);
panel.setOpaque(false);
panel.setLayout(this);
for (JComponent c : components) {
c.setOpaque(false);
panel.add(c);
}
return panel;
}
/**
* {@inheritDoc}
*/
@Override
public Component getListCellRendererComponent(JList<? extends BuildableType> list,
BuildableType value,
int index,
boolean isSelected,
boolean cellHasFocus) {
final ImageLibrary lib = getImageLibrary();
JPanel panel = new MigPanel(new MigLayout());
panel.setOpaque(false);
if (isSelected) {
panel.setUI((PanelUI)FreeColSelectedPanelUI.createUI(panel));
}
JLabel imageLabel = new JLabel(new ImageIcon(ImageLibrary
.getBuildableTypeImage(value, buildingDimension)));
JLabel nameLabel = new JLabel(Messages.getName(value));
String reason = lockReasons.get(value);
panel.add(imageLabel, "span 1 2");
if (reason == null) {
panel.add(nameLabel, "wrap");
} else {
panel.add(nameLabel, "split 2");
panel.add(lib.getLockLabel(), "wrap");
panel.setToolTipText(reason);
}
List<AbstractGoods> required = value.getRequiredGoodsList();
int size = required.size();
for (int i = 0; i < size; i++) {
AbstractGoods goods = required.get(i);
ImageIcon icon = new ImageIcon(lib.getSmallGoodsTypeImage(goods.getType()));
JLabel goodsLabel = new JLabel(Integer.toString(goods.getAmount()), icon, SwingConstants.CENTER);
if (i == 0 && size > 1) {
panel.add(goodsLabel, "split " + size);
} else {
panel.add(goodsLabel);
}
}
return panel;
}
/**
* Format animation controls and add them to a container. This includes
* assigning the borders of all the components, assigning the PanelUI of the
* container, assigning the preferred size of the container, and making a
* spacebar on the slider trigger the toggle play button.
*
* @param container
* this container is emptied, assigned a GridBagLayout, and the
* other arguments are added to this container.
* @param togglePlayButton
* this button will be the left-most button. It is expected to
* always be visible.
* @param buttons
* an optional collection of buttons to display after the
* togglePlayButton.
* @param slider
* the slider that stretches to fill the remaining width.
*/
public static void format(JPanel container, final JButton togglePlayButton,
JButton[] buttons, JSlider slider) {
if (buttons == null)
buttons = new JButton[] {};
container.setUI(new GradientPanelUI(new Color(0xebebeb), Color.white));
container.removeAll();
container.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 0;
c.weighty = 1;
c.fill = GridBagConstraints.VERTICAL;
container.add(togglePlayButton, c);
for (int a = 0; a < buttons.length; a++) {
c.gridx++;
container.add(buttons[a], c);
buttons[a].setOpaque(false);
buttons[a].setContentAreaFilled(false);
buttons[a].setRolloverIcon(new DarkenedIcon(buttons[a], .5f));
buttons[a].setPressedIcon(new DarkenedIcon(buttons[a], .75f));
buttons[a].setBorder(new PartialLineBorder(Color.gray, new Insets(
1, 0, 1, 1)));
}
c.weightx = 1;
c.gridx++;
c.fill = GridBagConstraints.BOTH;
container.add(slider, c);
togglePlayButton.setOpaque(false);
togglePlayButton.setContentAreaFilled(false);
togglePlayButton.setBorder(new LineBorder(Color.gray));
slider.setOpaque(false);
slider.setBorder(new PartialLineBorder(Color.gray, new Insets(1, 0, 1,
1)));
Dimension d = slider.getPreferredSize();
d.width = 60;
d.height = 25;
slider.setPreferredSize((Dimension) d.clone());
d.width = d.height;
togglePlayButton.setPreferredSize(d);
for (int a = 0; a < buttons.length; a++) {
buttons[a].setPreferredSize(d);
}
InputMap inputMap = slider.getInputMap(JComponent.WHEN_FOCUSED);
inputMap.put(KeyStroke.getKeyStroke(' '), "togglePlay");
slider.getActionMap().put("togglePlay", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
togglePlayButton.doClick();
}
});
togglePlayButton.setFocusPainted(false);
setInsetFocusBorder(togglePlayButton);
for (JButton button : buttons) {
button.setFocusPainted(false);
setInsetFocusBorder(button);
}
setInsetFocusBorder(slider);
}
public static JPanel createSplashPanel() {
final ImageIcon picture = IconLoader.getInstance().getAboutDialogPicture();
// Create the image panel
final JPanel imagePanel = new JPanel( new BorderLayout() );
imagePanel.setUI( new BackgroundUI( picture ) );
imagePanel.setBorder( BorderFactory.createLineBorder( Color.DARK_GRAY ) );
// Overlay the version
final JLabel versionLabel = new JLabel();
final String buildString = ReportDesignerInfo.getInstance().getVersion();
if ( buildString == null ) {
versionLabel.setText( Messages.getString( "SplashScreen.DevelopmentVersion" ) );
} else {
versionLabel.setText( buildString );
}
versionLabel.setText( Messages.getString( "SplashScreen.Version", versionLabel.getText() ) );
versionLabel.setFont( new Font( Font.SANS_SERIF, Font.BOLD, 14 ) );
versionLabel.setOpaque( false );
versionLabel.setBackground( TRANSPARENT );
versionLabel.setForeground( DARK_GREY );
versionLabel.setBorder( BORDER );
versionLabel.setBounds( XLOC, YLOC, TEXT_WIDTH, versionLabel.getPreferredSize().height );
// Overlay the license
final String year = new SimpleDateFormat( "yyyy" ).format( new Date() );
final JTextArea copyrightArea = new JTextArea( Messages.getString( "SplashScreen.Copyright", year ) );
copyrightArea.setEditable( false );
copyrightArea.setBounds( XLOC, YLOC + 20, TEXT_WIDTH, LICENSE_HEIGHT );
copyrightArea.setOpaque( false );
copyrightArea.setLineWrap( true );
copyrightArea.setWrapStyleWord( true );
copyrightArea.setFont( COPYRIGHT_FONT );
copyrightArea.setEnabled( false );
copyrightArea.setBackground( TRANSPARENT );
copyrightArea.setForeground( DARK_GREY );
copyrightArea.setBorder( BORDER );
copyrightArea.setDisabledTextColor( copyrightArea.getForeground() );
// Overlay the copyright
final JTextArea licenseArea = new JTextArea( Messages.getString( "SplashScreen.License" ) );
licenseArea.setEditable( false );
licenseArea.setBounds( XLOC, YLOC + 14 + LICENSE_HEIGHT, TEXT_WIDTH, COPYRIGHT_HEIGHT );
licenseArea.setOpaque( false );
licenseArea.setLineWrap( true );
licenseArea.setWrapStyleWord( true );
licenseArea.setFont( LICENSE_FONT );
licenseArea.setEnabled( false );
licenseArea.setBackground( TRANSPARENT );
licenseArea.setBorder( BORDER );
licenseArea.setDisabledTextColor( copyrightArea.getForeground() );
// Add all the overlays
final JPanel imagePanelOverlay = new JPanel( null );
imagePanelOverlay.setOpaque( false );
imagePanelOverlay.add( versionLabel );
imagePanelOverlay.add( copyrightArea );
imagePanelOverlay.add( licenseArea );
imagePanelOverlay.setBackground( TRANSPARENT );
imagePanel.add( imagePanelOverlay );
imagePanel.setPreferredSize( new Dimension( picture.getIconWidth(), picture.getIconHeight() ) );
return imagePanel;
}