001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 /***
018 * @author Alexander T. Simbirtsev
019 */
020 package javax.swing.border;
021
022 import java.awt.Color;
023 import java.awt.Component;
024 import java.awt.Graphics;
025 import java.awt.Insets;
026
027 import org.apache.harmony.x.swing.internal.nls.Messages;
028
029
030 public class BevelBorder extends AbstractBorder {
031
032 public static final int RAISED = 0;
033 public static final int LOWERED = 1;
034
035 private static final int INSETS_SIZE = 2;
036
037 protected int bevelType;
038
039 protected Color highlightOuter;
040 protected Color highlightInner;
041 protected Color shadowInner;
042 protected Color shadowOuter;
043
044 public BevelBorder(final int bevelType) {
045 this.bevelType = bevelType;
046 }
047
048 public BevelBorder(final int bevelType, final Color highlightOuterColor, final Color highlightInnerColor, final Color shadowOuterColor, final Color shadowInnerColor) {
049 this(bevelType);
050
051 if (highlightOuterColor == null || highlightInnerColor == null ||
052 shadowOuterColor == null || shadowInnerColor == null) {
053 throw new NullPointerException(Messages.getString("swing.68")); //$NON-NLS-1$
054 }
055 highlightOuter = highlightOuterColor;
056 highlightInner = highlightInnerColor;
057 shadowOuter = shadowOuterColor;
058 shadowInner = shadowInnerColor;
059 }
060
061 public BevelBorder(final int bevelType, final Color highlight, final Color shadow) {
062 this(bevelType, highlight, highlight, shadow, shadow);
063 }
064
065 int getInsetSize() {
066 return INSETS_SIZE;
067 }
068
069 public Insets getBorderInsets(final Component c, final Insets insets) {
070 int insetSize = getInsetSize();
071
072 insets.left = insetSize;
073 insets.top = insetSize;
074 insets.bottom = insetSize;
075 insets.right = insetSize;
076
077 return insets;
078 }
079
080 public Insets getBorderInsets(final Component c) {
081 int insetSize = getInsetSize();
082 return new Insets(insetSize, insetSize, insetSize, insetSize);
083 }
084
085 protected void paintRaisedBevel(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
086 Color oldColor = g.getColor();
087 Color color = getShadowOuterColor(c);
088 g.setColor(color);
089 g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
090 g.drawLine(x + width - 1, y, x + width - 1, y + height - 1);
091
092 color = getShadowInnerColor(c);
093 g.setColor(color);
094 g.drawLine(x + 1, y + height - 2, x + width - 2, y + height - 2);
095 g.drawLine(x + width - 2, y + 1, x + width - 2, y + height - 2);
096
097 color = getHighlightOuterColor(c);
098 g.setColor(color);
099 g.drawLine(x + 1, y, x + width - 2, y);
100 g.drawLine(x, y + 1, x, y + height - 2);
101
102 color = getHighlightInnerColor(c);
103 g.setColor(color);
104 g.drawLine(x + 2, y + 1, x + width - 3, y + 1);
105 g.drawLine(x + 1, y + 2, x + 1, y + height - 3);
106 g.setColor(oldColor);
107 }
108
109 protected void paintLoweredBevel(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
110 Color oldColor = g.getColor();
111 Color color = getShadowInnerColor(c);
112 g.setColor(color);
113 g.drawLine(x, y, x + width - 1, y);
114 g.drawLine(x, y, x, y + height - 1);
115
116 color = getShadowOuterColor(c);
117 g.setColor(color);
118 g.drawLine(x + 1, y + 1, x + width - 2, y + 1);
119 g.drawLine(x + 1, y + 1, x + 1, y + height - 2);
120
121 color = getHighlightOuterColor(c);
122 g.setColor(color);
123 g.drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);
124 g.drawLine(x + width - 1, y + 1, x + width - 1, y + height - 2);
125
126 color = getHighlightInnerColor(c);
127 g.setColor(color);
128 g.drawLine(x + 2, y + height - 2, x + width - 2, y + height - 2);
129 g.drawLine(x + width - 2, y + 2, x + width - 2, y + height - 3);
130 g.setColor(oldColor);
131 }
132
133 public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
134 if (bevelType == BevelBorder.LOWERED) {
135 paintLoweredBevel(c, g, x, y, width, height);
136 } else if (bevelType == BevelBorder.RAISED){
137 paintRaisedBevel(c, g, x, y, width, height);
138 }
139 }
140
141 public Color getShadowOuterColor(final Component c) {
142 return (shadowOuter != null) ? shadowOuter : c.getBackground().darker().darker();
143 }
144
145 public Color getShadowInnerColor(final Component c) {
146 return (shadowInner != null) ? shadowInner : c.getBackground().darker();
147 }
148
149 public Color getHighlightOuterColor(final Component c) {
150 return (highlightOuter != null) ? highlightOuter : c.getBackground().brighter().brighter();
151 }
152
153 public Color getHighlightInnerColor(final Component c) {
154 return (highlightInner != null) ? highlightInner : c.getBackground().brighter();
155 }
156
157 public Color getShadowOuterColor() {
158 return shadowOuter;
159 }
160
161 public Color getShadowInnerColor() {
162 return shadowInner;
163 }
164
165 public Color getHighlightOuterColor() {
166 return highlightOuter;
167 }
168
169 public Color getHighlightInnerColor() {
170 return highlightInner;
171 }
172
173 public boolean isBorderOpaque() {
174 return true;
175 }
176
177 public int getBevelType() {
178 return bevelType;
179 }
180
181 }
分享到:
相关推荐
import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class BorderDemo { public static void main(String[] args) { JFrame frame = new JFrame("边框示例"); frame....
8. 文件中还提到了java.awt包和javax.swing包,这两个包分别属于Java基础和Java扩展图形用户界面工具集,包含了许多构建和操作图形用户界面的类和接口。 9. 文档中出现了import语句,显示了导入相关类的操作,这是...
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.event.*; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io....
根据提供的文件信息,本文将详细解析“J2SE中表格应用”的相关知识点,重点在于如何在Java Swing中使用`JTable`组件,并结合代码示例进行深入分析。 ### J2SE与Swing简介 Java 2 Standard Edition (J2SE) 是Java...
import java.awt.EventQueue; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table....
Java中的`Border`类是Swing库的一部分,用于在组件(如JFrame、JPanel等)上添加边框。边框不仅可以提供视觉上的装饰,还可以帮助用户更好地理解UI的结构。`Border`接口由Java AWT和Swing库提供,它是所有边框类型的...
button.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); ``` ### 5. 图标按钮 按钮不仅可以显示文本,还可以显示图标。可以同时设置文本和图标,或者仅设置图标: ```java // 设置图标 ...
Java-GUI编程之Swing组件 Java-GUI编程之Swing组件是Java编程语言中用于构建图形用户界面的一个核心组件。Swing是Java Foundation Classes(JFC)的一个组成部分,提供了丰富的图形用户界面组件,帮助开发者快速...
- 使用`BevelBorder`为`JPanel`添加边框效果,增加视觉层次感。 10. **Swing组件的布局管理**: - 在Swing中,可以通过设置不同的布局管理器(如FlowLayout、GridLayout、BoxLayout、GridBagLayout等)来调整组件...
设置了边框类型为`BevelBorder.RAISED`,使得工具栏有明显的边缘效果。 8. **标签设置**:`JLabel`的`setBounds`和`setIcon`方法用于调整标签的位置和设置初始的无图状态。当窗口尺寸变化时,标签的文本会根据窗口...
2. **边界效果**:Java AWT和Swing提供了Border接口及其各种实现,如EmptyBorder、BevelBorder等。自定义的边界效果可能通过实现或组合这些边界类来创建。开发者可能使用Graphics2D对象来绘制复杂的边框,包括但不...
Java基于要SWing技术自定义标签的边界效果,通过javax.swing.border.Border 接口,进行自定义边界类OwnBorder。可以实现空边界(EmptyBorder)、斜切边界(BevelBorder)、蚀刻边界(EtchedBorder)、线条边界...
- `BevelBorder`:提供斜角边框效果。 - `EtchedBorder`:创建雕刻效果的边框。 - `LineBorder`:创建线条边框。 - ` MatteBorder`:使用指定的图像或颜色作为边框材质。 - `TitledBorder`:在边框上添加文本...
本例将实现如空边界(EmptyBorder)、斜切边界(BevelBorder)、蚀刻边界(EtchedBorder)、线条边界(LineBorder)、带标题的边界(TitledBorder)和复合边界(CompoundBorder)等边界效果。 运行环境:Java/...
3. **自定义边框和颜色**:使用`Border`接口可以改变按钮的边框样式,比如`LineBorder`、`BevelBorder`或`EmptyBorder`。同时,可以修改按钮的背景色和前景色以突出显示或匹配主题。 ```java button.setBorder...
11. **GUI组件的属性设置**:代码中涉及到许多设置组件边框、内边距、尺寸的方法,如`Border`, `Insets`, `TitledBorder`, `BevelBorder`, `EmptyBorder`等,这些都是为了美化和定制GUI的外观。 12. **线程处理**:...
6. `BevelBorder`: 创建斜面边框,有升起和凹下的效果。 7. `CompoundBorder`: 结合两个边框,形成复合边框。 8. `EmptyBorder`: 只设置边距,没有实际的边框效果。 使用`Border`时,我们可以直接创建这些类的实例...
这个接口被`AbstractBorder`类实现,`AbstractBorder`是所有预定义Swing边框类(如`BevelBorder`, `CompoundBorder`, `EmptyBorder`, `EtchedBorder`, `LineBorder`, `MatteBorder`, `SoftBevelBorder` 和 `...