[英]Input from JOptionPane, output in JTextArea in JFrame?
您如何在JFrame中制作一個JTextArea來接受來自JOptionPane的多個輸入? 那有可能嗎? 感謝任何人的幫助!
JFrame
JTextArea
。 使其成為成員變量 append
方法將文本附加到JTextArea
(不要忘記檢查空/空字符串) 這是一個示例程序:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class InputTest extends JFrame {
private final JTextArea textarea;
private final JButton button;
public InputTest(String title) {
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textarea = new JTextArea(5,30);
button = new JButton("new input");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String input = JOptionPane.showInputDialog(InputTest.this, "Please enter some text");
if((input != null) && !input.isEmpty()) {
textarea.append(input);
textarea.append(System.getProperty("line.separator"));
}
}
});
JPanel p = new JPanel(new BorderLayout());
p.add(textarea, BorderLayout.NORTH);
p.add(button, BorderLayout.SOUTH);
this.getContentPane().add(p);
}
public static void main(String[] args) {
InputTest it = new InputTest("Input Test");
it.setSize(200, 200);
it.setVisible(true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.