Java 如何在JPanel边框中使用JButton?
我需要为Java 如何在JPanel边框中使用JButton?,java,swing,border,jbutton,Java,Swing,Border,Jbutton,我需要为JPanel创建一个自定义边框,该边框的左上角包含一个JButton(如标题边框中的标题)。在Java中有什么方法可以做到这一点吗 可能符合要求。我在过去成功地使用了它,只做了一些小的调整,用复选框制作了一个标题边框 (摘自网站): import java.awt.*; 导入javax.swing.*; 导入javax.swing.border.*; /** *ComponentBorder类允许您在中放置真实的组件 *为绘制构件边界而保留的空间。 * *本课程利用了所有Swing组件都
JPanel
创建一个自定义边框,该边框的左上角包含一个JButton
(如标题边框中的标题)。在Java中有什么方法可以做到这一点吗 可能符合要求。我在过去成功地使用了它,只做了一些小的调整,用复选框制作了一个标题边框
(摘自网站):
import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.*;
/**
*ComponentBorder类允许您在中放置真实的组件
*为绘制构件边界而保留的空间。
*
*本课程利用了所有Swing组件都是
*还有集装箱。默认情况下,布局管理器为空,因此我们应该
*能够将子组件放置在父组件中的任意位置。整齐
*防止子零部件在父零部件上方绘制的步骤
*组件边框添加到父组件中,以便
*边框将为要绘制的子组件保留空间,而不使用任何颜色
*影响父组件。
*/
公共类ComponentBorder实现了Border
{
公共枚举边
{
顶部
左边
底部,
正确的;
}
公共静态最终浮点超前=0.0f;
公共静态最终浮动中心=0.5f;
公共静态最终浮动尾随=1.0f;
私有组件父级;
私有组件;
私人边缘;
私人浮动调整;
私人内部差距=5;
私有布尔adjustInsets=true;
私有插图边框插图=新插图(0,0,0,0);
/**
*使用默认边(edge.RIGHT)和
*对齐(中间)。
*
*@param component要添加到边界区域的组件
*/
公共组件边界(JComponent组件)
{
此(组件,边缘。右侧);
}
/**
*使用默认路线(中间)的便利构造函数。
*
*@param component要添加到边界区域的组件
*@param edge顶部、左侧、底部和右侧的有效边缘枚举
*/
公共组件边界(JComponent组件,边缘)
{
这(组件、边缘、中心);
}
/**
*用于创建ComponentBorder的主构造函数。
*
*@param component要添加到边界区域的组件
*@param edge顶部、左侧、底部和右侧的有效边缘枚举
*@param alignment组件沿
*指定的边。必须在0-1.0范围内。
*/
公共组件边框(JComponent组件、边缘、浮动对齐)
{
这个组件=组件;
setSize(component.getPreferredSize());
setCursor(Cursor.getDefaultCursor());
设置边缘(边缘);
设置对齐(对齐);
}
公共布尔值isAdjustInsets()
{
返回调整插图;
}
公共void集合adjustInsets(布尔adjustInsets)
{
此.adjustInsets=adjustInsets;
}
/**
*沿边界边缘获取零部件对齐
*
*@返回路线
*/
公共浮动getAlignment()
{
返回对齐;
}
/**
*沿边界边缘设置零部件对齐方式
*
*@param校准范围为0-1.0的值。标准值为
*居中(默认),左侧和右侧。
*/
公共无效集合对齐(浮动对齐)
{
此。校准=校准>1.0f?1.0f:校准<0.0f?0.0f:校准;
}
/**
*获取零部件沿其定位的边
*
*@返回边缘
*/
公共边缘getEdge()
{
返回边缘;
}
/**
*设置零部件沿其定位的边
*
*@param-edge组件所在的边。
*/
公共无效设置边缘(边缘)
{
这个边=边;
}
/**
*获取边框组件和父组件之间的间隙
*
*@返回以像素为单位的间距。
*/
公共int getGap()
{
返回间隙;
}
/**
*设置边框构件和父构件之间的间隙
*
*@param gap以像素为单位的间距(默认值为5)
*/
公共空间设置间距(内部间距)
{
这个.gap=gap;
}
//
//实现边界接口
//
公共插图getBorderInsets(组件c)
{
返回边框插图;
}
公共布尔值isborder不透明()
{
返回false;
}
/**
*在这种情况下,需要绘制真实的组件。设置位置
*组件的损坏将导致在该位置涂漆。
*/
公共空白画框(组件c、图形g、整数x、整数y、整数宽度、整数高度)
{
float x2=(width-component.getWidth())*component.getAlignmentX()+x;
float y2=(height-component.getHeight())*component.getAlignmentY()+y;
组件设置位置((int)x2,(int)y2);
}
/*
*在指定组件上安装此边框,方法是更换
*包含原始边框的复合边框的现有边框
*以及我们的组件边界
*
*此方法的所有属性只应调用一次
*类。多次安装边框将导致
*不可预测的结果。
*/
public void安装(JComponent父级)
{
this.parent=parent;
determineseIntsandAlignment();
//将此边框添加到父级
Border current=parent.getBorder();
如果(当前==null)
{
母公司订单(本);
}
其他的
{
CompoundBorder复合=新的CompoundBorder(当前,此);
母公司订单(复合);
}
//将组件添加到pa
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* The ComponentBorder class allows you to place a real component in
* the space reserved for painting the Border of a component.
*
* This class takes advantage of the knowledge that all Swing components are
* also Containers. By default the layout manager is null, so we should be
* able to place a child component anywhere in the parent component. In order
* to prevent the child component from painting over top of the parent
* component a Border is added to the parent componet such that the insets of
* the Border will reserve space for the child component to be painted without
* affecting the parent component.
*/
public class ComponentBorder implements Border
{
public enum Edge
{
TOP,
LEFT,
BOTTOM,
RIGHT;
}
public static final float LEADING = 0.0f;
public static final float CENTER = 0.5f;
public static final float TRAILING = 1.0f;
private JComponent parent;
private JComponent component;
private Edge edge;
private float alignment;
private int gap = 5;
private boolean adjustInsets = true;
private Insets borderInsets = new Insets(0, 0, 0, 0);
/**
* Convenience constructor that uses the default edge (Edge.RIGHT) and
* alignment (CENTER).
*
* @param component the component to be added in the Border area
*/
public ComponentBorder(JComponent component)
{
this(component, Edge.RIGHT);
}
/**
* Convenience constructor that uses the default alignment (CENTER).
*
* @param component the component to be added in the Border area
* @param edge a valid Edge enum of TOP, LEFT, BOTTOM, RIGHT
*/
public ComponentBorder(JComponent component, Edge edge)
{
this(component, edge, CENTER);
}
/**
* Main constructor to create a ComponentBorder.
*
* @param component the component to be added in the Border area
* @param edge a valid Edge enum of TOP, LEFT, BOTTOM, RIGHT
* @param alignment the alignment of the component along the
* specified Edge. Must be in the range 0 - 1.0.
*/
public ComponentBorder(JComponent component, Edge edge, float alignment )
{
this.component = component;
component.setSize( component.getPreferredSize() );
component.setCursor(Cursor.getDefaultCursor());
setEdge( edge );
setAlignment( alignment );
}
public boolean isAdjustInsets()
{
return adjustInsets;
}
public void setAdjustInsets(boolean adjustInsets)
{
this.adjustInsets = adjustInsets;
}
/**
* Get the component alignment along the Border Edge
*
* @return the alignment
*/
public float getAlignment()
{
return alignment;
}
/**
* Set the component alignment along the Border Edge
*
* @param alignment a value in the range 0 - 1.0. Standard values would be
* CENTER (default), LEFT and RIGHT.
*/
public void setAlignment(float alignment)
{
this.alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? 0.0f : alignment;
}
/**
* Get the Edge the component is positioned along
*
* @return the Edge
*/
public Edge getEdge()
{
return edge;
}
/**
* Set the Edge the component is positioned along
*
* @param edge the Edge the component is position on.
*/
public void setEdge(Edge edge)
{
this.edge = edge;
}
/**
* Get the gap between the border component and the parent component
*
* @return the gap in pixels.
*/
public int getGap()
{
return gap;
}
/**
* Set the gap between the border component and the parent component
*
* @param gap the gap in pixels (default is 5)
*/
public void setGap(int gap)
{
this.gap = gap;
}
//
// Implement the Border interface
//
public Insets getBorderInsets(Component c)
{
return borderInsets;
}
public boolean isBorderOpaque()
{
return false;
}
/**
* In this case a real component is to be painted. Setting the location
* of the component will cause it to be painted at that location.
*/
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
{
float x2 = (width - component.getWidth()) * component.getAlignmentX() + x;
float y2 = (height - component.getHeight()) * component.getAlignmentY() + y;
component.setLocation((int)x2, (int)y2);
}
/*
* Install this Border on the specified component by replacing the
* existing Border with a CompoundBorder containing the original Border
* and our ComponentBorder
*
* This method should only be invoked once all the properties of this
* class have been set. Installing the Border more than once will cause
* unpredictable results.
*/
public void install(JComponent parent)
{
this.parent = parent;
determineInsetsAndAlignment();
// Add this Border to the parent
Border current = parent.getBorder();
if (current == null)
{
parent.setBorder(this);
}
else
{
CompoundBorder compound = new CompoundBorder(current, this);
parent.setBorder(compound);
}
// Add component to the parent
parent.add(component);
}
/**
* The insets need to be determined so they are included in the preferred
* size of the component the Border is attached to.
*
* The alignment of the component is determined here so it doesn't need
* to be recalculated every time the Border is painted.
*/
private void determineInsetsAndAlignment()
{
borderInsets = new Insets(0, 0, 0, 0);
// The insets will only be updated for the edge the component will be
// diplayed on.
//
// The X, Y alignment of the component is controlled by both the edge
// and alignment parameters
if (edge == Edge.TOP)
{
borderInsets.top = component.getPreferredSize().height + gap;
component.setAlignmentX(alignment);
component.setAlignmentY(0.0f);
}
else if (edge == Edge.BOTTOM)
{
borderInsets.bottom = component.getPreferredSize().height + gap;
component.setAlignmentX(alignment);
component.setAlignmentY(1.0f);
}
else if (edge == Edge.LEFT)
{
borderInsets.left = component.getPreferredSize().width + gap;
component.setAlignmentX(0.0f);
component.setAlignmentY(alignment);
}
else if (edge == Edge.RIGHT)
{
borderInsets.right = component.getPreferredSize().width + gap;
component.setAlignmentX(1.0f);
component.setAlignmentY(alignment);
}
if (adjustInsets)
adjustBorderInsets();
}
/*
* The complimentary edges of the Border may need to be adjusted to allow
* the component to fit completely in the bounds of the parent component.
*/
private void adjustBorderInsets()
{
Insets parentInsets = parent.getInsets();
// May need to adust the height of the parent component to fit
// the component in the Border
if (edge == Edge.RIGHT || edge == Edge.LEFT)
{
int parentHeight = parent.getPreferredSize().height - parentInsets.top - parentInsets.bottom;
int diff = component.getHeight() - parentHeight;
if (diff > 0)
{
int topDiff = (int)(diff * alignment);
int bottomDiff = diff - topDiff;
borderInsets.top += topDiff;
borderInsets.bottom += bottomDiff;
}
}
// May need to adust the width of the parent component to fit
// the component in the Border
if (edge == Edge.TOP || edge == Edge.BOTTOM)
{
int parentWidth = parent.getPreferredSize().width - parentInsets.left - parentInsets.right;
int diff = component.getWidth() - parentWidth;
if (diff > 0)
{
int leftDiff = (int)(diff * alignment);
int rightDiff = diff - leftDiff;
borderInsets.left += leftDiff;
borderInsets.right += rightDiff;
}
}
}
}