草草的。 小弟在线等答案~~

zhouxinyuan 2008-01-03 02:48:48
我现在在做一个下拉菜单~

菜单 中有几个选项,

然后我想在点击其中一个选项的时候能弹出另外的一个对话框,

比如:点“修改密码” 弹出一个对话框里有“请输入旧密码,请输入新密码,确认密码”

那位高手能帮下忙,

我弄明白就结账,说到做到.

还有我现在在用NetBeans IDE 6.0做~
...全文
256 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
alugey 2008-01-04
  • 打赏
  • 举报
回复
可能是你弹出来的对话框中,有这个函数:

public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}

所以你一关对话框,整个程序就退出了.
zhouxinyuan 2008-01-03
  • 打赏
  • 举报
回复
我现在又遇到问题了~


弹是弹出来了, 但是一关弹出的对话框,

原来的也关了,

怎么才能让原来 的开着不关啊???????
zhouxinyuan 2008-01-03
  • 打赏
  • 举报
回复
BB的。 自己找到了~


就两句话

private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
TestHelp th = new TestHelp();
th.setVisible(true);
}

就JB完事了~~

我就日了~
ttlyfast 2008-01-03
  • 打赏
  • 举报
回复
哈 草草
zhouxinyuan 2008-01-03
  • 打赏
  • 举报
回复
我草。 大哥。 你强
yun81811091 2008-01-03
  • 打赏
  • 举报
回复
感谢楼上的回复 十分感谢
rock8108 2008-01-03
  • 打赏
  • 举报
回复

这是一段实现代码,你看看,不明白再问.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;

public class PlafTest
{
public static void main(String[] args)
{
ImageViewerFrame frame = new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
A frame that has a menu for loading an image and a display
area for the loaded image.
*/
class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
setTitle("FileChooserTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// set up menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu menu = new JMenu("File");
menuBar.add(menu);

JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new FileOpenListener());

JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});

// use a label to display the images
label = new JLabel();
add(label);

// set up file chooser
chooser = new JFileChooser();

// accept all image files ending with .jpg, .jpeg, .gif
final ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension("jpg");
filter.addExtension("jpeg");
filter.addExtension("gif");
filter.setDescription("Image files");
chooser.setFileFilter(filter);

chooser.setAccessory(new ImagePreviewer(chooser));

chooser.setFileView(new FileIconView(filter, new ImageIcon("palette.gif")));
}

/**
This is the listener for the File->Open menu item.
*/
private class FileOpenListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
chooser.setCurrentDirectory(new File("."));

// show file chooser dialog
int result = chooser.showOpenDialog(ImageViewerFrame.this);

// if image file accepted, set it as icon of the label
if(result == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
}

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 400;

private JLabel label;
private JFileChooser chooser;
}

/**
This file filter matches all files with a given set of
extensions.
*/
class ExtensionFileFilter extends FileFilter
{
/**
Adds an extension that this file filter recognizes.
@param extension a file extension (such as ".txt" or "txt")
*/
public void addExtension(String extension)
{
if (!extension.startsWith("."))
extension = "." + extension;
extensions.add(extension.toLowerCase());
}

/**
Sets a description for the file set that this file filter
recognizes.
@param aDescription a description for the file set
*/
public void setDescription(String aDescription)
{
description = aDescription;
}

/**
Returns a description for the file set that this file
filter recognizes.
@return a description for the file set
*/
public String getDescription()
{
return description;
}

public boolean accept(File f)
{
if (f.isDirectory()) return true;
String name = f.getName().toLowerCase();

// check if the file name ends with any of the extensions
for (String extension : extensions)
if (name.endsWith(extension))
return true;
return false;
}

private String description = "";
private ArrayList<String> extensions = new ArrayList<String>();
}

/**
A file view that displays an icon for all files that match
a file filter.
*/
class FileIconView extends FileView
{
/**
Constructs a FileIconView.
@param aFilter a file filter--all files that this filter
accepts will be shown with the icon.
@param anIcon--the icon shown with all accepted files.
*/
public FileIconView(FileFilter aFilter, Icon anIcon)
{
filter = aFilter;
icon = anIcon;
}

public Icon getIcon(File f)
{
if (!f.isDirectory() && filter.accept(f))
return icon;
else return null;
}

private FileFilter filter;
private Icon icon;
}

/**
A file chooser accessory that previews images.
*/
class ImagePreviewer extends JLabel
{
/**
Constructs an ImagePreviewer.
@param chooser the file chooser whose property changes
trigger an image change in this previewer
*/
public ImagePreviewer(JFileChooser chooser)
{
setPreferredSize(new Dimension(100, 100));
setBorder(BorderFactory.createEtchedBorder());

chooser.addPropertyChangeListener(new
PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent
event)
{
if (event.getPropertyName() ==
JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
{
// the user has selected a new file
File f = (File) event.getNewValue();
if (f == null) { setIcon(null); return; }

// read the image into an icon
ImageIcon icon = new ImageIcon(f.getPath());

// if the icon is too large to fit, scale it
if(icon.getIconWidth() > getWidth())
icon = new ImageIcon(icon.getImage().getScaledInstance(
getWidth(), -1, Image.SCALE_DEFAULT));

setIcon(icon);
}
}
});
}
}
yun81811091 2008-01-03
  • 打赏
  • 举报
回复
我本来的想法是 在下拉的菜单选项里定义一个鼠标的单击事件 因为NetBeans能自动实现时间见听的代码 所以我就直接NEW了一个 窗体的对象 然后将该对象的VISIBLE属性定义为TRUE 可是上述方法在按钮的单击事件中是很好用的 在菜单的下拉选项里不能用 谁能解释下 谢谢各位
mcluvin 2008-01-03
  • 打赏
  • 举报
回复
2楼亮点
zhouxinyuan 2008-01-03
  • 打赏
  • 举报
回复
现在是都说是用监听器 可我就是弄不上去。

崩溃中~
yun81811091 2008-01-03
  • 打赏
  • 举报
回复
您给的代码 大概的想法我能看懂 但是刚我 尝试写了一下 没写懂。。呵呵 我用NetBeans 写完之后 反而在自动生成的代码部分提示了异常 郁闷ING
lihaifeng0412 2008-01-03
  • 打赏
  • 举报
回复
弄个监听器不就行了
alugey 2008-01-03
  • 打赏
  • 举报
回复
不是.
是我以前用记事本写的.
但原理一样.
yun81811091 2008-01-03
  • 打赏
  • 举报
回复
楼上回复的是用NetBeans IDE 6.0体现的吗?
还是用别的开发工具? 我是新手请说下 谢谢您
alugey 2008-01-03
  • 打赏
  • 举报
回复
用Menu对象封装菜单对象如:
Menu menu1=new Menu("文件");
Menu menu2=new Menu("编辑");


定义菜单成员:
MenuItem mitem11=new MenuItem("新建");
MenuItem mitem12=new MenuItem("打开");
MenuItem mitem21=new MenuItem("复制");
MenuItem mitem22=new MenuItem("粘贴");

为菜单增加事件监听器:
menu1.add(mitem11);
mitem11.addActionListener(new MenuHandler());
menu1.add(mitem12);
mitem12.addActionListener(new MenuHandler());
...

事件监听器类似:
class MenuHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==mitem11){
//mitem11被点击时的处理代码
}
if(e.getSource()==mitem12){
//mitem12被点击时的处理代码
}
...
}
}



yun81811091 2008-01-03
  • 打赏
  • 举报
回复
刚才回复问题的同学 你给提供的方法我回去看了 在方法里使用您的JOptionPane.showMessageDialog(...);之前我也看过类似的方法 但是不知道确定怎么使用望指教~~
zhouxinyuan 2008-01-03
  • 打赏
  • 举报
回复
我晕死。
四楼的,能说明白点么?
wunan320 2008-01-03
  • 打赏
  • 举报
回复
哈哈 2楼是亮点.
rock8108 2008-01-03
  • 打赏
  • 举报
回复
如上所说,增加事件侦听器,当产生事件时new个对话框.
k_code 2008-01-03
  • 打赏
  • 举报
回复
你需要进行事件捕捉,当捕捉到你的这个点击动作后,就可以在相应的方法里利用JOptionPane.showMessageDialog(...);来弹出一个对话框,当然,这个框你可以自己设计的
加载更多回复(3)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧