JTextArea.getText() 未返回应有的内容 (SWING)
JTextArea.getText() not returning what it should (SWING)
我有以下代码用于上面带有 JTextArea 的 JScrollPane。
这样做的目的是创建一个查询并通过 JDBC
将其发送到数据库
// 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 工作正常,因为我硬编码的其他查询工作正常(基本上每个查询 1 个按钮)。但是我无法从这个 TextArea 中获取文本。
当我 运行 程序时,控制台打印:
The query is:
Conecction Successfull
org.postgresql.util.PSQLException: No result from query. //Translated
关于 getText() 为何不返回我在 TextArea 中键入的内容的任何想法?
再次查看您的代码后,这显然是行不通的:
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);
}
以及初始化 GUI 的位置:
this.getBtnFinishButton().addActionListener(new SaveQueryListener(display, this));
我有以下代码用于上面带有 JTextArea 的 JScrollPane。 这样做的目的是创建一个查询并通过 JDBC
将其发送到数据库 // 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 工作正常,因为我硬编码的其他查询工作正常(基本上每个查询 1 个按钮)。但是我无法从这个 TextArea 中获取文本。 当我 运行 程序时,控制台打印:
The query is:
Conecction Successfull
org.postgresql.util.PSQLException: No result from query. //Translated
关于 getText() 为何不返回我在 TextArea 中键入的内容的任何想法?
再次查看您的代码后,这显然是行不通的:
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);
}
以及初始化 GUI 的位置:
this.getBtnFinishButton().addActionListener(new SaveQueryListener(display, this));