Java 将方法结果添加到窗口/框架

Java 将方法结果添加到窗口/框架,java,swing,jframe,Java,Swing,Jframe,我的程序目前正在运行。它显示了对控制台调用event.WQLookup()方法的结果 我试图修改代码,以便在窗口框架中的文本区域中显示event.WQLookup()结果。我尝试过添加JTextArea和JLabel的几种方法,但只要我尝试在ActionPerformed方法中添加或附加它们,就会出现错误,原因可能是不允许使用void,或者变量似乎超出范围 这里显示的代码显示了我使用JTextArea进行的追加尝试 我觉得这很可能是一个简单的解决办法,尽管我的研究让我回到了我已经尝试过的建议。我

我的程序目前正在运行。它显示了对控制台调用event.WQLookup()方法的结果

我试图修改代码,以便在窗口框架中的文本区域中显示event.WQLookup()结果。我尝试过添加JTextArea和JLabel的几种方法,但只要我尝试在ActionPerformed方法中添加或附加它们,就会出现错误,原因可能是不允许使用void,或者变量似乎超出范围

这里显示的代码显示了我使用JTextArea进行的追加尝试

我觉得这很可能是一个简单的解决办法,尽管我的研究让我回到了我已经尝试过的建议。我显然是Java新手,所以非常感谢您的指导

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class WQQuery extends JFrame implements WindowListener,ActionListener,Runnable {

JButton button1;
JLabel InterFaceLabel;
JLabel ErrorCodeLabel;
JTextField InterFaceField;
JTextField ErrorCodeField;
JTextArea SQLLabel;
String InterFace = "";
String ErrorCode = "";

public static void main(String[] args) throws IOException {
    WQQuery window = new WQQuery("WQ Lookup");
    window.setSize(400,500);
    window.setVisible(true);
    JTextArea SQLLabel = new JTextArea(2,20);
    window.getContentPane().add(SQLLabel);
}

@SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
public WQQuery(String name) throws IOException {
    super(name);

    setLayout(new FlowLayout());
    addWindowListener(this);

    InterFaceLabel = new JLabel("Interface: ");
    add(InterFaceLabel);

    InterFaceField = new JTextField(10);
    InterFaceField.addActionListener(this);
    add(InterFaceField);

    ErrorCodeLabel = new JLabel("Error Code:");
    add(ErrorCodeLabel);

    ErrorCodeField = new JTextField(10);
    ErrorCodeField.addActionListener(this);
    add(ErrorCodeField);

    button1 = new JButton("Search");
    add(button1);
    button1.addActionListener(this);

}

@Override
public void actionPerformed(ActionEvent e) {
    InterFace = InterFaceField.getText();
    ErrorCode = ErrorCodeField.getText();
    WQSQL event = new WQSQL(InterFace, ErrorCode);
    try {
        SQLLabel.append(event.WQLookup());
    } catch (SQLException | ClassNotFoundException ex) {
        Logger.getLogger(WQQuery.class.getName()).log(Level.SEVERE, null, ex);
    } 
}

@Override
public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
}

@Override
public void windowOpened(WindowEvent e) {}
@Override
public void windowActivated(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowDeactivated(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}

@Override
public void run() {
      } 
}
关键是“不允许使用无效类型”消息。这意味着我正在调用一个void类型的方法,并期望它返回数据

简单的修复方法是将方法WQLookup(用append代码调用)从void更改为字符串类型。简单错误的简单修复

第二个修复方法是将JTextArea SQLLabel初始化移动到WQQuery类:

public class WQQuery extends JFrame implements WindowListener,ActionListener,Runnable {

    JButton button1;
    JLabel InterFaceLabel;
    JLabel ErrorCodeLabel;
    JTextField InterFaceField;
    JTextField ErrorCodeField;
    JTextArea SQLLabel = new JTextArea(2,20);
    String InterFace = "";
    String ErrorCode = "";
此外,实际JTextArea的添加需要与其余组件一起移动:

public WQQuery(String name) throws IOException {
    super(name);

    setLayout(new FlowLayout());
    addWindowListener(this);

    InterFaceLabel = new JLabel("Interface: ");
    add(InterFaceLabel);

    InterFaceField = new JTextField(10);
    InterFaceField.addActionListener(this);
    add(InterFaceField);

    ErrorCodeLabel = new JLabel("Error Code:");
    add(ErrorCodeLabel);

    ErrorCodeField = new JTextField(10);
    ErrorCodeField.addActionListener(this);
    add(ErrorCodeField);

    button1 = new JButton("Search");
    add(button1);
    add(SQLLabel);
    button1.addActionListener(this);
在旁注中,我还添加了一行以清除actionPerfomed方法中的文本区域:

SQLLabel.setText(null);
程序编译后运行良好

我尝试过添加JTextArea和JLabel的几种方法,不过只要我尝试在ActionPerformed方法中添加或附加它们

虽然可以动态地将组件添加到现有GUI中,但要使其正常工作有点困难,我觉得有更好的策略。这些战略之一是:

  • pack()之前添加组件
  • setVisible(true)
    添加所有组件后
  • 在用户或GUI操作后更新组件的文本内容或状态。

您能否显示从
WQSQL
查询中获取结果并尝试更新UI的位置(可能只是使用
JTextArea
)?我已经添加了附加代码和我使用的JTextArea代码。1)在
pack()
之前添加组件,然后仅在
setVisible(true)
之后添加组件。在用户或GUI操作后更新组件的文本内容或状态。2) 请学习常见的Java命名法(命名约定-例如,
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是一个
大写常量
),并始终如一地使用它。“你能…”回答谁?提示:添加@MadProgrammer(或任何人,
@
很重要)以通知此人新的评论。@AndrewThompson感谢提供有关如何使用堆栈溢出的提示。你的第一个建议是对的。我建议在将来添加这样的答案作为一个实际的答案帖子,这样它就可以作为一个可行的答案被充实出来,并得到适当的评分,而不是留在评论中。从你的销售代表分数和历史记录来看,你显然很清楚这一过程,我想知道在这方面有所不同的好处是什么?也许,你能告诉我这会有什么好处?