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上呢?
这样吧,我的代码里面有两个要添加到对话框上的按钮,你把代码全部抄下去给我改一下,实现我想要的功能,这样可能要容易理解一点。
展开