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

 

 

 

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