getComponentByName (khung hình, tên)
NẾU bạn đang sử dụng NetBeans IDE này hay cách khác mà theo mặc định tạo ra các biến riêng (trường) để giữ tất cả các thành phần AWT/Swing của bạn, sau đó mã sau đây có thể làm việc cho bạn. Sử dụng như sau:
// get a button (or other component) by name
JButton button = Awt1.getComponentByName(someOtherFrame, "jButton1");
// do something useful with it (like toggle it's enabled state)
button.setEnabled(!button.isEnabled());
Dưới đây là đoạn code để làm cho trên có thể ...
import java.awt.Component;
import java.awt.Window;
import java.lang.reflect.Field;
/**
* additional utilities for working with AWT/Swing.
* this is a single method for demo purposes.
* recommended to be combined into a single class
* module with other similar methods,
* e.g. MySwingUtilities
*
* @author http://javajon.blogspot.com/2013/07/java-awtswing-getcomponentbynamewindow.html
*/
public class Awt1 {
/**
* attempts to retrieve a component from a JFrame or JDialog using the name
* of the private variable that NetBeans (or other IDE) created to refer to
* it in code.
* @param <T> Generics allow easier casting from the calling side.
* @param window JFrame or JDialog containing component
* @param name name of the private field variable, case sensitive
* @return null if no match, otherwise a component.
*/
@SuppressWarnings("unchecked")
static public <T extends Component> T getComponentByName(Window window, String name) {
// loop through all of the class fields on that form
for (Field field : window.getClass().getDeclaredFields()) {
try {
// let us look at private fields, please
field.setAccessible(true);
// compare the variable name to the name passed in
if (name.equals(field.getName())) {
// get a potential match (assuming correct <T>ype)
final Object potentialMatch = field.get(window);
// cast and return the component
return (T) potentialMatch;
}
} catch (SecurityException | IllegalArgumentException
| IllegalAccessException ex) {
// ignore exceptions
}
}
// no match found
return null;
}
}
Nó sử dụng phản ánh để xem xét thông qua các lĩnh vực lớp học để xem nó có thể tìm thấy một thành phần được gọi bởi một biến cùng tên.
LƯU Ý: Mã ở trên sử dụng Generics để truyền kết quả cho bất kỳ loại nào bạn mong đợi, vì vậy trong một số trường hợp, bạn có thể phải rõ ràng về việc nhập kiểu. Ví dụ, nếu myOverloadedMethod
chấp nhận cả JButton
và JTextField
, bạn có thể cần phải xác định một cách rõ ràng tình trạng quá tải bạn muốn gọi ...
myOverloadedMethod((JButton) Awt1.getComponentByName(someOtherFrame, "jButton1"));
Và nếu bạn không chắc chắn, bạn có thể nhận được một Component
và kiểm tra xem nó với instanceof
...
// get a component and make sure it's a JButton before using it
Component component = Awt1.getComponentByName(someOtherFrame, "jButton1");
if (component instanceof JButton) {
JButton button = (JButton) component;
// do more stuff here with button
}
Hy vọng điều này sẽ hữu ích!
Window.getWindows() và sau đó quét những gì bạn cần – bestsss
Tại sao bạn muốn làm điều đó?Bằng cách đó, bạn mất hai kiểm tra trình biên dịch tĩnh quan trọng: - Đầu tiên, trường tồn tại. - Thứ hai, đó là loại đúng. Ngoài ra, tìm kiếm động sẽ chậm hơn so với tham chiếu đến trường. – fortran
Ví dụ: do API đáng sợ, nơi bạn không có quyền truy cập trực tiếp vào một số thành phần mà bạn muốn sửa đổi. –