APP Prompted Dialog using JFrame without Frame

Joe

Thành viên VIP
21/1/13
3,007
1,335
113
Hi
Member gonyban asked:
Em mới học java giờ em không biết tạo đường viền cho Jframe và bo góc cho nó . Anh chị nào biết thì chỉ em có đc k ạ.
(Click HERE for more)

And so far I could understand that he wanted to pop up a JFrame as a dialog. This way is very useful to create a complex prompt that shows up over everything on the screen and waits till the request is fulfilled (regardless of active or inactive). Because JFrame is an extension of Frame the method setUndecorated(true) can be used to do the task. It's a bit tricky. The method should work with JFrame that contains some displayable components such as JTextField, JTextArea, JButton, etc. Example:
Java:
import javax.swing.*;
public class Example {

  public static void main(String[] a) throws Exception {
    if (a.length == 0) UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    else UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    JFrame jf = new JFrame("Transparent Window");
    // setUndecorated
    jf.setUndecorated(true);
    //
    JTextField txt1 = new JTextField("Name", 32);
    JTextField txt2 = new JTextField("Address", 32);
    txt2.addActionListener(e -> {
      String s = txt1.getText();
      System.out.println("Text_1:"+(s.length() > 0? s:"empty"));
      s = txt2.getText();
      System.out.println("Text_2:"+(s.length() > 0? s:"empty"));
      jf.dispose();
      System.exit(0);
    });
    jf.add("North", txt1);
    jf.add("South", txt2);
    jf.setVisible(true);
    jf.setLocation(400, 400);
    jf.pack();
    SwingUtilities.invokeLater(()->{
      txt1.requestFocus();
    });
  }
}
and the prompted Dialog has NO JFrame, but only TWO JTextfields:

exp1.png
 
Sửa lần cuối: