java titledborder_Java Swing:向TitledBorder添加按钮(Java Swing: adding button to TitledBorder)...

Java Swing:向TitledBorder添加按钮(Java Swing: adding button to TitledBorder)

我有一个TitledBorder面板列表,其中包含表示点的文本字段。

我刚被要求在TitledBorder的面板上添加一个按钮,这将使我删除面板及其内容。

那么如何在TitledBorder面板的右上角添加一个按钮?

编辑这里是我的

7abed133123416a3b5fdd31def13a666.png

这是我想要添加的内容(对不起,没有专业的photoshop质量:P)

480b62cc5c73ff11ebd0333a97d00d23.png

I have a list of TitledBorder panels that contain textfields that represents point.

I've just been asked to add a button to the panel with the TitledBorder, that will alow me delete the panel and its contents.

so how can I add a button in the top right corner of a panel with TitledBorder?

edit here is what I have

7abed133123416a3b5fdd31def13a666.png

and here is what I would like to add (sorry this no professional photoshop quality :P)

480b62cc5c73ff11ebd0333a97d00d23.png

原文:https://stackoverflow.com/questions/6433381

更新时间:2019-12-17 09:02

最满意答案

你可以在边框上绘制任何你想要的东西。 也只需将MouseListener添加到componnet并检查MouseEvent的Point。 如果在图像区域中发生了单击,请在那里执行代码。

You can draw whatever you want on the border. Also just add MouseListener to you componnet and check Point of the MouseEvent. If the click happens in the image area do your code there.

2011-06-22

相关问答

不幸的是,这个边缘宽度在TitledBorder类中是硬编码的。 所以你不能删除这个间距。 public class TitledBorder extends AbstractBorder

{

//...

// Space between the border and the component's edge

static protected final int EDGE_SPACING = 2;

}

但是你可以尝试扩展这个类(覆盖方法“void paintBorder

...

所以,你基本上有两个基本组,标题和按钮,这两个需要单独管理,因为它们有不同的布局要求(主要是按钮布局在中间)。 import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.

...

saveButton.setVisible(true); your_window.add(saveButton); 就这样。 saveButton.setVisible(true); your_window.add(saveButton); Thats all.

从示例http://www.java2s.com/Code/Java/Swing-JFC/CustomBorderSample.htm开始 你可以在边框上绘制任何你想要的东西。 也只需将MouseListener添加到componnet并检查MouseEvent的Point。 如果在图像区域中发生了单击,请在那里执行代码。 Start from the example http://www.java2s.com/Code/Java/Swing-JFC/CustomBorderSample.htm

...

按钮未显示的原因是您不打电话: super.paint(g);

在paint()方法中。 此调用将导致按钮和其他添加的组件被绘制。 在Swing中更好的自定义绘制方法是将自定义绘制放在扩展JComponent组件中,并覆盖paintComponent以利用Swing优化的绘制模型。 还可以考虑使用布局管理器来调整和放置组件。 避免使用绝对定位( null布局)。 从没有布局管理器做 : 虽然可以不使用布局管理器,但是如果可能的话,应该使用布局管理器。 布局管理器可以更轻松地调整依赖于外观的组件外

...

您已经在GPanel的实例上调用了setBorder() ,然后在后者的paintComponent()实现中修改了图形上下文的转换。 边界不知道这一点,所以显示的结果完全是预期的。 相反,请遵循setBorder()的父JComponent实现建议:“将组件放入JPanel并在JPanel设置边框。” 这里给出一个相关的例子。 在下面的变化中, 封闭的bPanel现在具有Border ,封闭的gPanel可以安全地操作图形上下文。 在translate()之前,红点以原点为中心; transla

...

我只换了一行(27): final JPanel panel2 = new JPanel();

panel2.add(new JLabel(" Testing Label 2"));

panel2.setBorder(new TitledBorder("P2"));

c = new GridBagConstraints();

// You have to use BOTH for the panel to take

// all vertical

...

我发现问题是什么。 问题在于我实例化MainFrame两次,首先通过在main中调用Application构造函数,然后在调用Application.getInstance()时删除添加到MainFrame的所有组件。 I found out what the problem was. The problem was that I instantiated MainFrame twice, first by calling Application constructor in main and t

...

Android使用java作为编码(本机)语言,没有区别,但他们已经删除了android api中的一些软件包,因为它们可能在移动开发中不需要,而且它们包含了一些自己的软件包。 Android应用程序在平台及其范围方面与Swing应用程序不同。 Android应用程序在.xml文件中设计得更多,它有不同的机制(比如在xml中为R.java指定生成id等)。 但是您访问资源的基本java是相同的 Android is using java as a coding(native) language ,

...

获得NullPointerException的最可能原因是您没有在按钮的ActionListener中初始化列表 btnadd.addActionListener(new ActionListener() {

public List user_array;

@Override

public void actionPerformed(ActionEvent e) {...}

在这里,而不是 public List user_array;

使

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值