简体   繁体   English

如何获取文本字段值并在下一个JFrame中打印Neo4j Cypher查询的结果?

[英]How do I get the textfield value and print the result of my Neo4j Cypher query in the next JFrame?

The imports are okay, I just want to print out the result in the JTextArea in the second JFrame after the search button is clicked. 导入是可以的,我只想在单击搜索按钮后在第二个JFrame的JTextArea中打印结果。

When I run this program, my whole JFrame panel shows a huge full screen button named 'search'. 当我运行该程序时,我的整个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();
            }
         }
      });
   }
}

Issues and suggestions: 问题和建议:

  1. JFrame contentPanes use BorderLayout by default. JFrame contentPanes默认使用BorderLayout。
  2. If you add anything to a BorderLayout-using container without specifying where to place it, it will go by default to the BorderLayout.CENTER position. 如果将任何内容添加到使用BorderLayout的容器中,但未指定放置位置,则默认情况下它将移至BorderLayout.CENTER位置。
  3. Something in the BorderLayout.CENTER position will cover up anything previously added there. BorderLayout.CENTER位置中的内容将掩盖以前在其中添加的所有内容。
  4. You really don't want your GUI to have more than one JFrame. 您确实不希望您的GUI具有多个JFrame。 A separate dialog window perhaps, or a swapping of views via CardLayout perhaps, but not more than one main program window. 可能是一个单独的对话框窗口,或者可能是通过CardLayout交换视图,但不超过一个主程序窗口。 Please check out The Use of Multiple JFrames, Good/Bad Practice? 请检查使用多个JFrame,良好/不良做法? .
  5. You're calling setVisible(true) on your JFrame before adding all components to it, and this can lead to non-visualization or inaccurate visualization of your GUI. 将所有组件添加到JFrame 之前,您要在JFrame上调用setVisible(true) ,这可能导致GUI的不可视化或不正确的可视化。 Call setVisible(true) after adding all components that you're adding initially. 在添加最初要添加的所有组件之后,调用setVisible(true)
  6. Why does your class extend JFrame when it's never used as a JFrame? 为什么您的类在从未用作JFrame的情况下扩展了JFrame?
  7. Your critical code is contained all within a static method losing all the advantages of Java's use of the object-oriented programming paradigm. 您的关键代码全部包含在静态方法中,从而失去了Java使用面向对象编程范例的所有优势。 Myself, when I create a new project, I concentrate on creating classes first, and then GUI's second. 我自己,当我创建一个新项目时,我专注于首先创建类,然后是GUI。

Solution for the button covering up your GUI -- read up on and use layout managers in a smart and pleasing way to create smart and pleasing GUI's. 覆盖GUI的按钮的解决方案-以一种智能且愉悦的方式阅读并使用布局管理器,以创建智能且令人愉悦的GUI。 The tutorials can be found here: 教程可以在这里找到:

For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM