简体   繁体   中英

Overflow on JFrame instance inside of ActionListener

I have an issue with a program that I'm writing. I put you in context...

I try to implement the: MVC pattern in this program, so, basically, I separate the classes, in different packages.

I have a: View, and controller packages, in the view package I have the: UserInterface class, that extends of JFrame, and contains a: JMenuBar, JMenu, and JMenuItem...

You can add tabs with the JMenuItem, this JMenuItem, gets and adds an: ActionListener, that actionlistener comes from another class, this class is: Item, and implements an actionlistener, this actionlistener create a tab, and add to the JFrame.

Finally, the Main class, just invok the UserInterface and run the program.

The issue is this... In the: Item class, that implements ActionListener, I need to make an instance of the: UserInterface class, to can add the tabs, but when I do this, and run the program, the tabs added, but the JFrame start to overflows in each click, I refer to open, open, and open the JFrame multiple times.

I know this happens cause I make an instance in the class that implements: ActionListener, but I can't fix.

The most logic for me, was make the instance of the: UserInterface, outside of the actionlistener method, just to call the identifier of instance, and add the tabs to the user interface, but nothing works.

I spend a lot of hours trying to fix this, but I can't... The objetive of this, fix the issue, and avoid the overflows of the JFrame, I refer to just add the tabs to the frame, and fix the issue with the multiple opening frames.

Code:

Main class:

package controller;
import view.*;

public class Main{

public void InvokeMain(){
UserInterface ui = new UserInterface();}

public static void main(String args []){

Main m = new Main();
m.InvokeMain();}}

UserInterface class:

package view;
import controller.*;
import javax.swing.*;

public class UserInterface extends JFrame{
private static final long serialVersionUID = 1L;
public JMenuBar navbar = new JMenuBar();
public JMenu file = new JMenu("File");
public JMenuItem tab = new JMenuItem("Tab");

public UserInterface(){
Item action = new Item();
setSize(800, 400);
setDefaultCloseOperation(UserInterface.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setJMenuBar(navbar);
setVisible(true);

file.add(tab);
tab.addActionListener(action);
navbar.add(file);}}

Item class:

package controller;
import view.*;
import javax.swing.*;
import java.awt.event.*;

public class Item implements ActionListener{
public static JTabbedPane tabs = new JTabbedPane(); 

public void actionPerformed(ActionEvent A){
UserInterface user = new UserInterface();
tabs.addTab("untitled", null, null, "untitled");
user.add(tabs);}}

¿Someone can help me to fix this?, or ¿Give me an idea? Hope someone can help me, I really appreciate your time, regards.

In such a problem, I would avoid to create new instances of your class UserInterface . Instead of that, I would define it as a singleton object and just access it all through your application.

Following Josh Bloch's suggestion ( http://www.informit.com/articles/article.aspx?p=1216151&seqNum=3 ), I would use an enum to implement UserInterface:

public enum UserInterface {
APP;

private JFrame frame = new JFrame();

...

public addTab(JTabbedPane tabs) {
   this.frame.add(tabs);
}

...
}

And in your Item class:

...
public void actionPerformed(ActionEvent A){
tabs.addTab("untitled", null, null, "untitled");
UserInterface.APP.addTab(tabs);}}
...

This would force you to rearchitecture your application, but it has many advantages:

  • Your main class should not extend JFrame. This way, you have a more fine grained design that would allow you to easily compose your UI elements if you need to redesign
  • You fix in an easy and thread-safe way your access to your main frame.

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