① java編程:我想實現一個功能:點擊下來菜單一個按鈕時,彈出一個對話框
一個簡單的例子。。。。importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextField;publicclassAextendsJFrame{privateAa;publicJLabellbl1;publicJLabellbl2;publicA(){lbl1=newJLabel("111111111");lbl2=newJLabel("222222222");JButtonbtnTest=newJButton("測試按鈕");btnTest.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){newB(a).setVisible(true);}});setLayout(newGridLayout(1,3));add(lbl1);add(lbl2);add(btnTest);setSize(300,70);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);a=this;}publicstaticvoidmain(String[]args){newA().setVisible(true);}}classBextendsJFrame{privateAa;privateJTextFieldtxt1;privateJTextFieldtxt2;publicB(Aaa){a=aa;txt1=newJTextField();txt2=newJTextField();JButtonbtnTest=newJButton("測試按鈕");btnTest.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){a.lbl1.setText(txt1.getText());a.lbl2.setText(txt2.getText());}});setLayout(newGridLayout(3,1));add(txt1);add(txt2);add(btnTest);setBounds(100,100,300,100);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}}===============有問題直接在這提問吧,說的清楚點就行了。我不要懸賞分
② java如何在線程中彈出對話框
1.顯示一個錯誤對話框,該對話框顯示的 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]);
③ JAVA怎麼彈出對話框
需要引入下面的名稱空間javax.swing.JOptionPane JOptionPane
public JOptionPane(Object message,
int messageType,
int optionType,
Icon icon,
Object[] options,
Object initialValue)在指定最初選擇的選項的前提下,創建一個顯示消息的 JOptionPane 的實例,使其具有指定的消息類型、圖標和選項。
參數:
message – 要顯示的 Object
messageType – 要顯示的消息類型:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE
optionType – 要在窗格中顯示的選項:DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION、OK_CANCEL_OPTION
icon – 要顯示的圖標圖像
options – 用戶可以選擇的選項
initialValue – 最初選擇的選項;如果為 null,則不做最初選擇;只有在使用 options 時才有意義
④ 用java編寫一個程序,程序運行時彈出一個輸入對話框,用戶使用該對話
packagecn.fu;
importjava.awt.BorderLayout;
importjava.awt.EventQueue;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.border.EmptyBorder;
importjavax.swing.JOptionPane;
importjavax.swing.JTextField;
importjavax.swing.JLabel;
importjavax.swing.JButton;
importjava.awt.event.ActionListener;
importjava.awt.event.ActionEvent;
importjava.awt.Window.Type;
publicclassLoginextendsJFrame{
privateJPanelcontentPane;
privateJTextFieldtextField;
/**
*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
Loginframe=newLogin();
frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*Createtheframe.
*/
publicLogin(){
setTitle("工具");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,450,300);
contentPane=newJPanel();
contentPane.setToolTipText("");
contentPane.setBorder(newEmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField=newJTextField();
textField.setBounds(121,86,194,21);
contentPane.add(textField);
textField.setColumns(10);
JLabellblNewLabel=newJLabel("請輸入10位數以內的字元串");
lblNewLabel.setBounds(145,59,194,15);
contentPane.add(lblNewLabel);
JButtonbtnNewButton=newJButton("確定");
btnNewButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
Stringca=textField.getText();
intn=ca.length();
if(n>10){
JOptionPane.showMessageDialog(null,"對不起,您輸入的字元串長度超過10",
"錯誤提示",JOptionPane.ERROR_MESSAGE);
}elseif(n>=0||n<=10){
JOptionPane.showMessageDialog(null,"字元串長度為"+n,"提示",
JOptionPane.PLAIN_MESSAGE);
}
}
});
btnNewButton.setBounds(172,130,93,23);
contentPane.add(btnNewButton);
}
}
⑤ 在JAVA類中彈出js對話框
1:java程序
package org.elb.util;
public class JavaScriptDemo_1 {
public static String getJS(){
return "<script language=\"JavaScript\">alert(\"彈出框\")</script>";
}
}
2:JSP頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<jsp:useBean id="a" class="org.elb.util.JavaScriptDemo_1"></jsp:useBean>
<%=a.getJS() %>
</body>
</html>
想彈出什麼來,就寫什麼就行了,明白了嗎?
⑥ java怎麼通過點擊菜單彈出對話框
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class testMenuItem {
public static void main(String[] args) {
int x=200,y=200,width=400,height=400;
final JFrame j=new JFrame();
j.setLayout(new GridLayout(10, 1));
JMenuBar jb=new JMenuBar();
j.add(jb);
JMenu jm=new JMenu("菜單");
jb.add(jm);
JMenuItem jm1=new JMenuItem("選項1");
JMenuItem jm2=new JMenuItem("選項2");
JMenuItem jm3=new JMenuItem("選項3");
ActionListener al =new ActionListener(){
public void actionPerformed(ActionEvent e) {
String buttonString=e.getActionCommand();
if(buttonString.equals("選項1"))
JOptionPane.showMessageDialog(j, "您選擇了 選項1");
else if(buttonString.equals("選項2"))
JOptionPane.showMessageDialog(j, "您選擇了 選項2");
else if(buttonString.equals("選項3"))
JOptionPane.showMessageDialog(j, "您選擇了 選項3");
}
};
jm1.addActionListener(al);
jm2.addActionListener(al);
jm3.addActionListener(al);
jm.add(jm1);
jm.add(jm2);
jm.add(jm3);
j.setBounds(x, y, width, height);
j.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
}
}
⑦ java中關於頁面彈出對話框問題
if(JOptionPane.showConfirmDialog(null, "確定不通過此次家長會申請嗎?", "提示", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_CANCEL_OPTION)==JOptionPane.YES_OPTION)或者下面
if(JOptionPane.showConfirmDialog(null, "確定不通過此次家長會申請嗎?", "提示", JOptionPane.OK_CANCEL_OPTION)==0){
request.getRequestDispatcher("/AppAudit.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/a.jsp").forward(request, response);
}
方法簽名:
showConfirmDialog
public static int showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType)
throws HeadlessException
調用一個由 optionType 參數確定其中選項數的對話框,messageType 參數確定要顯示的圖標。messageType 參數主要用於提供來自外觀的默認圖標。
參數:
parentComponent - 確定在其中顯示對話框的 Frame;如果為 null 或者 parentComponent 不具有 Frame,則使用默認的 Frame。
message - 要顯示的 Object
title - 對話框的標題字元串
optionType - 指定可用於對話框的選項的整數:YES_NO_OPTION 或 YES_NO_CANCEL_OPTION
messageType - 指定此消息種類的整數;主要用於確定來自可插入外觀的圖標:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE
返回:
指示用戶所選選項的整數
源碼如下:
public class JOptionPane extends JComponent implements Accessible
{
public static final int YES_OPTION = 0;
/** Return value from class method if NO is chosen. */
public static final int NO_OPTION = 1;
/** Return value from class method if CANCEL is chosen. */
public static final int CANCEL_OPTION = 2;
/** Return value form class method if OK is chosen. */
public static final int OK_OPTION = 0;
/** Return value from class method if user closes window without selecting
* anything, more than likely this should be treated as either a
* <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
public static final int CLOSED_OPTION = -1;
}