2 个 JFrames 之间的 Java 通信



我在下面的代码中得到了一个AWT-EventQueue-0空指针异常,但我无法修复它。

我想要一个主框架,通过按下按钮从中我打开第二个框架,在那里我可以选择创建新播放器,这将显示在主框架中。我将引用传递给构造函数,但仍然不断收到错误。

我会很高兴得到一些帮助,谢谢!

public class Main { 
   public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {                     
                 @Override
                 public void run() {
                       new App();
                 }
          });
   }
}
public class App extends JFrame {
   private MainPanel mainPanel;
   private SecondPanel secondPanel;
   public App() {
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setSize(800, 600);
          secondPanel = new SecondPanel(mainPanel);
          mainPanel = new MainPanel(secondPanel);
          add(mainPanel);
   }
}
public class MainPanel extends JPanel {
   private JTextArea textArea;
   private JScrollPane scrollPane;
   private JButton options;          

   public MainPanel(SecondPanel secondPanel) {
          textArea = new JTextArea();
          textArea.setColumns(20);
          textArea.setRows(5);
          textArea.setSize(300, 300);
          textArea.setVisible(true);
          textArea.setEditable(false);
          scrollPane = new JScrollPane(textArea);
          scrollPane.setSize(new Dimension(400, 400));

          options = new JButton("Options");
          options.addActionListener(new ActionListener() {
                 @Override
                 public void actionPerformed(ActionEvent e) {
                      secondPanel.setVisible(true);
                 }
          });

          add(scrollPane);
          add(options);
   }

   public JTextArea getTextArea() {
          return textArea;
   }

   public void setTextArea(JTextArea textArea) {
          this.textArea = textArea;
   }
}
public class SecondPanel extends JFrame {
   private JButton create, remove;
   private JPanel panel;
   public SecondPanel(MainPanel mainPanel) {
          setVisible(false);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setSize(400, 400);
          panel = new JPanel();
          panel.setLayout(new BorderLayout());
          create = new JButton("create");
          create.addActionListener(new ActionListener() {
                 @Override
                 public void actionPerformed(ActionEvent e) {
                      mainPanel.getTextArea().append("New Player Created");
                 }
          });
          remove = new JButton("remove");
          remove.addActionListener(new ActionListener() {
                 @Override
                 public void actionPerformed(ActionEvent e) {
                       mp.getTextArea().setText("Player removed");
                 }
          });
          add(panel);
          panel.add(create, BorderLayout.EAST);
          panel.add(remove, BorderLayout.WEST);
   }
}

好吧,它必须是"null",因为在将 MainPanel 交给 secondPanel 之前,您不会对其进行初始化。

相反,请创建一个自己的方法来在第二个面板上设置主面板,并在主面板上设置第二个面板。

我看到您需要在构造函数中的第二个面板来设置操作侦听器。在你的"setSecondPanel(SecondPanel sPanel("中这样做,正如我之前提到的,你应该创建。

最新更新