Java – add a gridbaglayout in actionlistener

I have a jmenuitem with an actionlistener. In this actionlistener, I want to add a gridbaglayout to my framework (I may not have added a content pane - for testing purposes, it will not), and then add components to the framework The design of the framework is my own, but I want to trigger it from the actionlistener on jmenuitem. Here is the problem I encountered It will not be displayed from inside the actionlistener I try to run the same code as Al from different methods in the class, and it doesn't work

When I fully annotate the actionlistener, the jlabel I want to test adds GBL in the correct position, and the system prints my debugging lines here and here The compiler did not receive a syntax error This produces the desired results and prints the label (when I completely annotate Al, please look at the image below.) The code snippets of the code (in which framework is my JFrame) are as follows:

// (frame created,menus added,etc.) ...
JMenuItem vPoke1Item = new JMenuItem("Pokemon 1");
vPoke1Item.setActionCommand("poke1");
viewMenu.add(vPoke1Item);

//Setup GBL to view stats for Pokemon 1
  vPoke1Item.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
//        debug output
          System.out.println("here");

//        Set up the content pane
          frame.getContentPane().removeAll();
          GridBagLayout gbl = new GridBagLayout();
          GridBagConstraints gbc = new GridBagConstraints();
          Container pane = frame.getContentPane();
          pane.setLayout(gbl);

//        Make a StatCalcObject (all my labels/fields are already initialized)
          StatCalc1 sc1 = new StatCalc1();

//        Add it to pane
          gbc.gridx = 0;gbc.gridy = 0;gbl.setConstraints(sc1.speciesL,gbc);
          pane.add(sc1.speciesL);
          frame.revalidate();
          frame.repaint();

//        debug output
          System.out.println("here2");
      }
  });
// (etc.)

Now when I run this code, I still get the debug lines "here" and "here2" printed, so it tells me that the actionlistener is running normally But the label does not display There are still no syntax errors picked up by the compiler So I'm grabbing my head here. What did I do wrong? I hope this code fragment is enough to understand this problem, but if you want complete code, I can provide it

Solution

Provides you with a fixed size window. If you replace it, everything will work

frame.revalidate();
frame.repaint();

with

pane.invalidate();
pane.validate();

or

pack();

If you don't have a fixed size frame Please note that JFrame or container does not support revalidation Better yet, replace it

gbl.setConstraints(sc1.speciesL,gbc);
pane.add(sc1.speciesL);

with

pane.add(sc1,gbc);

For better code style

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>