简体   繁体   English

在另一个类中添加组件时,JPanel不会更新

[英]JPanel doesn't update when adding Component in another class

I'm fairly new to Java Swing and I'm running into a few problems. 我是Java Swing的新手,但是遇到了一些问题。

  1. As a side question, when making a fairly large Java Swing Application, what is the best way to split up code? 附带的问题是,在制作相当大的Java Swing应用程序时,拆分代码的最佳方法是什么? 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. 在我的情况下,我希望有一个布局与Microsoft Word一样的应用程序,其中的JToolBar充满按钮,而主JPanel则根据工具栏中按下的按钮进行更改。
  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. 因此,如下面的代码所示,我有一个JFrame并调用MainPanel类以创建面板并添加带有按钮的ToolBar。 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{ 公共类MainPanel实现ActionListener {\nJPanel mPanel; JPanel mPanel;\nJToolBar mToolBar; JToolBar mToolBar;\nJButton addQuestion; JButton addQuestion;\n    public MainPanel() { 公共MainPanel(){\n        mPanel = new JPanel(new BorderLayout()); mPanel = new JPanel(new BorderLayout());\n        mToolBar = new JToolBar(); mToolBar =新的JToolBar();\n        addQuestion = new JButton("test"); addQuestion = new JButton(“ test”); 

    \n\n 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); }

    } }

You should revalidate your panel 您应该重新验证面板

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

我相信你需要调用重新验证()和重绘()看到的变化,这里是一个类似的问题在这里

The problem here is the panel is not repainted automatically.. When you resize the panel Java repaints the panel on the screen. 这里的问题是面板不会自动重新绘制。.调整面板大小时,Java会在屏幕上重新绘制面板。 Try repainting the panel everytime any button to modify the panel is clicked.. 每次单击任何可修改面板的按钮时,请尝试重新粉刷面板。

Just call the validate() and repaint() method with the panel 只需在面板上调用validate()和repaint()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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