JPanel doesn't update when adding Component in another class

a 夏天 提交于 2019-11-26 10:03:28

问题


I\'m fairly new to Java Swing and I\'m running into a few problems.

  1. As a side question, when making a fairly large Java Swing Application, what is the best way to split up code? In my case I want to have an application that has a layout just as Microsoft Word where there is a JToolBar filled with buttons and a main JPanel where changes are made based on the buttons pressed in the Tool Bar.
  2. So as shown in the code below, I have a JFrame and I call the MainPanel class in order to create a panel and add a ToolBar with a button. When the button is pressed it adds a button to the panel. The problem comes when you click the button nothing shows up until you resize the window(in my case I simply manually drag the screen to make it larger).

    public class Main {
    
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame(\"MathMaker\");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        //Create the menu bar.  Make it have a green background.
        //MainToolBar mainTB = new MainToolBar();
        MainPanel mainPanel = new MainPanel();
    
        frame.getContentPane().add(mainPanel.getGUI(), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application\'s GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
    }
    

    public class MainPanel implements ActionListener{
    JPanel mPanel;
    JToolBar mToolBar;
    JButton addQuestion;
        public MainPanel() {
            mPanel = new JPanel(new BorderLayout());
            mToolBar = new JToolBar();
            addQuestion = new JButton(\"test\");
    
        addQuestion.addActionListener(this);
    
        mPanel.setLayout(new BorderLayout());
        mPanel.setBackground(new Color(248, 213, 131));
        mPanel.setPreferredSize(new Dimension(200, 180));
    
        mToolBar.add(addQuestion);
        mPanel.add(mToolBar, BorderLayout.PAGE_START);
    }
    public JComponent getGUI()
    {
        return mPanel;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
         JButton temp = new JButton(\"temp\");
         mPanel.add(temp);
    }
    

    }


回答1:


You should revalidate your panel

@Override
public void actionPerformed(ActionEvent e) {
   JButton temp = new JButton("temp");
   mPanel.add(temp);
   mPanel.revalidate();
   mPanel.repaint();
}



回答2:


I believe you need to call revalidate() and repaint() to see the changes, here is a similar question here




回答3:


The problem here is the panel is not repainted automatically.. When you resize the panel Java repaints the panel on the screen. Try repainting the panel everytime any button to modify the panel is clicked..

Just call the validate() and repaint() method with the panel



来源:https://stackoverflow.com/questions/25163805/jpanel-doesnt-update-when-adding-component-in-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!