我专门写了个给你:
Test.java
import java.awt.*
import java.awt.event.*
import javax.swing.*
public class Test {
public static void main(String[] args) {
new MyFrame()
}
}
class MyFrame extends JFrame {
public MyFrame() {
Container c = this.getContentPane()
c.setLayout(new FlowLayout())
JButton button = new JButton("点击")
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = "hi"//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message)
}
})
c.add(button)
this.setSize(300, 200)
this.show()
}
}
其中输入消息的关键语句是:
JOptionPane.showMessageDialog(rootPane, message)
我不确定你说的“弹出一个文本框”是不是这个意思,如果不是的话补充一下问题我帮你改吧。
可以使用JoptionPane:
有几种提示框:
第一种:
JOptionPane.showMessageDialog(jPanel, "提示消息", "标题",JOptionPane.WARNING_MESSAGE)
第二种:
int n = JOptionPane.showConfirmDialog(null, "你高兴吗?", "标题",JOptionPane.YES_NO_OPTION)//返回的是按钮的index i=0或者1
第三种:
Object[] obj2 ={ "足球", "篮球", "乒乓球" }
String s = (String) JOptionPane.showInputDialog(null,"请选择你的爱好:\n", "爱好", JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), obj2, "足球")