java swing 事件响应问题收集

         在swing的窗口里面,即jframe里面,进行关闭窗口动作,如果事先不在代码中设置好,则默认是把窗口设成setVisable(false),程序还是依然运行着,继续占用内存,所以如果不小心,越调试则会发现内存占用率越高,机子越来越卡。

            一般的,如果没有特殊要求,只需要这样让该窗口对象调用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);则当我们关闭窗口时,程序结束运行  。                                                     
 

但是,如果有特殊要求,如关闭之前要调用一些方法,则可以这样:让该窗口对象调用addWindowListener(new WindowAdapter());方法,并重载方法内容,就像下面的代码那样。

 

addWindowListener(new WindowAdapter() {
 
 
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//加入动作
//
 }
 
}); 

 

 

how to trigger the windowclosing event manually

I need to trigger windoclosing event for JFrame manullay.

My purpose is to close a frame by clicking a button,when button is pressed windowclosing event of that frame must be triggerd
 

Just for the record, the following code will actually trigger a "real" window closing event programmatically:

        JFrame frame = ...
        Toolkit tk = Toolkit.getDefaultToolkit();
        EventQueue evtQ = tk.getSystemEventQueue();
        evtQ.postEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

I wouldn't use this myself though.

reflink: https://community.oracle.com/thread/1366228

 

设置JProgressBar

https://www.geeksforgeeks.org/java-swing-jprogressbar/

 

Allowing the “Enter” key to press the submit button, as opposed to only using MouseClick

In the ActionListener Class you can simply add

public void actionPerformed(ActionEvent event) {
    if (event.getSource()==textField){
        textButton.doClick();
    }
    else if (event.getSource()==textButton) {
        //do something
    }
}

 

 

How To limit the number of characters in JTextField?

extfield.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyTyped(java.awt.event.KeyEvent evt) {
        if(textfield.getText().length()>=5&&!(evt.getKeyChar()==KeyEvent.VK_DELETE||evt.getKeyChar()==KeyEvent.VK_BACK_SPACE)) {
            getToolkit().beep();
            evt.consume();
         }
     }
});

reflink: https://stackoverflow.com/questions/3519151/how-to-limit-the-number-of-characters-in-jtextfield

 

Java Swing JTextField - Only Numbers [duplicate]

zipField = new JFormattedTextField(
                    createFormatter("#####"));
...
protected MaskFormatter createFormatter(String s) {
    MaskFormatter formatter = null;
    try {
        formatter = new MaskFormatter(s);
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
        System.exit(-1);
    }
    return formatter;
}

reflink: https://www.math.uni-hamburg.de/doc/java/tutorial/uiswing/components/formattedtextfield.html

https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#maskformatter

https://stackoverflow.com/questions/20768653/java-swing-jtextfield-only-numbers

https://stackoverflow.com/questions/8203378/how-to-use-jformattedtextfield-allowing-only-letters-and-numbers

https://stackoverflow.com/questions/27180459/limit-characters-in-a-jformattedtextfield

https://coderanch.com/t/434146/java/JTextField-set-size-limit

https://coderanch.com/t/570167/java/limit-numbers-entered-JFormattedTextField

 

How to turn off word wrap in JEditorPane?

why don't you use a jTextField?

it's only one line.

https://stackoverflow.com/questions/8960732/how-to-turn-off-word-wrap-in-jeditorpane

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章