Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java中的JButton打开后窗口显示不正确_Java_Swing_Jbutton - Fatal编程技术网

用Java中的JButton打开后窗口显示不正确

用Java中的JButton打开后窗口显示不正确,java,swing,jbutton,Java,Swing,Jbutton,我的java应用程序有一个非常奇怪的问题。我基本上是单击JButton,然后打开我想要打开的新窗口,但没有显示任何内容。下面是发生的情况 如果我自己运行这个类而不使用JButton,它可能会这样运行。 这是按钮的代码 public Create() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 629, 316); contentPane =

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

如果我自己运行这个类而不使用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();
    }


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

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();
    }


}
公共类CreateTest扩展JFrame{
公共测试{
}
//创建到Neo4j的连接
驱动程序驱动程序=图形数据库。驱动程序(“bolt://localhost,AuthTokens.basic(“*******”,“*******”);
Session Session=driver.Session();
语句结果变量;
记录变量;
//创建变量以管理与Neo4j的通信
String resultString=新字符串();
查询neoQuery=新查询();
私有JTextField progressTextField;
公共静态void main(字符串[]args)
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
新建CreateTest().initUI();
}
});
}
受保护的void initUI()
{
最终JFrame=新JFrame();
//形式
setTitle(CreateTest.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button=newjbutton(“单击此处添加数据”);
addActionListener(新建ActionListener())
{
@凌驾
已执行的公共无效操作(操作事件e)
{
销钉();
}
});
progressTextField=新的JTextField(25);
progressTextField.setEditable(false);
frame.getContentPane().add(progressTextField,BorderLayout.NORTH);
frame.getContentPane().add(按钮,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
受保护的空榫(){
SwingWorker worker=新SwingWorker(){
@凌驾
受保护的Void doInBackground()引发异常{
CreateTest框架=新建CreateTest();
}
@凌驾
受保护的无效进程(列表块){
progressTextField.setText(chunks.get(chunks.size()-1.toString());
}
@凌驾
受保护的void done(){
progressTextField.setText(“成功:已添加所有节点”);
}
};
worker.execute();
}
}

这两扇窗户之间有区别。一个是绝对布局,另一个是jpanel,但我认为这不是问题所在。如果有任何人有任何想法,请让我知道,任何想法将不胜感激。谢谢。

您使用new CreateTest()调用空构造函数


它不做任何事情,因为您的代码在它之外。

使用
new CreateTest().setVisible(true)
您正在创建一个新的
CreateTest
实例并使其可见。但是
CreateTest
的构造函数没有做任何特殊的工作,因此您最终基本上得到一个空框架。但是,在没有JButtonies的情况下运行该框架时,该框架确实包含内容,因为您在action listener中创建了一个新实例,并且永远不要对其调用
initUI()
。^基本上,当您运行文件时,它会调用main方法;当您创建类的实例时,它会运行您调用的构造函数。您的构造函数现在是类
public CreateTest(){}
中的前两行,它不做任何操作。。。3) 在源代码中只需要一行空白就可以了。
{
之后或
}
之前的空行通常也是多余的。4) 在这个例子中,似乎没有令人信服的理由来扩展
JFrame
。相反,代码应该实例化一个标准框架,并根据需要进行调整。5) 请学习常见的Java命名法(命名约定-例如
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是
大写常量
),并一致使用它。