用Java中的JButton打开后,窗口无法正常显示

时间:2017-11-28 14:31:08

标签: java swing jbutton

我的java应用程序有一个非常奇怪的问题。我基本上单击了JButton并打开了我要打开的新窗口,但没有显示任何内容。这是发生的事情。

This is what happens after clicking the button

如果我在不使用JButton的情况下自行运行该类,它会像这样运行。 The window working without JButton

以下是该按钮的代码。

public Create()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 629, 316);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(255, 255, 204));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnBuildData = new JButton("Build Graph");
        btnBuildData.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {

                //CreateTest frame = new CreateTest();
                new CreateTest().setVisible(true);


            }
        });

        btnBuildData.setBackground(Color.WHITE);
        btnBuildData.setForeground(Color.BLACK);
        btnBuildData.setBounds(29, 59, 107, 23);
        contentPane.add(btnBuildData);

这对我来说很奇怪,因为我已将此代码用于其他类,并且它按预期工作。我尝试了许多不同的方法来做同样的事情,但没有一个方法有效。这是我用按钮打开的框架的一些代码。

public class CreateTest extends JFrame {
    public CreateTest() {
    }
    //Create the connection to Neo4j
    Driver  driver      = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "*****", "*******" ) );
    Session session     = driver.session();
    StatementResult resultVariable;
    Record          recordVariable;

    //Create variables to manage communication with Neo4j
    String  resultString = new String();
    Query   neoQuery = new Query();





    private JTextField progressTextField;


    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            @Override
            public void run() 
            {
                new CreateTest().initUI();



            }
        });
    }

    protected void initUI() 
    {
        final JFrame frame = new JFrame();

        //the form
        frame.setTitle(CreateTest.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        JButton button = new JButton("Click here to add data");
        button.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {


                doWork();
            }
        });




        progressTextField = new JTextField(25);
        progressTextField.setEditable(false);
        frame.getContentPane().add(progressTextField, BorderLayout.NORTH);
        frame.getContentPane().add(button, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    protected void doWork() {
        SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
            @Override
            protected Void doInBackground() throws Exception {
                CreateTest frame = new CreateTest();
}


            @Override
            protected void process(List<Integer> chunks) {
                progressTextField.setText(chunks.get(chunks.size() - 1).toString());
            }

            @Override
            protected void done() {
                progressTextField.setText("Success: All Nodes have been added ");
            }
        };
        worker.execute();
    }


}

两个窗口之间存在差异。一个是绝对布局和另一个jpanel但我不认为这是问题。如果有任何想法请告诉我,任何想法将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

使用新的CreateTest()

调用空构造函数
public CreateTest() {
}

由于您的代码不在其中,因此无效。