Hi, Khách!
![]() | ![]() ![]() ![]() ![]() |
Bài 27 - JOptionPane cơ bản
Đây có thể nói là công cụ Dialog mạnh nhất
Trước tiên hãy xem qua ví dụ sau
import javax.swing.*;
class Core
{
public static void main(String args[)
{
JFrame f = new JFrame("My frame");
JOptionPane.showMessageDialog(f,"Hien thi cau thong bao","Hien thi tieu de",JOptionPane.ERROR_MESSAGE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Copy code
class Core
{
public static void main(String args[)
{
JFrame f = new JFrame("My frame");
JOptionPane.showMessageDialog(f,"Hien thi cau thong bao","Hien thi tieu de",JOptionPane.ERROR_MESSAGE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Copy code
JOptionPane bao gồm các thành phần chính sau đây: Title, Icon, Message, InputValue và OptionButtons. Không cần bao gồm đủ tất cả
* Khởi tạo của JOptionPane
JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[ options)
Bạn có thể thíêu bất cứ thành phần nào thậm chí có thể thiếu hết
Object message: câu thông báo hiển thị trên JOptionpane
int messageType: bao gồm ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, và PLAIN_MESSAGE
int optionType: bao gồm DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION
Icon icon: hình icon của JOptionPane
Ví dụ:
import javax.swing.*;
class Core
{
public static void main(String args[)
{
JFrame f = new JFrame("My frame");
f.add(new JOptionPane("Hien thi cau thong bao", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION));
f.setSize(250,150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Copy code
* Các phương thức (hay sử dụng hơn)class Core
{
public static void main(String args[)
{
JFrame f = new JFrame("My frame");
f.add(new JOptionPane("Hien thi cau thong bao", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION));
f.setSize(250,150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Copy code
void showMessageDialog(Component parentComponent, Object message, String title, int messageType)
String showInputDialog(Component parentComponent, Object message, String title, int messageType)
int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType) Yes trả về 0 và No trả về 1.
Nguồn: sinhvienit

Trực Tuyến:
Khách: 1