简体   繁体   English

将JFrame设为只读

[英]Make JFrame read-only

Is there any way to convert a JFrame to read-only, similarly to Collections.unmodifiableList() ? 有什么方法可以将JFrame转换为只读,类似于Collections.unmodifiableList()

I need to pass it as parameter to many classes, but I don't want them to close the window or do anything but consulting. 我需要将其作为参数传递给许多类,但是我不希望他们关闭窗口或进行其他任何操作。 Also I would need some custom methods to be available (because I'm extending JFrame). 另外,我还需要一些自定义方法(因为我正在扩展JFrame)。

(I don't mean disable all components, I mean you can't write on the object if you have a reference to it). (我并不是说要禁用所有组件,我是说如果有对它的引用就不能在该对象上写东西)。

No, there is no way to do that. 不,没有办法做到这一点。 The proper solution is not to extend JFrame at all. 正确的解决方案是根本不扩展JFrame。 In general, you should never extend component classes to do things you can already do just by calling their public methods; 通常,永远不要扩展组件类以仅通过调用它们的公共方法来完成已经可以做的事情。 routine usage of a class like JFrame does not constitute a new type of JFrame, any more than putting different people in a Volkswagen qualifies it as a new type of car. 像JFrame这样的类的常规用法并不构成新型的JFrame,除了将不同的人放在大众汽车手中之外,还可以将其视为新型的汽车。

Instead, you should wrap the JFrame in a class which extends nothing , and hide the JFrame in it. 相反,您应该将JFrame包装在一个不扩展的类中,并将JFrame隐藏在其中。 Then you can expose only the methods you want to expose: 然后,您可以仅公开要公开的方法:

public class MyAppWindow {
    private final JFrame frame;

    private final JComponent frameContents;

    public MyAppWindow() {
        this.frame = new JFrame("My App");
        this.frameContents = new JPanel();
        // Set up frameContents here...
    }

    public void show() {
        frame.setVisible(true);
    }

    public void hide() {
        frame.setVisible(false);
    }

    public void addField(Component field,
                         String labelText) {

        JLabel label = new JLabel(labelText);
        label.setLabelFor(field);

        // Create layout constraints for label here...

        frameContents.add(label, constraints);

        // Create layout constraints for field here...

        frameContents.add(field, constraints);
    }
}

Technically, a class with no extends clause will inherit from Object. 从技术上讲,没有extends子句的类将从Object继承。

Possible solutions: 可能的解决方案:

  1. Make the components it holds "read-only", one way by making them all individually non-focusable. 使组件所拥有的组件为“只读”,这是一种使它们各自都无法聚焦的方式。
  2. I suppose you could recursively iterate through the container/component tree making all non-focusable 我想您可以递归地遍历容器/组件树,使所有对象都无法聚焦
  3. Cover the JFrame with a glasspane that blocks user input. 用一块玻璃板遮盖JFrame,以防止用户输入。

As a side recommendation: 作为附带建议:

Also I would need some custom methods to be available (because I'm extending JFrame). 另外,我还需要一些自定义方法(因为我正在扩展JFrame)。

You may be painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. 您可能需要使类扩展JFrame,迫使您创建和显示JFrame(通常需要更大的灵活性),从而使自己陷入困境。 In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. 实际上,我会冒险地说,我创建的大多数Swing GUI代码都不会扩展JFrame,实际上,很少有人愿意这样做。 More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. 更常见的是,您的GUI类将面向创建JPanels,然后将其放置到JFrames或JDialogs或JTabbedPanes中,或者在需要时通过CardLayouts进行交换。 This will greatly increase the flexibility of your GUI coding. 这将大大增加GUI编码的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM