社区
Java SE
帖子详情
求助!关于createTitledBorder里如何加一个控件
snowing3773
2007-09-28 04:28:21
请问
jPanel1.setBorder(BorderFactory.createTitledBorder("6666"));
createTitledBorder里如何加一个控件?
请赐教
...全文
177
1
打赏
收藏
求助!关于createTitledBorder里如何加一个控件
请问 jPanel1.setBorder(BorderFactory.createTitledBorder("6666")); createTitledBorder里如何加一个控件? 请赐教
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
vtudiv
2007-09-30
打赏
举报
回复
重载getBorderComponent,setBorderComponent
例:(转载)
public class CompTitledBorder extends TitledBorder {
protected JComponent component;
public CompTitledBorder(JComponent component) {
this(null, component, LEFT, TOP);
}
public CompTitledBorder(Border border) {
this(border, null, LEFT, TOP);
}
public CompTitledBorder(Border border, JComponent component) {
this(border, component, LEFT, TOP);
}
public CompTitledBorder(Border border,
JComponent component,
int titleJustification,
int titlePosition) {
super(border, null, titleJustification,
titlePosition, null, null);
this.component = component;
if (border == null) {
this.border = super.getBorder();
}
}
public void paintBorder(Component c, Graphics g,
int x, int y, int width, int height) {
Rectangle borderR = new Rectangle(x + EDGE_SPACING,
y + EDGE_SPACING,
width - (EDGE_SPACING * 2),
height - (EDGE_SPACING * 2));
Insets borderInsets;
if (border != null) {
borderInsets = border.getBorderInsets(c);
} else {
borderInsets = new Insets(0, 0, 0, 0);
}
Rectangle rect = new Rectangle(x,y,width,height);
Insets insets = getBorderInsets(c);
Rectangle compR = getComponentRect(rect, insets);
int diff;
switch (titlePosition) {
case ABOVE_TOP:
diff = compR.height + TEXT_SPACING;
borderR.y += diff;
borderR.height -= diff;
break;
case TOP:
case DEFAULT_POSITION:
diff = insets.top/2 - borderInsets.top - EDGE_SPACING;
borderR.y += diff;
borderR.height -= diff;
break;
case BELOW_TOP:
case ABOVE_BOTTOM:
break;
case BOTTOM:
diff = insets.bottom/2 - borderInsets.bottom - EDGE_SPACING;
borderR.height -= diff;
break;
case BELOW_BOTTOM:
diff = compR.height + TEXT_SPACING;
borderR.height -= diff;
break;
}
border.paintBorder(c, g, borderR.x, borderR.y,
borderR.width, borderR.height);
Color col = g.getColor();
g.setColor(c.getBackground());
g.fillRect(compR.x, compR.y, compR.width, compR.height);
g.setColor(col);
component.repaint();
}
public Insets getBorderInsets(Component c, Insets insets) {
Insets borderInsets;
if (border != null) {
borderInsets = border.getBorderInsets(c);
} else {
borderInsets = new Insets(0,0,0,0);
}
insets.top = EDGE_SPACING + TEXT_SPACING + borderInsets.top;
insets.right = EDGE_SPACING + TEXT_SPACING + borderInsets.right;
insets.bottom = EDGE_SPACING + TEXT_SPACING + borderInsets.bottom;
insets.left = EDGE_SPACING + TEXT_SPACING + borderInsets.left;
if (c == null || component == null) {
return insets;
}
int compHeight = 0;
if (component != null) {
compHeight = component.getPreferredSize().height;
}
switch (titlePosition) {
case ABOVE_TOP:
insets.top += compHeight + TEXT_SPACING;
break;
case TOP:
case DEFAULT_POSITION:
insets.top += Math.max(compHeight,borderInsets.top) - borderInsets.top;
break;
case BELOW_TOP:
insets.top += compHeight + TEXT_SPACING;
break;
case ABOVE_BOTTOM:
insets.bottom += compHeight + TEXT_SPACING;
break;
case BOTTOM:
insets.bottom += Math.max(compHeight,borderInsets.bottom) - borderInsets.bottom;
break;
case BELOW_BOTTOM:
insets.bottom += compHeight + TEXT_SPACING;
break;
}
return insets;
}
public JComponent getTitleComponent() {
return component;
}
public void setTitleComponent(JComponent component) {
this.component = component;
}
public Rectangle getComponentRect(Rectangle rect,Insets borderInsets) {
Dimension compD = component.getPreferredSize();
Rectangle compR = new Rectangle(0,0,compD.width,compD.height);
switch (titlePosition) {
case ABOVE_TOP:
compR.y = EDGE_SPACING;
break;
case TOP:
case DEFAULT_POSITION:
compR.y = EDGE_SPACING +
(borderInsets.top -EDGE_SPACING -TEXT_SPACING -compD.height)/2;
break;
case BELOW_TOP:
compR.y = borderInsets.top - compD.height - TEXT_SPACING;
break;
case ABOVE_BOTTOM:
compR.y = rect.height - borderInsets.bottom + TEXT_SPACING;
break;
case BOTTOM:
compR.y = rect.height - borderInsets.bottom + TEXT_SPACING +
(borderInsets.bottom -EDGE_SPACING -TEXT_SPACING -compD.height)/2;
break;
case BELOW_BOTTOM:
compR.y = rect.height - compD.height - EDGE_SPACING;
break;
}
switch (titleJustification) {
case LEFT:
case DEFAULT_JUSTIFICATION:
compR.x = TEXT_INSET_H + borderInsets.left;
break;
case RIGHT:
compR.x = rect.width - borderInsets.right -TEXT_INSET_H -compR.width;
break;
case CENTER:
compR.x = (rect.width - compR.width) / 2;
break;
}
return compR;
}
}
Swing编程边框(
Border
)的用法总结.doc
Swing 编程边框(
Border
)是 Java 中
一个
常用的 UI 组件,用于设置
控件
的边框样式。通过 set
Border
方法可以设置边框,该方法是 JComponent 类的成员方法。
Border
Factory 是
一个
工厂类,提供了标准的
Border
对象,...
JAVA组件大全.pdf
JCheckBox组件可以用来创建复选框、选项按钮、列表方框、下拉式列表等各种类型的选择
控件
。 JCheckBox类层次结构图 java.lang.Object -- java.awt.Component -- java.awt.Container -- javax.swing.JComponent -- ...
添
加
边框的方法
接着创建了
一个
`JPanel`容器用于放置
控件
。然后创建了
一个
带有文本“Button with Line
Border
”的`JButton`按钮,并为其设置了
一个
品红色、宽度为2像素的边框。最后将按钮添
加
到面板中,并将面板添
加
到窗口中,完成...
Java下拉式菜单实现
在Java图形用户界面开发中,下拉式菜单(即组合框或选择框)是非常常见的一种
控件
,它允许用户从
一个
列表中选择
一个
值。本文将详细介绍如何使用Java Swing库中的`JComboBox`类来创建和定制下拉式菜单,并通过具体的...
java程序设计PPT第五章
例如:
Border
border
=
Border
Factory.
create
Title
d
Border
(" 边框标题 ");button.set
Border
(
border
);用 Swing 创建图形界面 (7) 处理事件– Swing 使用事件监听器模型处理用户交互。例如,为按钮添
加
点击事件:...
Java SE
62,623
社区成员
307,257
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章