java - 通过重绘方法在 GUI 中添加组件

标签 java swing user-interface paint

我并不像我想的那样理解 Java GUI。在我的框架绘制方法中,我想删除所有当前按钮,并添加新按钮。 (总体目标是有一个界面,用户可以看到角色并点击按钮下载与角色相关的文档。由于每个角色都不同,当用户从我的列表中选择一个新用户时,一组新的文档他们将可以使用按钮。)

这是我刚刚编写的一个测试框架,它显示了事情的发展方向。它具有我在实际程序中使用的类似范例,没有太多困惑:

public class GUITest extends JFrame
{

/**
 * @param args
 */
public static void main(String[] args)
{
    Container gui_test = new GUITest();

}

private JComponent content = null;

public GUITest()
{
    super();

    setVisible(true);
}

public void paint(Graphics g)
{
    this.removeAll();

    content = new JPanel();

    JComponent test_button = new JButton("New Button 1");
    JComponent button = new JButton("New Button 2");

    content.add(button);
    content.add(test_button);

    this.add(content);

    super.paint(g);
}

}

如果不调用 removeAll(),按钮将继续放在 JPanel 的顶部,但调用后,不会显示任何内容。我不知道这是为什么,因为我正在适本地添加组件,对吗?

编辑
明白了,让我给你更详细的分解。一位客户通过查看西面板游戏中的角色列表来浏览我的程序。他们可以从列表中选择一行,该行将在东面板显示字符详细信息。细节是图像和描述。最近,我为那个特定的字符添加了相关文档,它将显示在东面板的底部。我创建了按键监听器,因此客户可以通过按数字键快速查看文档,但我也想让他们能够单击按钮启动 pdf View 并查看文档的内容。

由于每个字符都有不同的相关文档和不同数量的文档,我每次都重新绘制按钮,以反射(reflect)相关文档的数量和文档的适当标题。这是重绘表现奇怪的地方。你给了我一个很好的解释,说明出了什么问题,但我现在不知道如何让客户访问文档,除了绘制文档的描述以及启动它所需的热键之外。这有意义吗?

最佳答案

切勿在 paint 或 paintComponent 方法中向 GUI 添加组件或移除组件。只是不要这样做。曾经。时期。

这些方法仅用于绘图,需要尽可能快,否则您的程序会出现无响应。不仅如此,您无法完全控制何时甚至是否调用这些方法,因此程序逻辑和结构不应包含在这些方法中。

而是使用事件监听器(例如 ActionListeners、ListSelectionListeners)或键绑定(bind)来响应用户事件。

编辑
关于

Got it, let me give you a more detailed breakdown. A client is navigating my program by looking at a list of characters in a game on a west panel. They can select a row from the list which will show char details on the east panel. The details are an image and description. Recently, I added relevant documents for that particular char, which will show on the bottom of the east panel. I created key listener's, so the client can quickly view the document by pressing a num key, but I also want to give them the ability to click on the button to launch a pdf view and see the contents of the document.

我将使用 JList 来保存左侧的可选信息列表,并使用 ListSelectionListener 对其作出 react 。在监听器中,我会更改相关的显示信息。我也避免将 KeyListeners 与 Swing 一起使用,而是倾向于使用 Key Bindings,因为它们在焦点方面更灵活且不那么死板。

关于

Since every char has different related docs and different number of docs, I repainted the buttons every time, to reflect the amount of related docs and the appropriate titles for the docs. This is where the repaint is acting strange. You gave me a good explanation of what's going wrong, but I don't know how to give the client access to the docs now, aside from painting a description of the doc along with the hot key needed to launch it. Does that make sense?

我不确定你在这里做什么或你想做什么。

关于java - 通过重绘方法在 GUI 中添加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11479930/

相关文章:

java - 不使用 doGet/doPost 获取 cookie

java - Selenium - 使用 xpath 或 cssSelector 查找元素

java - 在Java中的JFrame上显示鼠标坐标

python - 将参数传递给 QT Designer 构建的槽函数

java - Android布局阴影

swift - 如何实现这种之字形 Collection View 布局?

java - 修改执行 Jar 文件

java - 使用 REST assured 进行 JSON 验证

java - 尝试使用java更改背景颜色时出错

java - 用颜色填充圆圈