简体   繁体   中英

Update Inactive JPanel on Inactive JFrame

I have 2 JFrames for a game with multiple windows. 1 JFrame is the main one while the other is just for info. Is it possible to update the inactive info jframe without having to click the inactive window ? I don't know anything with that ability. PS I DONT want to use libraries just pure java. If the library is TINY then I might use it.

Your question actually has less to do with windows and JFrames and much more to do with the general issue of communication between object. If you want to change the state of an object, you can call one of its methods. The same can be done for your "JFrames", by having the active code, whatever or wherever that is, call a method of one of your other "non-active" display component objects, thereby having then alter their displays. Often the issue becomes one of when to call a method, and with event-driven GUI's, this often means use of an observer pattern of one sort or another.

If my answer seems a bit vague and general, I'm afraid that this is the best I can do given the information so far presented. If you need more specific help, then consider posting relevant code, and more information about your overall problem and program structure.

Also, read about The Use of Multiple JFrames, Good/Bad Practice? as your overall GUI design with its multiple JFrames, while a common newbie program design, may be annoying to the users. It may be better to display multiple views in other ways.

It is possible, basically one JFrame has to have a reference to the other. You said one of your JFrame is the main one, and the other a info one. The idea is to have a member variable which will be the reference to the other JFrame (or JPanel in your case). You can then use that reference to update it (repainting it, perhaps). Of course another solution would be to have a separate class which has references to all the JFrame/JPanel, and can therefore access them all. Note that you can also set the focus to a JFrame using requestFocusInWindow().

Here's a sample code to show you the principle.

import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Main { public static void main(String[] args) { JFrame childFrame = new JFrame("Child Frame"); JPanel childPanel = new JPanel(); childFrame.setSize(300, 200); childFrame.add(childPanel); MainFrame mainFrame = new MainFrame(childPanel); mainFrame.setVisible(true); childFrame.setVisible(true); } } class MainFrame extends JFrame { private JPanel childPanel; public MainFrame(JPanel childPanel) { super(); this.childPanel = childPanel; setSize(300,200); setTitle("Main Frame"); actOnChild(); } public void actOnChild() { // Do something... childPanel.add(new JLabel("I was updated with something!")); } }

The output:

在此处输入图片说明

You'll see the child (info) JFrame's panel will contain the JLabel, which we added through a method of the first JFrame, effectively updating the second JFrame from the first one. This is possible from any other class which has that reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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