代码之家  ›  专栏  ›  技术社区  ›  João Pedro Abubakir

JTextArea。getText()未返回它应该返回的内容(SWING)

  •  0
  • João Pedro Abubakir  · 技术社区  · 7 年前

    我有以下关于带有JTextArea的JScrollPane的代码。

       // create the middle panel components
            JTextArea display = new JTextArea(16, 58);
            display.setLineWrap(true);
            display.setEditable(true); // set textArea editable
            JScrollPane scroll = new JScrollPane (display);
            scroll.setBounds(19, 21, 487, 294);
            scroll.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            middlePanel.add(scroll, BorderLayout.CENTER);
    
            //Add Textarea in to middle panel
            middlePanel.add(scroll);
    
            JFrame frame = new JFrame();
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.getContentPane().add(middlePanel);
    
            this.setBtnFinishButton(new JButton("FINISH"));
            this.getBtnFinishButton().addActionListener(new SaveQueryListener(display.getText(), this));
            this.getBtnFinishButton().addFocusListener(new CreateQueryWindowFocusListener(this));
            middlePanel.add(btnFinishButton, BorderLayout.SOUTH);
    

    侦听器“SaveQueryListener”的代码如下所示

    private String query;
        private CreateQueryWindow cqw;
    
        public SaveQueryListener(String query, CreateQueryWindow cqw) {
            this.setQuery(query);
            this.setCqw(cqw);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("The query is: "+query); //Use this to know what it is returning
            new PostgreSQLJDBC(this.query);
        }
    

    JDBC工作正常,因为我硬编码的其他查询工作正常(每个查询基本上有一个按钮)。但是我无法从这个文本区域获取文本。

    The query is: 
    Conecction Successfull
    org.postgresql.util.PSQLException: No result from query. //Translated
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   markspace    7 年前

    在第二次查看代码后,这显然不起作用:

    this.getBtnFinishButton().addActionListener(new SaveQueryListener(display.getText(), this));
    

    可能需要这样的东西:

    private JTextArea view;
    
    public SaveQueryListener(JTextArea view, CreateQueryWindow cqw) {
        this.view = view;
        this.setCqw(cqw);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        String query = view.getText();
        System.out.println("The query is: "+query); //Use this to know what it is returning
        new PostgreSQLJDBC(this.query);
    }
    

    this.getBtnFinishButton().addActionListener(new SaveQueryListener(display, this));