日期:2014-05-20  浏览次数:20850 次

【急】菜鸟请教!如何在java中实现点击按钮弹出一个新的自定义对话框
我想实现点击按钮弹出一个自己新定义的对话框,请教高手如何实现,最好有具体的代码,谢谢!
以下是我自己写的代码,不知道如何实现弹出对话框的功能,请高手在此基础上帮忙修改,谢谢!

import java.awt.*;
import java.awt.event.*;
public class work0 extends Frame
{
private Panel gridPanel1;
  public work0(String title)
  {
  super(title);
  setSize(600,400);
  setLocation(100,100);
  setgridLayoutPanel1();
  add(gridPanel1);
  addWindowListener(
  new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  setVisible(false);
  System.exit(0);}});
  }
public <InterfaceHeader> void setgridLayoutPanel1()
  {
gridPanel1=new Panel();
gridPanel1.setLayout(new GridLayout(4,8));
Button btn1=new Button("1");
Button btn2=new Button("2");
Button btn3=new Button("3");
Button btn4=new Button("4");
Button btn5=new Button("5");
gridPanel1.add(btn1);
gridPanel1.add(btn2);
gridPanel1.add(btn3);
gridPanel1.add(btn4);
gridPanel1.add(btn5);
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
  }
public static void main(String[] args) 
{
work0 w=new work0("弹出对话框");
w.show();
}

}

------解决方案--------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FrameTest extends JFrame{

public FrameTest()
{
setSize(220,220);
JButton button=new JButton("按钮");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
DialogPanel dialog=new DialogPanel();
}
});
add(button);
}


}
class DialogPanel extends JPanel
{
JDialog dialog=new JDialog();
public DialogPanel()
{
//加入其它你想加的东西
dialog.add(this);
dialog.setSize(300,300);
dialog.setLocation(300,300);
dialog.setVisible(true);
}
}

public class Run1 {
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable(){
public void run()
{
FrameTest test=new FrameTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
});

}
}
试一下吧 把你需要的东西加进去应该就可以了