日期:2014-05-20 浏览次数:20876 次
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DialogDemo extends JFrame{ private JButton button=new JButton("打开"); private JDialog dialog=new JDialog(this); private TextField textField=new TextField(10); JButton btn=new JButton("close");; public DialogDemo(String title){ super(title); textField.setEditable(true); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ dialog.add(btn); dialog.pack(); dialog.setVisible(true); // textField.setText(dialog.getText()); } }); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ dialog.dispose(); //这一行代码就可以关闭对话框 } }); Container contentPane=this.getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(button); contentPane.add(textField); setSize(500,300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new DialogDemo("DialogDemo"); } }
------解决方案--------------------
需要引入jquery框架:
<script type="text/javascript">
$(function() {
$('#editDialog').dialog({
autoOpen:false,
width:'80%',
modal : true,
buttons:{
"保存":function(){},
//点击取消键dialog就会关闭
"取消":function(){$('#editDialog').dialog("close")}
}
});
});
</script>
<body>
<div id="editDialog" title="修改界面" style="display:none"></div>
</body>
------解决方案--------------------
------解决方案--------------------
import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class Test extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private JButton btntest; public Test(){ super("按钮时间测试"); btntest=new JButton("Click Me"); btntest.addActionListener(this); getContentPane().add(btntest); this.setSize(88, 88); Dimension winSize=Toolkit.getDefaultToolkit().getScreenSize(); setLocation((winSize.width-this.getWidth())/2,(winSize.height-this.getHeight())/2); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource().equals(btntest)){ JOptionPane.showMessageDialog(null, "OK!"); } } public static void main(String[] args) { new Test().setVisible(true); } }