February 3, 2005 3:41:41
Question: If there are two jpopupmenu: a, Ba has a JMenu, B is also, but B is more than one JMenuitem
We use uppercase letters represent the name on the button
__D button A: A__c__ | __e | __f
__D c__ | __e | | __f button B - | | |
I originally wrote this:
JPopupmenu a = new jpopupmenu ("a"); jpopupmenu b = new jpopupmenu ("b"); jMenu c = new jpopunu (); // wants A and B public C * _ * jMenuitem D = new jMenuItem ("D "); JMenuitem e = new jMenuitem (" e "); jMenuitem f = new jmenuitem (" f "); C.Add (d); C.Add (e); C.Add (f); A.Add ( c); now the actual situation: A button is like __da__c__ | __e | __f is ok, continue. B.Add (c); // This statement is the reason for the back failed JMenuItem G = New JmenuiteM ("g"); B.Add (g); now I think about what I think is: __da__c__ | __e | __f
__D c__ | __e | | __f b-- | |
But a component cannot be in both COMPOMENT, so in B.Add (c); this sentence, this sentence is originally in the top of the B. So now the actual situation is:
-
__D c__ | __e | | __f b - | | --g is as we see, buttons a do n'thing :(
So what do you do? ? The first solution that is first in my mind is that a write, a and b repeated part of the step, and it is necessary to write twice. For example, write: (Of course I don't have this way, build a variable for repeating each button, name, set the font, set the color ... too tired)
JPopupMenu A = New JPopUpMenu ("a"); jpopupmenu b = new jpopupmenu ("b"); jMenu c = new jpopunu (); jMenuitem D = new jMenuItem ("d"); jMenuItem E = New Jmenuitem ("e" ); JMenuitem f = new jMenuitem ("f"); C.Add (d); C.Add (e); C.Add (f); A.Add (c); // A End JMENU G = New JPopupmenu (); JMenuitem h = new jMenuitem ("D"); jMenuitem i = new jMenuItem ("e"); jMenuitem j = new jMenuitem ("f"); g.Add (h); g.Add (i); G.Add (j); jMenuitem k = new jmenuitem ("g"); B.Add (g); B.Add (k); // B is ended, the current actual result must be correct. It seems to assume that there is not much code amount, but as mentioned earlier, we are just a demonstration, plus font, color, listener, etc. Actually the amount of code is very big! In fact, the biggest problem is not the problem of code quantity, the problem is that your program is reducing! ! To know that the Swing program member must not only build a complex and friendly interface, but also write a lot of various events, and find a button in a big pile of hard-reading code, it is better to go to the cinema to see 100 times "2046": (2046 is really ugly, I said it straight) We need to make our program readability, I don't remember which master says: In a business cycle of a program, 10% time is used to write It, 90% of the time is brought to people. For many reasons, we must solve the problem in an elegant way :) I started thinking like this: I can write a MyPopupMenu Extends JPopUpMenu if mypopupmenu p = new mypopUpMenu (); some MENUITEM already exists in P?
After practical, practice is truth :) Soon I get it. The method is like this: make our own MyjPopUpMenu class, which inherits from JPopupmenu, write all the public parts, so no matter how JMenuitem is not annoyed. MyjpopupButton class is as follows: Suppose in the main class managerui.java PUBLIC Class Managerui {Private ... private font font = new font (...); private color buttoncolor = new color (...); ... public managerui () {...} // Here is JPopupMenu listener private class MyPopupMenuListener implements ActionListener {......} private class MyJPopupMenu extends JPopupMenu {private JMenuItem d = new JMenuItem ( "D"); private JMenuItem e = new JMenuItem ( "E"); private JMenuItem f = New jMenuitem ("f"); public myjpopupmenu ()} public myjpopupmenu (String Name) {super (name);} // All in this method is one-time, each component, etc., you will only call it. This // public method is OK Public Void AddPublicMenuItem () {// set font D.SetFont (Font); // Set color D.SetBackgroundColor (ButtonColor); E.SetFont (font); e.setBackgroundColor (buttonColor); f.setFont (font); f.setBackgroundColor (buttonColor); MyPopopMenuListener mp = new MyPopopMenuListener (); d.addActionListener (mp); e.addActionListener (mp); f.addActionListener (mp); C.Add (d); C.Add (e); c.Add (f); add (c); // Note add () method is}}}}}}}}}}}}}}}
In places where JPopupMenu is needed to generate JPopUpMenu, the code is like this: public class managerui {private ... private font font = new font (...); private color buttoncolor = new color (...); Private jpopupmenu a; // Note that we are using public categories, myjpopupmenu's private jpopupmenu b; // parental category, etc. will need to transform down ... public managerui () {... a = New myjpopupmenu ("a"); // very simple (MyjPUPMENU) A.addpublicMenuItem (); // Remember to transform down the down myjpopUpMenu ("b"); (MyjpopupMenu) B.AddpublicMenuItem (); jMenuItem c = new JMenuItem ("g"); B.Add (c); .....} // Here is the jpopupmenu's listener private class mypopupmenulistener implements anctionsListener {public void actionPerformed (ActionEvent Event) {string id = ((JMenuItem) Event .getsource ()). getActionCommand (); if (id.equals ("d")) {...} else if (id.equals ("e")) {...} else if (id.equals) (iQUALS) "F")) {...} else if (id.equals ("g")) {...}}} private class myjpopUpMenu Extends jpopupMenu Implement ActionListener {Private Jmenuitem D = New Jmenuitem ("D"); private jMenuItem E = New Jmenuitem ("E"); private jMenuitem f = new jMenuitem ("f"); public myjpopupnu () {} public myjpopupmenu (String Name) {super (name);} public void addpublicMenu () {D.SetFont (font); D.SetBackgroundColor (ButtonColor); E.SETFONT (FONT);
e.setBackgroundColor (buttonColor); f.setFont (font); f.setBackgroundColor (buttonColor); MyPopopMenuListener mp = new MyPopopMenuListener (); d.addActionListener (mp); e.addActionListener (mp); f.addActionListener (mp); C.Add (d); C.Add (e); c.Add (f); add (c);}} The last job is not in the case, absolutely correct :) End? I remember that James Cooper said anyway, he thinks more, the more difficult management, we can further optimize, delete the JPopupMenu button listener class, why not? Very simple, just use myJPopupMenu classes to implement the ActionListener interface to turn this:
Public class managerui {private ... private font font = new font (...); private color buttoncolor = new color (...); private jpopupmenu a; private jpopupmenu b; ... public managerui () {.. .... a = new myjpopupmenu ("a"); (MyjpopupMenu) a.addpublicMenuItem (); b = new myjpopupmenu ("b"); (myjpopupmenu) B.AddpublicMenuItem (); jMenuItem C = New Jmenuitem ("g "); B.Add (c); .....} private class myjpopupmenu Extends JPopUpMenu Implement ActionListener {Private JMenuItem D = New Jmenuitem (" D "); Private JMenuItem E = New Jmenuitem (" e "); private jMenuItem f = new jMenuitem ("f"); public myjpopupmenu () {} public myjpopUpMenu (String Name) {super (name);} public void addpublicMenu () {D.SetFont (font); D.SetBackGroundc olor (buttonColor); e.setFont (font); e.setBackgroundColor (buttonColor); f.setFont (font); f.setBackgroundColor (buttonColor); MyPopopMenuListener mp = new MyPopopMenuListener (); d.addActionListener (this); // Note: Remember to change this to this e.addactionListener (this); f.AddActionListener (this); C.Add (d); C.Add (e); C.Add (f); add (c);} / / Write the actionPerformed () method here public void action {string id = ((jMenuItem) Event.getsource ()). GetActionCommand ();