简体   繁体   English

Java Swing中的文本输出

[英]Text Output in Java Swing

When a user clicks a JButton in my Java-Swing application, a string is returned from a method and the user then needs to be able to read the string (somehow). 当用户单击Java-Swing应用程序中的JButton时,方法会返回一个字符串,然后用户需要能够以某种方式读取该字符串。 The JButton is within a JPanel. JButton在JPanel中。 My first thought was to create an 'alert' dialogue (thinking this would be easy), I tried to follow this example that looked easy: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DialogExample.htm 我的第一个想法是创建一个“警报”对话框(认为这很容易),我尝试遵循看起来很简单的示例: http : //www.java2s.com/Code/Java/SWT-JFace-Eclipse/DialogExample .htm

I have not yet been able to confirm if this works because I do not know how to import the libraries into eclipse. 我还不能确认这是否可行,因为我不知道如何将库导入eclipse。 For example import org.eclipse.swt.SWT; 例如import org.eclipse.swt.SWT; gives the error "... cannot be resolved". 给出错误“ ...无法解决”。

So one possible solution is how to import in Eclipse. 因此,一种可能的解决方案是如何在Eclipse中导入。 Another possible solution is to dynamically change the text within the JPanel somehow. 另一个可能的解决方案是以某种方式动态更改JPanel中的文本。

As Ben mentioned in his comment. 正如Ben在评论中提到的那样。 I would set a jLabel with a blank text to start. 我将设置一个jLabel并带有空白文本。 Then, when you click your button that triggers the method, simply tack on: 然后,当您单击触发该方法的按钮时,只需继续操作即可:

label.setText(value);

Alternatively you could use another pane to popup and display the message. 或者,您可以使用另一个窗格来弹出并显示消息。

In this link you will find the information u require in order to add the library and import it: 在此链接中,您将找到您需要的信息以添加库并导入它:

http://www.eclipsepluginsite.com/swt.html http://www.eclipsepluginsite.com/swt.html

as well I would not mix SWT with swing and I would stick to swing you can set the label on the event of your button 同样,我不会将SWT与Swing混合使用,而我会坚持Swing,您可以在按下按钮时设置标签

button.addActionListener(new ActionListener {
    public void actionPerformed(ActionEvent e) {
        String message = "Test String";
        labelMessage.setText(message );
    }
}

If you're talking about Swing, an easy solution is to pop a message box with the string: 如果您正在谈论Swing,一个简单的解决方案是使用以下字符串弹出一个消息框:

button.addActionListener(new ActionListener {
    public void actionPerformed(ActionEvent e) {
        String message = methodThatReturnsYourString();
        JOptionPane.showMessageDialog(null, message);
    }
}

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

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