本文目錄一覽:
java彈出文本框
告訴你為什麼樓上答案都這麼長,因為他們只懂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);
我不確定你說的「彈出一個文本框」是不是這個意思,如果不是的話補充一下問題我幫你改吧。
怎麼樣在java中彈出一個messagebox
在java中實現一個消息框, 常見的有兩種方式
第一種方法: JOptionPane 類. 創建1個彈窗.
例一:簡單的消息提示框
JOptionPane.showMessageDialog(null, “消息提示”);
2.例二:帶警告圖片的消息提示框
JOptionPane.showMessageDialog(null, “提示消息”, “標題”,JOptionPane.WARNING_MESSAGE);
第二種方法 彈窗JDialog 設置setModal(true); 也可以實現. 類似於寫JFrame
兩者區別在於:
JOptionPane 代碼簡短, 適應一些普通需求, 比如彈出消息提示, 彈出警告信息, 提示用戶輸入, 提示用戶選擇, 等簡單的場景
JDialog 代碼略長, 組件需要自行定義. 滿足一些複雜的需求. 比如多個輸入框 ,多個按鈕等
java如何操作彈出框
//不知道有沒有理解你的意思;類似一個死循環:下面是點一次出來一個窗口,記錄上次輸入的文字
//思路就是給個全局變數即可;坐標同理遞增防止覆蓋;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestText extends JFrame {
private JTextField jt1;
private JButton but;
static String str=””;
static int x=0,y=0,count=0;
private static final long serialVersionUID = 1L;
TestText(){
//如果第二次開始沒有輸入字就不創建
if(count=1(str.length()1)) {
return;
}
count++;
x=100;
y+=80;
this.setTitle(“第”+count+”個窗口”);
this.setBounds(x, y, 200, 80);
this.setLayout(new FlowLayout());
this.setResizable(false);
init();
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
private void init() {
jt1=new JTextField(10);
jt1.setText(str);
but=new JButton(“確定”);
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
str=jt1.getText();
new TestText();
}
});
this.add(jt1);
this.add(but);
}
public static void main(String[] args) {
new TestText();
}
}
如何用java彈出自己編輯的對話框
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class showMessage extends JFrame{
public showMessage(){
Container c =this.getContentPane();
JButton jb = new JButton(“點我出現message”);
c.add(jb,BorderLayout.NORTH);
setSize(100, 80);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, “沒錯,我就是神奇的Message!”);
}
});
}
public static void main(String[] args) {
new showMessage();
}
}
java怎麼設置彈出文本框?
其實很簡單的哦\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非常簡單的一個彈消息框的代碼
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/195560.html