java怎么设置弹出文本框?

Python013

java怎么设置弹出文本框?,第1张

其实很简单的哦\x0d\x0a\x0d\x0aimport javax.swing.JOptionPane\x0d\x0a\x0d\x0apublic class Test {\x0d\x0a\x0d\x0apublic static void main(String[] args) {\x0d\x0aString s=JOptionPane.showInputDialog("请输入:")\x0d\x0a}\x0d\x0a}\x0d\x0a非常简单的一个弹消息框的代码

告诉你为什么楼上答案都这么长,因为他们只懂copy别人的。。

我专门写了个给你:

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, "足球")