如何访问 JFrame 内的多个 JPanel?

发布于 2024-07-08 19:30:55 字数 530 浏览 5 评论 0原文

我有一个 JFrame ,其中包含一个带有 JTextField 的“显示”JPanel 和一个带有按钮的“控件”JPanel应该访问显示 JPanel 的内容。 我认为我的问题与如何使用观察者模式有关,原则上我理解这一点。 您需要放置侦听器并更新消息,但我不知道将它们放在哪里,如何从一个面板访问另一个面板,也许如果有必要引入“数据模型”类。 例如,我想从控制面板访问 JTextField 的内容,并使用匿名操作侦听器,如下所示:

JButton openfile = new JButton("Convert file");
openfile.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        openButtonPressed();
    }           
});

I have a JFrame that contains a "display" JPanel with JTextField and a "control" JPanel with buttons that should access the contents of the display JPanel. I think my problem is related on how to use the observer pattern, which in principle I understand. You need to place listeners and update messages, but I don't have a clue where to put these, how to get access from one panel to the other and maybe if necessary to introduce a "datamodel" class. For example, I want to access the contents of the JTextField from the control panel and I use an anonymous action listener as follows:

JButton openfile = new JButton("Convert file");
openfile.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        openButtonPressed();
    }           
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

中性美 2024-07-15 19:30:55

您需要减少这些对象之间的耦合。

您可以有一个主对象,它拥有所有文本字段和按钮(面板不相关),

然后在该主对象内有一个单独的动作侦听器(我称之为调解器,请参阅调解器模式)

该操作侦听器在调解器上执行一个方法,该方法在然后从文本字段中获取值并创建一个传输对象。

通过这种方式,您可以减少面板、文本字段等之间的耦合,并将控件放在一个位置(中介器),也就是说,您不会让它们彼此了解。

你可以看一下这个问题中的代码:
https://stackoverflow.com/questions/324554/#324559

它在运行代码中显示了这些概念。

顺便说一句,观察者模式已经在 J​​TextField、JButton、ActionListener 等中实现。您只需要添加挂钩即可。

我希望这有帮助。

编辑将两个答案合并为一个。

这是代码。

class App { // this is the mediator

    // GUI components.
    private JFrame frame;
    private JTextField name;
    private JTextField count;
    private JTextField date;
    // Result is displayed here.
    private JTextArea textArea;

    // Fired by this button.
    private JButton   go;

    private ActionListener actionListener;


    public App(){
        actionListener = new ActionListener(){
            public void actionPerformed( ActionEvent e ){
                okButtonPressed();
            }
        };
    }

    private void okButtonPressed(){
         // template is an object irrelevant to this code.
         template.setData( getData() );
         textArea.setText( template.getTransformedData() );
    }


    public void initialize(){

        frame     = new JFrame("Code challenge v0.1");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        name     = new JTextField();
        count    = new JTextField();
        date     = new JTextField();
        textArea = new JTextArea();
        go       = new JButton("Go");
        go.addActionListener( actionListener ); // prepare the button.

        layoutComponents(); // a lot of panels are created here. Irrelevant.
    }
}

可以在此处检索完整且正在运行的代码:

尽可能支持组合而不是继承,这一点很重要。

You need to reduce the coupling between these objects.

You can have a master object, that owns all the text fields and the button ( the panels are irrelevant )

Then a separete actionlistener within that master object ( I call it mediator see mediator pattern )

That action listener performs a method on the mediator which in turn take the values from the textfields and create perhaps a transfer object.

This way you reduce the coupling between the panels, textfields etc. and let the control in one place ( the mediator ) that is, you don't let them know each other.

You can take a look at the code in this question:
https://stackoverflow.com/questions/324554/#324559

It shows these concepts in running code.

BTW the observer pattern is already implemented in the JTextField, JButton, ActionListener etc. You just need to add the hooks.

I hope this helps.

EDIT Joined two answers into one.

This is the code.

class App { // this is the mediator

    // GUI components.
    private JFrame frame;
    private JTextField name;
    private JTextField count;
    private JTextField date;
    // Result is displayed here.
    private JTextArea textArea;

    // Fired by this button.
    private JButton   go;

    private ActionListener actionListener;


    public App(){
        actionListener = new ActionListener(){
            public void actionPerformed( ActionEvent e ){
                okButtonPressed();
            }
        };
    }

    private void okButtonPressed(){
         // template is an object irrelevant to this code.
         template.setData( getData() );
         textArea.setText( template.getTransformedData() );
    }


    public void initialize(){

        frame     = new JFrame("Code challenge v0.1");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        name     = new JTextField();
        count    = new JTextField();
        date     = new JTextField();
        textArea = new JTextArea();
        go       = new JButton("Go");
        go.addActionListener( actionListener ); // prepare the button.

        layoutComponents(); // a lot of panels are created here. Irrelevant.
    }
}

Complete and running code can be retrieved here:

It is important to favor composition over inheritance when possible.

胡大本事 2024-07-15 19:30:55

如果您在一层中创建模型并在上面添加一两层来创建组件和布局,它确实会使代码更清晰。 当然不要扩展诸如 JFrameJPanel 之类的东西。

不觉得需要使模型层中的组合层次结构与显示完全匹配。 然后只需从 Document 中获取文本并执行相关操作即可。

好吧,也许没那么简单。 Swing 模型有点混乱。 特别是 ButtonModel 脑部受损,控制器区域的代码可能不完全纯净。

It does make the code cleaner if you create the models in one layer and add a layer or two above to create the components and layout. Certainly do not extend the likes of JFrame and JPanel.

Do not feel the need to make the composition hierarchy in the model layer exactly match the display. Then it's just a matter of taking the text from the Document and performing the relevant operation.

Okay, perhpas not that simple. Swing models are a little bit messy. In particular ButtonModel is brain damaged, and the controller area of code might not be entirely pure.

冧九 2024-07-15 19:30:55

我们有所谓的构建器,它将从子面板中构建父面板。 在此构建器中,您将可以访问需要监听的所有子组件,因此可以在那里实现任何逻辑。

最后,构建器将返回具有完整逻辑的父面板。

一旦你有了父面板,再去访问子组件并让它们做任何事情就真的是一团糟。

We have so called builders, which will build the parent panel out of the children. In this builder you will have access to all the subcomponents you need to listen to and can thus can implement any logic there.

Finally the builder will then return the parent panel with the complete logic.

Once you've got the parent panel it's really a mess getting to the child components and have them do anything.

弄潮 2024-07-15 19:30:55

谢谢。 我添加了一个数据模型层,它以某种方式处理面板之间的通信。

我还发现 JTextField 上的 Listeners 上的此链接很有用:

链接文本

thanks. I added a datamodel layer which handles somehow the communication between the panels.

I also found this link on Listeners on JTextField usefull:

link text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文