「java关闭事件」java事务如何关闭
今天给各位分享java关闭事件的知识,其中也会对java事务如何关闭进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
JAVA关于窗体JFrame的关闭事件
//设置关闭后直接退出
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
package Login;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame {
private JLabel jLabel_School_logo;
private JLabel jLabel_user_name;
private JLabel jLabel_passwd;
private JButton jButton_reg;
private JButton jButton_login;
private JPasswordField jPasswordField;
private JTextField jTextField_user_name;
private JLabel jLabel_title;
private int student_statue=0;
private JComboBox jComboBox1;
private int manager_statue=0;
/**
*
*/
/**
*
*/
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setTitle("教室管理系统登录");
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NewJFrame() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jLabel_School_logo = new JLabel();
getContentPane().add(jLabel_School_logo);
jLabel_School_logo.setBounds(0, 0, 86, 84);
ImageIcon logo=new ImageIcon("img/log.jpg");
jLabel_School_logo.setIcon(logo);
}
{
jLabel_title = new JLabel();
getContentPane().add(jLabel_title);
jLabel_title.setBounds(86, 0, 447, 84);
ImageIcon title=new ImageIcon("img/title.jpg");
jLabel_title.setIcon(title);
}
{
jLabel_user_name = new JLabel();
getContentPane().add(jLabel_user_name);
jLabel_user_name.setText("\u5b66\u53f7\uff1a");
jLabel_user_name.setBounds(155, 131, 51, 31);
jLabel_user_name.setFont(new java.awt.Font("叶根友毛笔行书2.0版",0,12));
}
{
jTextField_user_name = new JTextField();
getContentPane().add(jTextField_user_name);
jTextField_user_name.setBounds(199, 135, 151, 23);
}
{
jLabel_passwd = new JLabel();
getContentPane().add(jLabel_passwd);
jLabel_passwd.setText("\u5bc6\u7801\uff1a");
jLabel_passwd.setBounds(152, 185, 51, 31);
jLabel_passwd.setFont(new java.awt.Font("叶根友毛笔行书2.0版",0,12));
}
{
jPasswordField = new JPasswordField();
getContentPane().add(jPasswordField);
jPasswordField.setBounds(199, 189, 151, 23);
}
{
jButton_login = new JButton();
getContentPane().add(jButton_login);
jButton_login.setText("\u767b\u5f55");
jButton_login.setBounds(199, 235, 68, 23);
jButton_login.setFont(new java.awt.Font("叶根友毛笔行书2.0版",0,12));
}
{
jButton_reg = new JButton();
getContentPane().add(jButton_reg);
jButton_reg.setText("\u6ce8\u518c");
jButton_reg.setBounds(282, 235, 68, 23);
jButton_reg.setFont(new java.awt.Font("叶根友毛笔行书2.0版",0,12));
jButton_reg.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
jButton_regMouseClicked(evt);
}
});
}
{
ComboBoxModel jComboBox1Model =
new DefaultComboBoxModel(
new String[] { "学生/老师登录", "管理员登录" });
jComboBox1 = new JComboBox();
getContentPane().add(jComboBox1);
jComboBox1.setModel(jComboBox1Model);
jComboBox1.setBounds(199, 90, 152, 23);
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
}
pack();
this.setSize(549, 334);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private void jComboBox1ActionPerformed(ActionEvent evt) {
if(jComboBox1.getSelectedIndex()==0)
{
jLabel_user_name.setText("学号:");
}
else
{
jLabel_user_name.setText("帐号:");
}
}
private void jButton_regMouseClicked(MouseEvent evt) {
if(jComboBox1.getSelectedIndex()==1)
{
JOptionPane.showMessageDialog(NewJFrame.this, "管理员帐号不提供注册!", "", JOptionPane.DEFAULT_OPTION);
}
else
{
reg register=new reg();
register.setTitle("注册");
register.setLocationRelativeTo(this);
register.setVisible(true);
}
}
}
//设置setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);在另一个方法,你怎么用到你这个F呢?
运行java代码怎样操作显示windowclosed--窗体关闭事件,求解答!!!
效果图
你想要触发的是窗口关闭事件windowClosed, 那么你需要调用dispose方法,对窗口进行销毁.
修改代码如下
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing--窗体正在关闭事件");
//f.setVisible(false);//隐藏窗口
f.dispose();//销毁窗口--这行代码会触发windowClosed事件
}
还有其他一些细节的修改,参考下面的代码
import java.awt.*;
import java.awt.event.*;
//类名不要写一个小写字母,推荐写一个完整的名词来作为类名
public class EventDemo {
public static void main(String[] args) {
Frame f = new Frame("hello");
f.setSize(300, 300);
f.setLocation(300, 200);
//f.setVisible(true);//不要先显示窗口, 先显示了窗口,就看不到"窗口打开事件"了
f.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened--窗体打开事件");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified--窗体图标化事件");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("windowDeiconifie--窗体取消图标化事件");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated--窗体停用事件");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing--窗体正在关闭事件");
//f.setVisible(false);//隐藏窗口
f.dispose();//销毁窗口
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed--窗体关闭事件");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated--窗体激活事件");
}
});
f.setVisible(true);//窗口显示
}
}
在java中我在一个窗体里设置了关闭窗体事件,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class jdemo {
public static int showDialog(Component c) {
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog (null, "Are you sure you want to quit?","Tips",dialogButton);
return dialogResult;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame("Closeing Windows Form Test");
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
if(showDialog(f)==JOptionPane.NO_OPTION) return; // 如果按了取消,则不退出
System.exit(0);// 否则退出
}
} );
f.setSize(300,200);
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
关于java关闭事件和java事务如何关闭的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。