当前位置: 技术问答>java相关
对话框的问题(幼稚的)
来源: 互联网 发布时间:2017-03-26
本文导语: 我想当用户按下一个按钮时就弹出一个对话框,如果用户点击了“确认” 则。。。否则。。。 | 用JAVASCRIPT的ALERT函数 | 我接着 djinzaghi(java) 来说: JButton button=new JButton("OK");...
我想当用户按下一个按钮时就弹出一个对话框,如果用户点击了“确认” 则。。。否则。。。
|
用JAVASCRIPT的ALERT函数
|
我接着 djinzaghi(java) 来说:
JButton button=new JButton("OK");
button.addActionListener(new ActionListener()
{
public void actionPerform(ActionEvent v)
{
//这就可以显示罗!
XXXXDialog.show();//XXXXDialog这是我定义的对话框.
..........}
});
至于,你在对话框中按确定,或.......
可以在对话框中定义一个监听器,来接收这个消息!
例如:
class LinkDialog extends JDialog implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()=="确定")
{ }
if(e.getSource()=="XXXX")
{ }
}
}
JButton button=new JButton("OK");
button.addActionListener(new ActionListener()
{
public void actionPerform(ActionEvent v)
{
//这就可以显示罗!
XXXXDialog.show();//XXXXDialog这是我定义的对话框.
..........}
});
至于,你在对话框中按确定,或.......
可以在对话框中定义一个监听器,来接收这个消息!
例如:
class LinkDialog extends JDialog implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()=="确定")
{ }
if(e.getSource()=="XXXX")
{ }
}
}
|
看一下这个类,JOptionPane
|
看一下这个类,JOptionPane
|
import java.swing.*;
JOptionPane.showInputDialog(this, "title", "message", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showInputDialog(this, "title", "message", JOptionPane.INFORMATION_MESSAGE);
|
JButton button=new JButton("OK");
button.addActionListener(new ActionListener()
{
public void actionPerform(ActionEvent v)
{..........}
});
button.addActionListener(new ActionListener()
{
public void actionPerform(ActionEvent v)
{..........}
});
|
何必做的这么麻烦,jbuilder就不能用javascript吗?
还是看看javascript吧,很简单
alert("确定吗?")
还是看看javascript吧,很简单
alert("确定吗?")
|
不明白你是什么意思,不过如果就是要弹出对话框可以用
JOptionPane
JOptionPane
|
用JOptionPane的功能来实现!一个例子:要求用户填写资料,完成后显示用户所有资料(在jdk1.4中编译通过!)
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Info extends JFrame {
private JLabel titleLabel = new JLabel("Title: ",
SwingConstants.RIGHT);
private JTextField title;
private JLabel addressLabel = new JLabel("Address: ",
SwingConstants.RIGHT);
private JTextField address;
private JLabel typeLabel = new JLabel("Type: ",
SwingConstants.RIGHT);
private JTextField type;
public Info() {
super("Site Information");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Site name
String response1 = JOptionPane.showInputDialog(null,
"Enter the site title:");
title = new JTextField(response1, 20);
// Site address
String response2 = JOptionPane.showInputDialog(null,
"Enter the site address:");
address = new JTextField(response2, 20);
// Site type
String[] choices = { "Personal", "Commercial", "Unknown" };
int response3 = JOptionPane.showOptionDialog(null,
"What type of site is it?",
"Site Type",
0,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
type = new JTextField(choices[response3], 20);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2));
pane.add(titleLabel);
pane.add(title);
pane.add(addressLabel);
pane.add(address);
pane.add(typeLabel);
pane.add(type);
setContentPane(pane);
}
public static void main(String[] arguments) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use the system "
+ "look and feel: " + e);
}
JFrame frame = new Info();
frame.pack();
frame.setVisible(true);
}
}
你要功能只是其中一部分!很容易实现!
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Info extends JFrame {
private JLabel titleLabel = new JLabel("Title: ",
SwingConstants.RIGHT);
private JTextField title;
private JLabel addressLabel = new JLabel("Address: ",
SwingConstants.RIGHT);
private JTextField address;
private JLabel typeLabel = new JLabel("Type: ",
SwingConstants.RIGHT);
private JTextField type;
public Info() {
super("Site Information");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Site name
String response1 = JOptionPane.showInputDialog(null,
"Enter the site title:");
title = new JTextField(response1, 20);
// Site address
String response2 = JOptionPane.showInputDialog(null,
"Enter the site address:");
address = new JTextField(response2, 20);
// Site type
String[] choices = { "Personal", "Commercial", "Unknown" };
int response3 = JOptionPane.showOptionDialog(null,
"What type of site is it?",
"Site Type",
0,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
type = new JTextField(choices[response3], 20);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2));
pane.add(titleLabel);
pane.add(title);
pane.add(addressLabel);
pane.add(address);
pane.add(typeLabel);
pane.add(type);
setContentPane(pane);
}
public static void main(String[] arguments) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use the system "
+ "look and feel: " + e);
}
JFrame frame = new Info();
frame.pack();
frame.setVisible(true);
}
}
你要功能只是其中一部分!很容易实现!
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。