- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在我的项目中实现这种类型的文本字段
我编写了一个画家类来为我的文本字段设置背景。
这是我的主类,我在其中将 Nimbus 设置为外观:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
class NimbusBaseDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField jtxt, txtDisEnabled;
int i;
private UIManager.LookAndFeelInfo[] lafs;
public NimbusBaseDemo() {
try {
// Set nimbus look and feel. nimbusBase works only for it.
new NimbusBaseUI();
} catch (Exception e) {
e.printStackTrace();
}
setTitle("Nimbus Base Demo");
setSize(400, 400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jtxt = new JTextField();
jtxt = new JTextField("Enabled Text Field");
jtxt.setEnabled(true);
add(jtxt);
// Disabled text field
txtDisEnabled = new JTextField("Disabled Text Field");
txtDisEnabled.setEnabled(false);
add(txtDisEnabled);
}
public static void main(String args[]) {
new NimbusBaseDemo();
}
}
这是我的 nimbus 主题类,在这里我根据需要扩展 NimbusLookAndFell 类和 setdefaults。
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
public class NimbusBaseUI extends NimbusLookAndFeel {
public NimbusBaseUI() {
super(); // Initialisation and installating
try {
new TextFieldTheme(this);
UIManager.setLookAndFeel(this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void initialize() {
// TODO Auto-generated method stub
super.initialize();
}
}
这是我在 NimbusBaseUI 类中使用的文本字段主题类
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.Painter;
public class TextFieldTheme {
public TextFieldTheme(NimbusBaseUI nimbusUI) {
nimbusUI.getDefaults().put("TextField.opaque", false);
nimbusUI.getDefaults().put("TextField.font",
new Font("Myriad Pro Light", Font.PLAIN, 13));
nimbusUI.getDefaults().put("TextField[Enabled].textForeground",
new Color(0, 0, 0));
nimbusUI.getDefaults().put("TextField.contentMargins",
new Insets(2, 10, 2, 2));
nimbusUI.getDefaults().put("TextField[Enabled].contentMargins",
new Insets(2, 10, 2, 2));
nimbusUI.getDefaults().put("TextField[Disabled].contentMargins",
new Insets(2, 10, 2, 2));
nimbusUI.getDefaults().put(
"TextField[Enabled].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TextField[Focused].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TextField[Selected].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TextField[MouseOver].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TextField[Enabled+Focused].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TextField[Enabled+Selected].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TextField[Enabled+MouseOver].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put("TextField[Disabled].textForeground",
new Color(0, 0, 0));
nimbusUI.getDefaults().put(
"TextField[Disabled].backgroundPainter",
new TextFeildPaintBorder(new Color(255, 255, 255), new Color(
255, 255, 255)));
nimbusUI.getDefaults().put(
"TextField[Disabled+Focused].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TextField[Disabled+Selected].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TextField[Disabled+MouseOver].backgroundPainter",
new TextFeildPaintBorder(new Color(0, 0, 255), new Color(255,
255, 255)));
}
public class TextFeildPaintBorder implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
protected int strokeSize = 1;
/** Color of shadow */
/** Color of shadow */
protected Color shadowColor = new Color(128, 128, 128, 140);
/** Sets if it drops shadow */
protected boolean shady = true;
/** Sets if it has an High Quality view */
protected boolean highQuality = false;
/** Double values for Horizontal and Vertical radius of corner arcs */
protected Dimension arcs = new Dimension(10, 10);
/** Distance between shadow border and opaque panel border */
protected int shadowGap = 1;
/** The offset of shadow. */
protected int shadowOffset = 1; // width of the shadow
/** The transparency value of shadow. ( 0 - 255) */
protected int shadowAlpha = 130;
public TextFeildPaintBorder(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, Object object, int w, int h) {
Color shadowColorA = new Color(shadowColor.getRed(),
shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
if (highQuality) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (shady) {
g.setColor(shadowColorA);
g.fillRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
arcs.height);
} else {
shadowGap = 1;
}
gradPaint = new GradientPaint((w / 2.0f), 0, new Color(255, 255,
255), (w / 2.0f), (h / 2.0f), new Color(255, 255, 255),
false);
g.setPaint(gradPaint);
g.fillRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setColor(new Color(188, 188, 187, 130));
g.setStroke(new BasicStroke(strokeSize));
g.drawRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setStroke(new BasicStroke());
}
}
}
编译并运行后我得到了这个:
最佳答案
您需要使用 jtxt.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2))
而不是 d.put("TextField.contentMargins", new Insets( 2, 10, 2, 2))
:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NimbusBaseDemo2 {
public JComponent makeUI() {
//UIDefaults d = UIManager.getLookAndFeelDefaults();
UIDefaults d = new UIDefaults();
//d.put("TextField.opaque", false);
//d.put("TextField.font", new Font("Myriad Pro Light", Font.PLAIN, 13));
//d.put("TextField[Enabled].textForeground", new Color(0, 0, 0));
//d.put("TextField[Disabled].textForeground", new Color(0, 0, 0));
//Insets ins = new Insets(2, 10, 2, 2);
//d.put("TextField.contentMargins", ins);
//d.put("TextField[Enabled].contentMargins", ins);
//d.put("TextField[Disabled].contentMargins", ins);
Painter<JComponent> painter = new TextFeildPaintBorder(
new Color(220, 220, 220), new Color(255, 255, 255));
d.put("TextField[Enabled].backgroundPainter", painter);
d.put("TextField[Focused].backgroundPainter", painter);
d.put("TextField[Selected].backgroundPainter", painter);
Painter<JComponent> painter2 = new TextFeildPaintBorder(
new Color(200, 200, 200), new Color(220, 220, 220));
d.put("TextField[Disabled].backgroundPainter", painter2);
d.put("TextField[Disabled+Focused].backgroundPainter", painter2);
d.put("TextField[Disabled+Selected].backgroundPainter", painter2);
//TEST:
//Painter<JComponent> painter3 = new Painter<JComponent>() {
// @Override public void paint(Graphics2D g, JComponent c, int w, int h) {
// }
//};
//d.put("TextField[Disabled].borderPainter", painter3);
//d.put("TextField[Enabled].backgroundPainter", painter3);
//d.put("TextField[Focused].backgroundPainter", painter3);
JTextField jtxt = new JTextField("Enabled Text Field");
jtxt.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));
jtxt.putClientProperty("Nimbus.Overrides", d);
JTextField txtDisEnabled = new JTextField("Disabled Text Field");
txtDisEnabled.setEnabled(false);
JTextField txtDisEnabled2 = new JTextField("Disabled Text Field");
txtDisEnabled2.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));
txtDisEnabled2.putClientProperty("Nimbus.Overrides", d);
txtDisEnabled2.setEnabled(false);
Box box = Box.createVerticalBox();
box.add(new JSeparator());
box.add(Box.createVerticalStrut(5));
box.add(new JTextField("Enabled Text Field"));
box.add(Box.createVerticalStrut(5));
box.add(jtxt);
box.add(Box.createVerticalStrut(5));
box.add(new JSeparator());
box.add(Box.createVerticalStrut(5));
box.add(txtDisEnabled);
box.add(Box.createVerticalStrut(5));
box.add(txtDisEnabled2);
box.add(Box.createVerticalStrut(5));
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
p.add(box, BorderLayout.NORTH);
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new NimbusBaseDemo2().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class TextFeildPaintBorder implements Painter<JComponent> {
private Color light, dark;
private GradientPaint gradPaint;
protected int strokeSize = 1;
/** Color of shadow */
protected Color shadowColor = new Color(128, 128, 128, 140);
/** Sets if it drops shadow */
protected boolean shady = true;
/** Sets if it has an High Quality view */
protected boolean highQuality = false;
/** Double values for Horizontal and Vertical radius of corner arcs */
protected Dimension arcs = new Dimension(10, 10);
/** Distance between shadow border and opaque panel border */
protected int shadowGap = 1;
/** The offset of shadow. */
protected int shadowOffset = 1; // width of the shadow
/** The transparency value of shadow. ( 0 - 255) */
protected int shadowAlpha = 130;
public TextFeildPaintBorder(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, JComponent c, int w, int h) {
Color shadowColorA = new Color(
shadowColor.getRed(),
shadowColor.getGreen(),
shadowColor.getBlue(),
shadowAlpha);
if (highQuality) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (shady) {
g.setColor(shadowColorA);
g.fillRoundRect(
0, 0, w - shadowGap, h - shadowGap, arcs.width, arcs.height);
} else {
shadowGap = 1;
}
gradPaint = new GradientPaint(0, 0, light, 0, h * .5f, dark, false);
g.setPaint(gradPaint);
g.fillRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setColor(new Color(188, 188, 187, 130));
g.setStroke(new BasicStroke(strokeSize));
g.drawRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setStroke(new BasicStroke());
}
}
添加:
I need to apply same text field theme all text fields in my project.
另一种选择是使用 TextField[Focused].borderPainter
和 TextField.contentMargins
:
d.put("TextField.contentMargins", insets);
d.put("TextField[Focused].borderPainter", emptyPainter);
NimbusBaseDemo3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NimbusBaseDemo3 {
public JComponent makeUI() {
JTextField jtxt = new JTextField("Enabled Text Field");
JTextField txtDisEnabled = new JTextField("Disabled Text Field");
txtDisEnabled.setEnabled(false);
JTextField txtDisEnabled2 = new JTextField("Disabled Text Field");
txtDisEnabled2.setEnabled(false);
Box box = Box.createVerticalBox();
box.add(new JSeparator());
box.add(Box.createVerticalStrut(5));
box.add(new JTextField("Enabled Text Field"));
box.add(Box.createVerticalStrut(5));
box.add(jtxt);
box.add(Box.createVerticalStrut(5));
box.add(new JSeparator());
box.add(Box.createVerticalStrut(5));
box.add(txtDisEnabled);
box.add(Box.createVerticalStrut(5));
box.add(txtDisEnabled2);
box.add(Box.createVerticalStrut(5));
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
p.add(box, BorderLayout.NORTH);
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
UIDefaults d = UIManager.getLookAndFeelDefaults();
Insets ins = new Insets(2, 10, 2, 2);
d.put("TextField.contentMargins", ins);
d.put("TextField[Enabled].contentMargins", ins);
d.put("TextField[Disabled].contentMargins", ins);
Painter<JComponent> painter = new TextFeildPaintBorder(
new Color(220, 220, 220), new Color(255, 255, 255));
d.put("TextField[Enabled].backgroundPainter", painter);
d.put("TextField[Focused].backgroundPainter", painter);
d.put("TextField[Selected].backgroundPainter", painter);
Painter<JComponent> painter2 = new TextFeildPaintBorder(
new Color(200, 200, 200), new Color(220, 220, 220));
d.put("TextField[Disabled].backgroundPainter", painter2);
Painter<JComponent> painter3 = new Painter<JComponent>() {
@Override public void paint(Graphics2D g, JComponent c, int w, int h) {}
};
d.put("TextField[Disabled].borderPainter", painter3);
d.put("TextField[Enabled].borderPainter", painter3);
d.put("TextField[Focused].borderPainter", painter3);
}
}
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new NimbusBaseDemo3().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class TextFeildPaintBorder implements Painter<JComponent> {
private Color light, dark;
private GradientPaint gradPaint;
protected int strokeSize = 1;
/** Color of shadow */
protected Color shadowColor = new Color(128, 128, 128, 140);
/** Sets if it drops shadow */
protected boolean shady = true;
/** Sets if it has an High Quality view */
protected boolean highQuality = false;
/** Double values for Horizontal and Vertical radius of corner arcs */
protected Dimension arcs = new Dimension(10, 10);
/** Distance between shadow border and opaque panel border */
protected int shadowGap = 1;
/** The offset of shadow. */
protected int shadowOffset = 1; // width of the shadow
/** The transparency value of shadow. ( 0 - 255) */
protected int shadowAlpha = 130;
public TextFeildPaintBorder(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, JComponent c, int w, int h) {
Color shadowColorA = new Color(
shadowColor.getRed(),
shadowColor.getGreen(),
shadowColor.getBlue(),
shadowAlpha);
if (highQuality) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (shady) {
g.setColor(shadowColorA);
g.fillRoundRect(
0, 0, w - shadowGap, h - shadowGap, arcs.width, arcs.height);
} else {
shadowGap = 1;
}
gradPaint = new GradientPaint(0, 0, light, 0, h * .5f, dark, false);
g.setPaint(gradPaint);
g.fillRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setColor(new Color(188, 188, 187, 130));
g.setStroke(new BasicStroke(strokeSize));
g.drawRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setStroke(new BasicStroke());
}
}
关于java - Java swing中如何使用GradientPaint实现阴影效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35551707/
如何在 XNA 中用图元(线条)制作的矩形周围制作阴影效果?我目前正在制作我的矩形,方法是将图元放入我制作的批处理中,然后添加纹理作为它们的背景。这些矩形应该象征着“ window ”。 我希望它们也
我刚刚在引擎中安装了照明系统。在以下屏幕截图中,您可以看到一个亮起的指示灯(黄色方框): 考虑到在照亮场景的光线之上,它还实现了FOV,该FOV将遮挡视场之外的任何物体。这就是为什么阴影的左侧部分看起
我是不熟悉Gradle并尝试编译我的项目的新手,但是也“阴影化”(就像在maven中一样)本地jar文件。 我正在尝试使用gradle shadow插件,但是当我运行“shadowJar”时,它并没有
用 JavaScript 编写解析器,对于任何语言,显然都使用 Map 来存储名称到变量的映射。 大多数语言允许以某种方式或内部作用域中的另一个变量遮蔽外部作用域中的变量。实现这一点的理想数据结构是功
// Shadowing #include using namespace std; const int MNAME = 30; const int M = 13; class Pers
我想设置我的导航栏和状态栏,就像下面链接中图片中的示例一样。我怎么能这样做 see the example 最佳答案 您可以通过像这样设置样式属性来简单地做到这一点: se
我以为我理解阴影的概念。但是这段代码让我想知道: public class Counter { int count = 0; public void inc() { count++; } pu
尝试遵循概述的方法 here为我的 UINavigationController 添加阴影。但是,该方法似乎不起作用。 这是我使用的代码: - (void)viewDidLoad { [sup
如何给 UITableViewCell 上下两边添加阴影? 我试过这个: cell.layer.shadowOpacity = 0.75f; cell.layer.shadowRadius = 5.0
我有一个博客,我为它制作了自定义光标,但它们并不是我想要的样子。使用一些免费的光标编辑程序制作光标,它们看起来只有一种颜色,没有边框、黑色或阴影。即使以图片形式上传后,在线.png 文件看起来也没有阴
我能得到像这张附图那样的带阴影的饼图吗? 如果是,怎么办? 这是饼图的代码: 最佳答案 遗憾的是,Kendo UI 饼图不支持任何 3D 元素。您可以在 Telerik 论坛中阅读相关信息 he
我一直试图获得给定的 curl 阴影效果 here对于 box-shadow:inset 但没有得到任何东西。任何onw有任何线索我可以让它工作吗? 一直试图让它在这个 上工作jsfiddle 将其更
我正在构建一个类似商店的东西,所以我有产品,侧面有圆形按钮,当我点击它们时,它会改变产品的颜色。一切都很完美,但我希望当我单击按钮时,按钮保持高亮状态。代码: CSS: #but1 { posi
我想创建一个底部边框下方有框显示的 H2 这是我的“基本”代码: My H2 #toto{ box-shadow: 0 4px 2px -2px gray; } 但我想
我有一个包含卡片列表的模板: --> {{event.eventTitle}} {{event.eve
CSS 阴影有问题。我不知道如何摆脱这里的顶部阴影:http://i.imgur.com/5FX62Fx.png 我得到的: box-shadow: 0 -3px 4px -6px #777, 0 3
在我的应用程序中,我想保留状态栏但使其背景与主屏幕相同。 所以我创建了一个自定义主题来设置应用程序背景: @drawable/background_shelf true
滚动 ListView 时如何去除阴影。 我在列表的顶部和底部出现了阴影。 最佳答案 关于android ListView 阴影,我们在Stack Overflow上找到一个类似的问题: https
我想在我的 Swift 4 中使用 MaterialComponents 为我的 View 添加阴影,但我不明白如何使用阴影电梯。我创建了一个名为 ShadowedView 的类,类似于 docume
给定以下 CAShapeLayer 是否可以在提示处添加如下图所示的阴影? 我正在使用 UIBezierPath 来绘制条形图。 - (CAShapeLayer *)gaugeCircleLayer
我是一名优秀的程序员,十分优秀!