本文目錄一覽:
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怎麼設置彈出文本框?
其實很簡單的哦\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非常簡單的一個彈消息框的代碼
java中實現彈出不同的警告和提示框
顯示一個錯誤對話框,該對話框顯示的 message 為 ‘alert’:
JOptionPane.showMessageDialog(null, “alert”, “alert”, JOptionPane.ERROR_MESSAGE);
2.顯示一個內部信息對話框,其 message 為 ‘information’:
JOptionPane.showInternalMessageDialog(frame, “information”,”information”, JOptionPane.INFORMATION_MESSAGE);
3.顯示一個信息面板,其 options 為 “yes/no”,message 為 ‘choose one’:
JOptionPane.showConfirmDialog(null, “choose one”, “choose one”, JOptionPane.YES_NO_OPTION);
4.顯示一個內部信息對話框,其 options 為 “yes/no/cancel”,message 為 ‘please choose one’,並具有 title 信息:
JOptionPane.showInternalConfirmDialog(frame,
“please choose one”, “information”,
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
5.顯示一個警告對話框,其 options 為 OK、CANCEL,title 為 ‘Warning’,message 為 ‘Click OK to continue’:
Object[] options = { “OK”, “CANCEL” };
JOptionPane.showOptionDialog(null, “Click OK to continue”, “Warning”,
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
6.顯示一個要求用戶鍵入 String 的對話框:
String inputValue = JOptionPane.showInputDialog(“Please input a value”);
7.顯示一個要求用戶選擇 String 的對話框:
Object[] possibleValues = { “First”, “Second”, “Third” };
Object selectedValue = JOptionPane.showInputDialog(null, “Choose one”, “Input”,
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
以上摘抄自CSDN, 純複製~ 不過挺符合你的要求的
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();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/151317.html