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

时间:2014-10-30 16:40:19

标签: java swing neo4j jframe cypher

导入没问题,我只想在点击搜索按钮后在第二个JFrame中打印出JTextArea中的结果。

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

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

1 个答案:

答案 0 :(得分:4)

问题和建议:

  1. 默认情况下,JFrame contentPanes使用BorderLayout。
  2. 如果您向BorderLayout使用容器添加任何内容而未指定放置位置,则默认情况下它将转到BorderLayout.CENTER位置。
  3. BorderLayout.CENTER位置中的某些内容将掩盖之前添加的任何内容。
  4. 您真的不希望您的GUI有多个JFrame。也许是单独的对话窗口,或者通过CardLayout交换视图,但不能超过一个主程序窗口。请查看The Use of Multiple JFrames, Good/Bad Practice?
  5. 您在JFrame 之前调用setVisible(true) 向其添加所有组件,这可能会导致GUI的不可视化或不准确的可视化。在添加您最初添加的所有组件后,请致电setVisible(true)
  6. 为什么你的类在JFrame从未用作JFrame时会扩展它?
  7. 您的关键代码全部包含在静态方法中,失去了Java使用面向对象编程范例的所有优点。我自己,当我创建一个新项目时,我首先专注于创建类,然后是GUI的第二个。

  8. 覆盖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();
       }
    }