java窗口添加按钮事件_java如何为对话框添加按钮,以及如何为按钮添加事件?...

publicclassDel{JTextFieldpathField;JFramedelFrame;publicstaticvoidmain(String[]args){newDel();}publicDel(){frameDel();}publicvoidframeDel(){delFrame=newJFrame("文件删除器...

public class Del

{

JTextField pathField;

JFrame delFrame;

public static void main(String[] args)

{

new Del ();

}

public Del ()

{

frameDel();

}

public void frameDel()

{

delFrame=new JFrame("文件删除器");

delFrame.setBounds(150, 150, 500, 200);

delFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

delFrame.setVisible(true);

delFrame.setLayout(null);

Container con=delFrame.getContentPane();

con.setBounds(0, 0, 400, 200);

con.setBackground(new Color(222,111,111));

con.setLayout(null);

pathField=new JTextField();

pathField.setBounds(30, 30, 250, 30);

con.add(pathField);

JButton delButton=new JButton("删除");

delButton.setBounds(350, 30, 60, 30);

con.add(delButton);

delButton.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

delFile(pathField.getText());

}

});

delFrame.validate();

delFrame.repaint();

}

public void delFile(String path)

{

File goalFile=new File(path);

if(goalFile.exists())

{

goalFile.delete();

Dialog completeDialog=new Dialog(delFrame,"删除结果提示");

completeDialog.setSize(50, 20);

completeDialog.setVisible(true);

completeDialog.setBounds(250,250, 150, 90);

completeDialog.setModal(true);

JButton testButton=new JButton("继续删除");

completeDialog.add(testButton);

JButton testButton2=new JButton("退出");

completeDialog.add(testButton2);

pathField.setText("文件删除成功!");

}

else

{

pathField.setText("文件不存在!");

}

}

}

像我这个例子,如何为对话框添加两个按钮,又如何为按钮添加事件呢?我刚开始学Dialog,什么都不懂,连setSize()怎么用都不知道。所以请大家给我讲基础一点,不要太深奥,谢谢。

请讲详细点好吗?我现在就是不知道怎样给对话框加按钮和事件,请大家在我的代码基础上改一下给我讲,这样可能容易理解一点。 我把分提高了,只要大家能给我讲懂,想要多少分请留言。

con是delFrame上的面板,我现在是要在对话框上面加按钮。怎么能把对话框上的按钮添加上到con上呢?

这样吧,我的代码里面有两个要添加到对话框上的按钮,你把代码全部抄下去给我改一下,实现我想要的功能,这样可能要容易理解一点。

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值