我正在尝试为 JButton/JTabbedPane 的特定实例覆盖“nimbusBase”颜色,但没有成功。 只有组件的特定属性,例如“Button.background”,正在工作。 有什么想法吗?
UIDefaults dialogTheme = new UIDefaults();
// dialogTheme.put(“nimbusBase”, Color.orange);
// dialogTheme.put("nimbusBlueGrey", Color.blue);
dialogTheme.put("Button.background", Color.yellow);
JButton dialogButton = new JButton("North");
dialogButton.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
dialogButton.putClientProperty("Nimbus.Overrides", dialogTheme);
最佳答案
您可以将所有“nimbusBase”组件的颜色添加到 LookAndFeelDefaults
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("nimbusBase", new ColorUIResource(210, 0, 210));
或者您编写的特定组件。可能会更新 componentTreeUI。
...
dialogTheme.put("Button.background", new ColorUIResource(0, 0, 210));
...
dialogButton.putClientProperty("Nimbus.Overrides", dialogTheme);
SwingUtilities.updateComponentTreeUI(dialogButton);
我从不为特定组件覆盖 "nimbusBase"。
我找到了一个不错的Nimbus Theme Creator,可以展示将一个Nimbus Default Color改成所有Components的效果:http://aephyr.googlecode.com/svn/trunk
编辑
您可以“复制”Painter,修改它们并通过 UIDefaults 设置它们:
带有 nimbusOrange JTabbedPane 和普通 GUI 的示例。

public class Example {
static public void main( String[] s ) throws Exception {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.getContentPane().setLayout( new BorderLayout() );
frame.setBounds( 50, 50, 600, 600 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JTabbedPane pane = new JTabbedPane();
pane.addTab( "Lorem", new JLabel("Lorem") );
pane.addTab( "Ipsum", new JLabel("Ipsum") );
pane.addTab( "Dolor", new JLabel("Dolor") );
pane.addTab( "Sit", new JLabel("Sit") );
pane.addTab( "Amet", new JLabel("Amet") );
UIDefaults dialogTheme = new UIDefaults();
dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED));
dialogTheme.put("TabbedPane:TabbedPaneTab[Disabled].backgroundPainter", new Painter(Painter.BACKGROUND_DISABLED));
dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED_MOUSEOVER));
dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled+Pressed].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED_PRESSED));
dialogTheme.put("TabbedPane:TabbedPaneTab[Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED));
dialogTheme.put("TabbedPane:TabbedPaneTab[Disabled+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_DISABLED));
dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_FOCUSED));
dialogTheme.put("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_MOUSEOVER));
dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+MouseOver+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_MOUSEOVER_FOCUSED));
dialogTheme.put("TabbedPane:TabbedPaneTab[Pressed+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_PRESSED));
dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+Pressed+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_PRESSED_FOCUSED));
dialogTheme.put("TabbedPane:TabbedPaneTabArea[Disabled].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_DISABLED));
dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled+MouseOver].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED_MOUSEOVER));
dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled+Pressed].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED_PRESSED));
dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED));
pane.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
pane.putClientProperty("Nimbus.Overrides", dialogTheme);
JTabbedPane secondPane = new JTabbedPane();
secondPane.addTab( "Lorem", new JLabel("Lorem") );
secondPane.addTab( "Ipsum", new JLabel("Ipsum") );
secondPane.addTab( "Dolor", new JLabel("Dolor") );
secondPane.addTab( "Sit", new JLabel("Sit") );
secondPane.addTab( "Amet", new JLabel("Amet") );
frame.getContentPane().setLayout( new BorderLayout() );
frame.getContentPane().add( pane, BorderLayout.NORTH );
frame.getContentPane().add( secondPane, BorderLayout.CENTER );
frame.setVisible( true );
}
});
}
}
带有 nimbusOrange 的 Painter 类
注意: 我在这个 Painter 示例中删除了绘画方法。因为这段代码太长了。只需通过 com.sun.java.swing.plaf.nimbus.TabbedPaneTabbedPaneTabPainter 的复制和粘贴添加它们。
/*
* TabbedPaneTabbedPaneTabPainter.java %E%
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import com.sun.java.swing.plaf.nimbus.AbstractRegionPainter;
/**
*/
public final class Painter extends AbstractRegionPainter {
//package private integers representing the available states that
//this painter will paint. These are used when creating a new instance
//of TabbedPaneTabbedPaneTabPainter to determine which region/state is being painted
//by that instance.
static final int BACKGROUND_ENABLED = 1;
static final int BACKGROUND_ENABLED_MOUSEOVER = 2;
static final int BACKGROUND_ENABLED_PRESSED = 3;
static final int BACKGROUND_DISABLED = 4;
static final int BACKGROUND_SELECTED_DISABLED = 5;
static final int BACKGROUND_SELECTED = 6;
static final int BACKGROUND_SELECTED_MOUSEOVER = 7;
static final int BACKGROUND_SELECTED_PRESSED = 8;
static final int BACKGROUND_SELECTED_FOCUSED = 9;
static final int BACKGROUND_SELECTED_MOUSEOVER_FOCUSED = 10;
static final int BACKGROUND_SELECTED_PRESSED_FOCUSED = 11;
private int state; //refers to one of the static final ints above
private PaintContext ctx;
//the following 4 variables are reused during the painting code of the layers
private Path2D path = new Path2D.Float();
private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
//All Colors used for painting are stored here. Ideally, only those colors being used
//by a particular instance of TabbedPaneTabbedPaneTabPainter would be created. For the moment at least,
//however, all are created for each instance.
private Color color1 = decodeColor("nimbusOrange", 0.032459438f, -0.55535716f, -0.109803945f, 0);
private Color color2 = decodeColor("nimbusOrange", 0.08801502f, 0.3642857f, -0.4784314f, 0);
private Color color3 = decodeColor("nimbusOrange", 0.08801502f, -0.63174605f, 0.43921566f, 0);
private Color color4 = decodeColor("nimbusOrange", 0.05468172f, -0.6145278f, 0.37647057f, 0);
private Color color5 = decodeColor("nimbusOrange", 0.032459438f, -0.5953556f, 0.32549018f, 0);
private Color color6 = decodeColor("nimbusOrange", 0.032459438f, -0.54616207f, -0.02352941f, 0);
private Color color7 = decodeColor("nimbusOrange", 0.08801502f, -0.6317773f, 0.4470588f, 0);
private Color color8 = decodeColor("nimbusOrange", 0.021348298f, -0.61547136f, 0.41960782f, 0);
private Color color9 = decodeColor("nimbusOrange", 0.032459438f, -0.5985242f, 0.39999998f, 0);
private Color color10 = decodeColor("nimbusOrange", 0.08801502f, 0.3642857f, -0.52156866f, 0);
private Color color11 = decodeColor("nimbusOrange", 0.027408898f, -0.5847884f, 0.2980392f, 0);
private Color color12 = decodeColor("nimbusOrange", 0.035931647f, -0.5553123f, 0.23137254f, 0);
private Color color13 = decodeColor("nimbusOrange", 0.029681683f, -0.5281874f, 0.18039215f, 0);
private Color color14 = decodeColor("nimbusOrange", 0.03801495f, -0.5456242f, 0.3215686f, 0);
private Color color15 = decodeColor("nimbusOrange", 0.032459438f, -0.59181184f, 0.25490195f, 0);
private Color color16 = decodeColor("nimbusOrange", 0.05468172f, -0.58308274f, 0.19607842f, 0);
private Color color17 = decodeColor("nimbusOrange", 0.046348333f, -0.6006266f, 0.34509802f, 0);
private Color color18 = decodeColor("nimbusOrange", 0.046348333f, -0.60015875f, 0.3333333f, 0);
private Color color19 = decodeColor("nimbusOrange", 0.004681647f, -0.6197143f, 0.43137252f, 0);
private Color color20 = decodeColor("nimbusOrange", 7.13408E-4f, -0.543609f, 0.34509802f, 0);
private Color color21 = decodeColor("nimbusOrange", -0.0020751357f, -0.45610264f, 0.2588235f, 0);
private Color color22 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.43866998f, 0.24705881f, 0);
private Color color23 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.44879842f, 0.29019606f, 0);
private Color color24 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.08776909f, -0.2627451f, 0);
private Color color25 = decodeColor("nimbusOrange", 0.06332368f, 0.3642857f, -0.4431373f, 0);
private Color color26 = decodeColor("nimbusOrange", 0.004681647f, -0.6198413f, 0.43921566f, 0);
private Color color27 = decodeColor("nimbusOrange", -0.0022627711f, -0.5335866f, 0.372549f, 0);
private Color color28 = decodeColor("nimbusOrange", -0.0017285943f, -0.4608264f, 0.32549018f, 0);
private Color color29 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.4555341f, 0.3215686f, 0);
private Color color30 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.46404046f, 0.36470586f, 0);
private Color color31 = decodeColor("nimbusOrange", -0.57865167f, -0.6357143f, -0.54901963f, 0);
private Color color32 = decodeColor("nimbusOrange", -4.2033195E-4f, -0.38050595f, 0.20392156f, 0);
private Color color33 = decodeColor("nimbusOrange", 0.0013483167f, -0.16401619f, 0.0745098f, 0);
private Color color34 = decodeColor("nimbusOrange", -0.0010001659f, -0.01599598f, 0.007843137f, 0);
private Color color35 = decodeColor("nimbusOrange", 0.0f, 0.0f, 0.0f, 0);
private Color color36 = decodeColor("nimbusOrange", 0.0018727183f, -0.038398862f, 0.035294116f, 0);
private Color color37 = decodeColor("nimbusRed", 0.0f, 0.0f, 0.0f, 0);
//Array of current component colors, updated in each paint call
private Object[] componentColors;
public Painter(int state) {
super();
this.state = state;
Insets insets = null;
switch( state ) {
case BACKGROUND_ENABLED: insets = new Insets(7, 7, 1, 7); break;
case BACKGROUND_ENABLED_MOUSEOVER: insets = new Insets(7, 7, 1, 7); break;
case BACKGROUND_ENABLED_PRESSED: insets = new Insets(7, 6, 1, 7); break;
case BACKGROUND_DISABLED: insets = new Insets(6, 7, 1, 7); break;
case BACKGROUND_SELECTED_DISABLED: insets = new Insets(7, 7, 0, 7); break;
case BACKGROUND_SELECTED: insets = new Insets(7, 7, 0, 7); break;
case BACKGROUND_SELECTED_MOUSEOVER: insets = new Insets(7, 9, 0, 9); break;
case BACKGROUND_SELECTED_PRESSED: insets = new Insets(7, 9, 0, 9); break;
case BACKGROUND_SELECTED_FOCUSED: insets = new Insets(7, 7, 3, 7); break;
case BACKGROUND_SELECTED_MOUSEOVER_FOCUSED:insets = new Insets(7, 9, 3, 9); break;
case BACKGROUND_SELECTED_PRESSED_FOCUSED: insets = new Insets(7, 9, 3, 9); break;
}
this.ctx = new AbstractRegionPainter.PaintContext(insets, new Dimension( 10 , 20 ), false );
}
@Override
protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) {
//populate componentColors array with colors calculated in getExtendedCacheKeys call
componentColors = extendedCacheKeys;
//generate this entire method. Each state/bg/fg/border combo that has
//been painted gets its own KEY and paint method.
switch(state) {
case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
case BACKGROUND_ENABLED_MOUSEOVER: paintBackgroundEnabledAndMouseOver(g); break;
case BACKGROUND_ENABLED_PRESSED: paintBackgroundEnabledAndPressed(g); break;
case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
case BACKGROUND_SELECTED_DISABLED: paintBackgroundSelectedAndDisabled(g); break;
case BACKGROUND_SELECTED: paintBackgroundSelected(g); break;
case BACKGROUND_SELECTED_MOUSEOVER: paintBackgroundSelectedAndMouseOver(g); break;
case BACKGROUND_SELECTED_PRESSED: paintBackgroundSelectedAndPressed(g); break;
case BACKGROUND_SELECTED_FOCUSED: paintBackgroundSelectedAndFocused(g); break;
case BACKGROUND_SELECTED_MOUSEOVER_FOCUSED: paintBackgroundSelectedAndMouseOverAndFocused(g); break;
case BACKGROUND_SELECTED_PRESSED_FOCUSED: paintBackgroundSelectedAndPressedAndFocused(g); break;
}
}
@Override
protected final PaintContext getPaintContext() {
return ctx;
}
// ... paintBackgroundEnabled( ...
// ...
}
带有 numbusOrange 的 AreaPainter
/*
* TabbedPaneTabbedPaneTabAreaPainter.java %E%
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.PaintContext;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import com.sun.java.swing.plaf.nimbus.AbstractRegionPainter;
/**
*/
public final class AreaPainter extends AbstractRegionPainter {
//package private integers representing the available states that
//this painter will paint. These are used when creating a new instance
//of TabbedPaneTabbedPaneTabAreaPainter to determine which region/state is being painted
//by that instance.
static final int BACKGROUND_ENABLED = 1;
static final int BACKGROUND_DISABLED = 2;
static final int BACKGROUND_ENABLED_MOUSEOVER = 3;
static final int BACKGROUND_ENABLED_PRESSED = 4;
private int state; //refers to one of the static final ints above
private PaintContext ctx;
//the following 4 variables are reused during the painting code of the layers
private Path2D path = new Path2D.Float();
private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
//All Colors used for painting are stored here. Ideally, only those colors being used
//by a particular instance of TabbedPaneTabbedPaneTabAreaPainter would be created. For the moment at least,
//however, all are created for each instance.
private Color color1 = new Color(255, 200, 0, 255);
private Color color2 = decodeColor("nimbusOrange", 0.08801502f, 0.3642857f, -0.4784314f, 0);
private Color color3 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.45471883f, 0.31764704f, 0);
private Color color4 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.4633005f, 0.3607843f, 0);
private Color color5 = decodeColor("nimbusOrange", 0.05468172f, -0.58308274f, 0.19607842f, 0);
private Color color6 = decodeColor("nimbusOrange", -0.57865167f, -0.6357143f, -0.54901963f, 0);
private Color color7 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.4690476f, 0.39215684f, 0);
private Color color8 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.47635174f, 0.4352941f, 0);
private Color color9 = decodeColor("nimbusOrange", 0.0f, -0.05401492f, 0.05098039f, 0);
private Color color10 = decodeColor("nimbusOrange", 0.0f, -0.09303135f, 0.09411764f, 0);
//Array of current component colors, updated in each paint call
private Object[] componentColors;
public AreaPainter(int state) {
super();
this.state = state;
this.ctx = new AbstractRegionPainter.PaintContext(new Insets(0, 5, 6, 5), new Dimension(5, 24), false );
}
@Override
protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) {
//populate componentColors array with colors calculated in getExtendedCacheKeys call
componentColors = extendedCacheKeys;
//generate this entire method. Each state/bg/fg/border combo that has
//been painted gets its own KEY and paint method.
switch(state) {
case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
case BACKGROUND_ENABLED_MOUSEOVER: paintBackgroundEnabledAndMouseOver(g); break;
case BACKGROUND_ENABLED_PRESSED: paintBackgroundEnabledAndPressed(g); break;
}
}
@Override
protected final PaintContext getPaintContext() {
return ctx;
}
private void paintBackgroundEnabled(Graphics2D g) {
rect = decodeRect1();
g.setPaint(color1);
g.fill(rect);
rect = decodeRect2();
g.setPaint(decodeGradient1(rect));
g.fill(rect);
}
private void paintBackgroundDisabled(Graphics2D g) {
rect = decodeRect2();
g.setPaint(decodeGradient2(rect));
g.fill(rect);
}
private void paintBackgroundEnabledAndMouseOver(Graphics2D g) {
rect = decodeRect2();
g.setPaint(decodeGradient3(rect));
g.fill(rect);
}
private void paintBackgroundEnabledAndPressed(Graphics2D g) {
rect = decodeRect2();
g.setPaint(decodeGradient4(rect));
g.fill(rect);
}
private Rectangle2D decodeRect1() {
rect.setRect(decodeX(0.0f), //x
decodeY(1.0f), //y
decodeX(0.0f) - decodeX(0.0f), //width
decodeY(1.0f) - decodeY(1.0f)); //height
return rect;
}
private Rectangle2D decodeRect2() {
rect.setRect(decodeX(0.0f), //x
decodeY(2.1666667f), //y
decodeX(3.0f) - decodeX(0.0f), //width
decodeY(3.0f) - decodeY(2.1666667f)); //height
return rect;
}
private Paint decodeGradient1(Shape s) {
Rectangle2D bounds = s.getBounds2D();
float x = (float)bounds.getX();
float y = (float)bounds.getY();
float w = (float)bounds.getWidth();
float h = (float)bounds.getHeight();
return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
new Color[] { color2,
decodeColor(color2,color3,0.5f),
color3,
decodeColor(color3,color4,0.5f),
color4,
decodeColor(color4,color2,0.5f),
color2});
}
private Paint decodeGradient2(Shape s) {
Rectangle2D bounds = s.getBounds2D();
float x = (float)bounds.getX();
float y = (float)bounds.getY();
float w = (float)bounds.getWidth();
float h = (float)bounds.getHeight();
return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
new Color[] { color5,
decodeColor(color5,color3,0.5f),
color3,
decodeColor(color3,color4,0.5f),
color4,
decodeColor(color4,color5,0.5f),
color5});
}
private Paint decodeGradient3(Shape s) {
Rectangle2D bounds = s.getBounds2D();
float x = (float)bounds.getX();
float y = (float)bounds.getY();
float w = (float)bounds.getWidth();
float h = (float)bounds.getHeight();
return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
new Color[] { color6,
decodeColor(color6,color7,0.5f),
color7,
decodeColor(color7,color8,0.5f),
color8,
decodeColor(color8,color2,0.5f),
color2});
}
private Paint decodeGradient4(Shape s) {
Rectangle2D bounds = s.getBounds2D();
float x = (float)bounds.getX();
float y = (float)bounds.getY();
float w = (float)bounds.getWidth();
float h = (float)bounds.getHeight();
return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
new Color[] { color2,
decodeColor(color2,color9,0.5f),
color9,
decodeColor(color9,color10,0.5f),
color10,
decodeColor(color10,color2,0.5f),
color2});
}
}
关于java - 覆盖每个组件实例的 Swing Nimbus L&F 原色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6922368/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击