Java 如何获取textfield值并在下一个JFrame中打印neo4jcypher查询的结果?

Java 如何获取textfield值并在下一个JFrame中打印neo4jcypher查询的结果?,java,swing,neo4j,jframe,cypher,Java,Swing,Neo4j,Jframe,Cypher,导入是可以的,我只想在点击搜索按钮后在第二个JFrame的JTextArea中打印结果 当我运行这个程序时,我的整个JFrame面板显示一个巨大的全屏按钮,名为“搜索” import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private static final long serialVersionUID = 1L; private static final Strin

导入是可以的,我只想在点击搜索按钮后在第二个JFrame的JTextArea中打印结果

当我运行这个程序时,我的整个JFrame面板显示一个巨大的全屏按钮,名为“搜索”

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

public class Main extends JFrame {

   private static final long serialVersionUID = 1L;
   private static final String DB_PATH = "C:/Users/Abed/Documents/Neo4j/Movies2.graphdb";
   public static GraphDatabaseService graphDB;

   public static void main(String[] args) throws Exception {

      showFrame();
   }

   public static void showFrame() {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      frame.setSize(500, 500);

      final JTextField actorInput = new JTextField(10);
      final JTextField genreInput = new JTextField(10);

      frame.getContentPane().add(actorInput);
      frame.getContentPane().add(genreInput);

      JButton submit = new JButton("Search");
      submit.setSize(5, 5);
      frame.getContentPane().add(submit);
      submit.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent event) {

            graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
            Transaction tx = graphDB.beginTx();

            String actor = actorInput.getText();
            String genre = genreInput.getText();

            final String query;

            query = "MATCH (Movie {genre:'"
                  + genre
                  + "'})<-[:ACTS_IN]-(Person {name:'"
                  + actor
                  + "'}) "
                  + "RETURN Person.name, Movie.genre, COUNT(Movie.title) AS cnt "
                  + "ORDER BY cnt DESC " + "LIMIT 100 ";

            try {
               JFrame frame2 = new JFrame();
               frame2.setVisible(true);
               JTextArea ta = new JTextArea();
               ta.setLineWrap(true);
               frame2.add(ta);
               ExecutionEngine engine = new ExecutionEngine(graphDB,
                     StringLogger.SYSTEM);
               ExecutionResult result = engine.execute(query);
               System.out.println(result);
               String resultArea = result.dumpToString();
               ta.setText(resultArea);
               tx.success();
            } finally {
               tx.close();
               graphDB.shutdown();
            }
         }
      });
   }
}
导入java.awt.event.*;
导入javax.swing.*;
公共类主框架{
私有静态最终长serialVersionUID=1L;
私有静态最终字符串DB_PATH=“C:/Users/Abed/Documents/Neo4j/Movies2.graphdb”;
公共静态GraphDatabaseService graphDB;
公共静态void main(字符串[]args)引发异常{
showFrame();
}
公共静态无效显示框(){
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
框架。设置尺寸(500500);
最终JTextField actorInput=新的JTextField(10);
最终JTextField genreInput=新JTextField(10);
frame.getContentPane().add(actorInput);
frame.getContentPane().add(genreInput);
JButton submit=新JButton(“搜索”);
提交。设置大小(5,5);
frame.getContentPane().add(submit);
submit.addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件){
graphDB=new GraphDatabaseFactory().newEmbeddedDatabase(DB_路径);
事务tx=graphDB.beginTx();
String actor=actorInput.getText();
字符串类型=genreInput.getText();
最终字符串查询;
query=“匹配(电影{流派:'”
+体裁

+“})问题和建议:

  • 默认情况下,JFrame内容窗格使用BorderLayout
  • 如果使用容器向BorderLayout添加任何内容,但未指定放置位置,则默认情况下,它将转到BorderLayout.CENTER位置
  • BorderLayout.CENTER位置中的某些内容将覆盖以前添加到此处的任何内容
  • 您真的不希望您的GUI有多个JFrame。可能需要一个单独的对话框窗口,或者通过CardLayout交换视图,但不能有多个主程序窗口。请签出
  • 在将所有组件添加到JFrame之前,您正在JFrame上调用
    setVisible(true)
    ,这可能会导致GUI的非可视化或不准确可视化。在添加最初添加的所有组件之后调用
    setVisible(true)
  • 为什么您的类在从未用作JFrame的情况下扩展JFrame
  • 您的关键代码都包含在一个静态方法中,失去了Java使用面向对象编程范例的所有优势。我自己在创建一个新项目时,首先集中精力创建类,然后再创建GUI

  • 覆盖GUI的按钮解决方案--阅读并以智能和令人愉快的方式使用布局管理器创建智能和令人愉快的GUI。教程可在此处找到:

    例如:

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dialog.ModalityType;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    
    import javax.swing.*;
    
    public class MyMain extends JPanel {
       private JTextField actorInput = new JTextField(10);
       private JTextField genreInput = new JTextField(10);
    
       public MyMain() {
          // JPanels use FlowLayout by default
    
          add(actorInput);
          add(genreInput);
          add(new JButton(new SubmitAction("Submit")));
          add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
       }
    
       private class SubmitAction extends AbstractAction {
          public SubmitAction(String name) {
             super(name);
          }
    
          @Override
          public void actionPerformed(ActionEvent evt) {
             String actor = actorInput.getText();
             String genre = genreInput.getText();
    
             // call database code here in a background thread
             // show a dialog with information
    
             String textToDisplay = "Text to display\n";
             textToDisplay += "Actor: " + actor + "\n";
             textToDisplay += "Genre: " + genre + "\n";
    
             InfoDialogPanel dlgPanel = new InfoDialogPanel();
             dlgPanel.textAreaSetText(textToDisplay);
    
             Window window = SwingUtilities.getWindowAncestor(MyMain.this);
             JDialog modalDialog = new JDialog(window, "Dialog", ModalityType.APPLICATION_MODAL);
             modalDialog.getContentPane().add(dlgPanel);
             modalDialog.pack();
             modalDialog.setLocationByPlatform(true);
             modalDialog.setVisible(true);
          }
       }
    
       private static void createAndShowGui() {
          MyMain mainPanel = new MyMain();
    
          JFrame frame = new JFrame("MyMain");
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class InfoDialogPanel extends JPanel {
       private JTextArea textArea = new JTextArea(10, 20);
       private JScrollPane scrollPane = new JScrollPane(textArea);
    
       public InfoDialogPanel() {
          JPanel btnPanel = new JPanel();
          btnPanel.add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));      
    
          setLayout(new BorderLayout());
          add(scrollPane, BorderLayout.CENTER);
          add(btnPanel, BorderLayout.PAGE_END);
       }
    
       public void textAreaSetText(String text) {
          textArea.setText(text);
       }
    
       public void textAreaAppend(String text) {
          textArea.append(text);
       }
    }
    
    class ExitAction extends AbstractAction {
    
       public ExitAction(String name, int mnemonic) {
          super(name);
          putValue(MNEMONIC_KEY, mnemonic);
       }
    
       @Override
       public void actionPerformed(ActionEvent evt) {
          Component component = (Component) evt.getSource();
          Window win = SwingUtilities.getWindowAncestor(component);
          win.dispose();
       }
    }
    

    问题和建议:

  • 默认情况下,JFrame内容窗格使用BorderLayout
  • 如果使用容器向BorderLayout添加任何内容,但未指定放置位置,则默认情况下,它将转到BorderLayout.CENTER位置
  • BorderLayout.CENTER位置中的某些内容将覆盖以前添加到此处的任何内容
  • 您真的不希望您的GUI有多个JFrame。可能需要一个单独的对话框窗口,或者通过CardLayout交换视图,但不能有多个主程序窗口。请签出
  • 在将所有组件添加到JFrame之前,您正在JFrame上调用
    setVisible(true)
    ,这可能会导致GUI的非可视化或不准确可视化。在添加最初添加的所有组件之后调用
    setVisible(true)
  • 为什么您的类在从未用作JFrame的情况下扩展JFrame
  • 您的关键代码都包含在一个静态方法中,失去了Java使用面向对象编程范例的所有优势。我自己在创建一个新项目时,首先集中精力创建类,然后再创建GUI

  • 覆盖GUI的按钮解决方案--阅读并以智能和令人愉快的方式使用布局管理器创建智能和令人愉快的GUI。教程可在此处找到:

    例如:

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dialog.ModalityType;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    
    import javax.swing.*;
    
    public class MyMain extends JPanel {
       private JTextField actorInput = new JTextField(10);
       private JTextField genreInput = new JTextField(10);
    
       public MyMain() {
          // JPanels use FlowLayout by default
    
          add(actorInput);
          add(genreInput);
          add(new JButton(new SubmitAction("Submit")));
          add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
       }
    
       private class SubmitAction extends AbstractAction {
          public SubmitAction(String name) {
             super(name);
          }
    
          @Override
          public void actionPerformed(ActionEvent evt) {
             String actor = actorInput.getText();
             String genre = genreInput.getText();
    
             // call database code here in a background thread
             // show a dialog with information
    
             String textToDisplay = "Text to display\n";
             textToDisplay += "Actor: " + actor + "\n";
             textToDisplay += "Genre: " + genre + "\n";
    
             InfoDialogPanel dlgPanel = new InfoDialogPanel();
             dlgPanel.textAreaSetText(textToDisplay);
    
             Window window = SwingUtilities.getWindowAncestor(MyMain.this);
             JDialog modalDialog = new JDialog(window, "Dialog", ModalityType.APPLICATION_MODAL);
             modalDialog.getContentPane().add(dlgPanel);
             modalDialog.pack();
             modalDialog.setLocationByPlatform(true);
             modalDialog.setVisible(true);
          }
       }
    
       private static void createAndShowGui() {
          MyMain mainPanel = new MyMain();
    
          JFrame frame = new JFrame("MyMain");
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class InfoDialogPanel extends JPanel {
       private JTextArea textArea = new JTextArea(10, 20);
       private JScrollPane scrollPane = new JScrollPane(textArea);
    
       public InfoDialogPanel() {
          JPanel btnPanel = new JPanel();
          btnPanel.add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));      
    
          setLayout(new BorderLayout());
          add(scrollPane, BorderLayout.CENTER);
          add(btnPanel, BorderLayout.PAGE_END);
       }
    
       public void textAreaSetText(String text) {
          textArea.setText(text);
       }
    
       public void textAreaAppend(String text) {
          textArea.append(text);
       }
    }
    
    class ExitAction extends AbstractAction {
    
       public ExitAction(String name, int mnemonic) {
          super(name);
          putValue(MNEMONIC_KEY, mnemonic);
       }
    
       @Override
       public void actionPerformed(ActionEvent evt) {
          Component component = (Component) evt.getSource();
          Window win = SwingUtilities.getWindowAncestor(component);
          win.dispose();
       }
    }
    

    谢谢你的回复/评论。这非常有帮助,我已经找到了一种方法让它工作,但你的肯定也是一个好方法!谢谢你的回复/评论。这非常有帮助,我已经找到了一种方法让它工作,但你的肯定也是一个好方法!