将容器的父级添加到自身

时间:2018-11-06 19:03:35

标签: java swing

我有以下代码:

package com.company;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

    private static JLabel portText = new JLabel("Port:");
    private static JLabel brokerAddressText = new JLabel("Broker ip address:");
    private static JLabel topicText = new JLabel("Topic");
    private static JLabel messageText = new JLabel("Message:");
    private static JTextField port = new JTextField(4);
    private static JTextField brokerAddress = new JTextField(10);
    private static JTextField topic = new JTextField(10);
    private static JTextField message = new JTextField(20);
    private static JButton connect = new JButton("Connect");
    private static JButton disconnect = new JButton("Disconnect");
    private static JButton subscribe = new JButton("Subscribe");
    private static JButton unsubscribe = new JButton("Unsubscribe");
    private static JButton publish = new JButton("Publish");
    private static int iPort;
    private static String sIp;

    public static void main(String[] args) {
        JFrame jFrame = getFrame();

        connect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    iPort = Integer.parseInt(port.getText());
                    sIp = brokerAddress.getText();
                }catch (NumberFormatException exc){
                    System.out.println("Wrong port input");
                }
            }
        });
    }

    private static JFrame getFrame(){
        JFrame jFrame = new JFrame();
        JPanel jPanel = (JPanel) jFrame.getContentPane();
        jFrame.setVisible(true);
        jFrame.setBounds(750,250,500,500);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.add(jPanel);
        jFrame.setTitle("MQTT client");
        portText.setLocation(10,10);
        jPanel.add(portText);
        port.setLocation(10,30);
        jPanel.add(port);
        jPanel.add(brokerAddressText);
        jPanel.add(brokerAddress);
        jPanel.add(connect);
        jPanel.add(disconnect);
        jPanel.add(topicText);
        jPanel.add(topic);
        jPanel.add(subscribe);
        jPanel.add(unsubscribe);
        jPanel.add(messageText);
        jPanel.add(message);
        jPanel.add(publish);
        jPanel.revalidate();
        return jFrame;
    }
}

当我编译程序时,出现以下错误:

 Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself at
 java.desktop/java.awt.Container.checkAddToSelf(Container.java:497) at
 java.desktop/java.awt.Container.addImpl(Container.java:1110) at
 java.desktop/java.awt.Container.add(Container.java:1025) at
 java.desktop/javax.swing.JFrame.addImpl(JFrame.java:553) at
 java.desktop/java.awt.Container.add(Container.java:432) at
 com.company.Main.getFrame(Main.java:50) at
 com.company.Main.main(Main.java:26)

我了解这是由于以下几行:

JPanel jPanel = (JPanel) jFrame.getContentPane();
jFrame.add(jPanel);

但是我不明白在另一种情况下如何添加面板。

1 个答案:

答案 0 :(得分:0)

删除此行

jFrame.add(jPanel);

替换此

jPanel.revalidate();

jFrame.revalidate();

您要重新验证面板,您需要自己重新验证框架。