简体   繁体   中英

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). The JButton is within a 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

I have not yet been able to confirm if this works because I do not know how to import the libraries into eclipse. For example import org.eclipse.swt.SWT; gives the error "... cannot be resolved".

So one possible solution is how to import in Eclipse. Another possible solution is to dynamically change the text within the JPanel somehow.

As Ben mentioned in his comment. I would set a jLabel with a blank text to start. 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

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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