JDialog能最大化吗?JWindow或JFrame能模态化吗? 现在的问题是JDialog弹出的时候,总是大小不够,用户需要手动去拉伸对话框来改变大小能否给对话框加上一个最大化的按钮的呢?我现有的知识认为是不能的??那么就只能用JWindow或JFrame,但是用他们我又不知如何实现模态对话框的效果,即始终居于程序最顶部哪位大虾能指点一下呢?要么是让对话框又最大化按钮要么是让JWindow或JFrame能够实现模态对话框的效果 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主说的好象都不可以不知道要在什么时候最大化JDialog如果只要求大小是全屏的话可以设置size JFrame模态两种方法:1) JNI2) frame.addWindowFocusListener(new WindowFocusListener() { public void windowGainedFocus(WindowEvent e) {} public void windowLostFocus(WindowEvent e) { frame.toFront(); } } );第二种方法也有些问题,从其他程序切换回来的时候,弹出的JFrame不在最前面。 frame.toFront();这个方法我试过了不是想要的效果不知道为什么... 如果自己设置的对话框大小不合适,可以在生成了对话框后,使用如下的方法,pack(),自动根据控件的大小来设置对话框的大小。 手头正好有实现JFrame的模态化的代码: import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;public class ModalFrameUtil { public ModalFrameUtil() { // } static class EventPump implements InvocationHandler { Frame frame; public EventPump(Frame frame) { this.frame = frame; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return frame.isShowing() ? Boolean.TRUE : Boolean.FALSE; } // when the reflection calls in this method has to be // replaced once Sun provides a public API to pump events. public void start() throws Exception { Class clazz = Class.forName("java.awt.Conditional"); Object conditional = Proxy.newProxyInstance( clazz.getClassLoader(), new Class[] {clazz}, this); Method pumpMethod = Class.forName("java.awt.EventDispatchThread") .getDeclaredMethod("pumpEvents", new Class[] {clazz}); pumpMethod.setAccessible(true); pumpMethod.invoke(Thread.currentThread(), new Object[] {conditional}); } } // 调用方法 public static void showAsModal(final Frame frame, final Frame owner) { frame.addWindowListener(new WindowAdapter() { public void windowOpened(WindowEvent e) { owner.setEnabled(false); } public void windowClosed(WindowEvent e) { owner.setEnabled(true); owner.removeWindowListener(this); } }); owner.addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent e) { if (frame.isShowing()) { frame.setExtendedState(JFrame.NORMAL); frame.toFront(); } else { owner.removeWindowListener(this); } } }); frame.setVisible(true); try { new EventPump(frame).start(); } catch (Throwable throwable) { throw new RuntimeException(throwable); } }} 不显示删除回复显示所有回复显示星级回复显示得分回复 怎么样利用点击第一个程序“登入”按钮 ,弹出第二个程序的窗体??? 我是初学者 最好是帮我修改下。。。谢 求一个字符串操作的简便方法 反射问题 使用aglet时关于domain name 的问题(急救!!) 请高手指点 这样一段代码!数据库的连接问题 drawString的问题 关于数据遍历的问题,高手们请指点 怪问题,居然能用JAVA关键字做类名!!! 哪位大虾知道安装pushlet?请指教! 关于String类中Split的问题。 聊天程序,如何打中文字体?
不知道要在什么时候最大化JDialog
如果只要求大小是全屏的话
可以设置size
1) JNI
2)
frame.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {}
public void windowLostFocus(WindowEvent e) {
frame.toFront();
}
} );第二种方法也有些问题,从其他程序切换回来的时候,弹出的JFrame不在最前面。
这个方法我试过了
不是想要的效果
不知道为什么...
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;public class ModalFrameUtil {
public ModalFrameUtil() {
//
} static class EventPump implements InvocationHandler {
Frame frame; public EventPump(Frame frame) {
this.frame = frame;
} public Object invoke(Object proxy, Method method, Object[] args) throws
Throwable {
return frame.isShowing() ? Boolean.TRUE : Boolean.FALSE;
} // when the reflection calls in this method has to be
// replaced once Sun provides a public API to pump events.
public void start() throws Exception {
Class clazz = Class.forName("java.awt.Conditional");
Object conditional = Proxy.newProxyInstance(
clazz.getClassLoader(),
new Class[] {clazz},
this);
Method pumpMethod = Class.forName("java.awt.EventDispatchThread")
.getDeclaredMethod("pumpEvents", new Class[] {clazz});
pumpMethod.setAccessible(true);
pumpMethod.invoke(Thread.currentThread(), new Object[] {conditional});
}
} // 调用方法
public static void showAsModal(final Frame frame, final Frame owner) {
frame.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
owner.setEnabled(false);
} public void windowClosed(WindowEvent e) {
owner.setEnabled(true);
owner.removeWindowListener(this);
}
}); owner.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
if (frame.isShowing()) {
frame.setExtendedState(JFrame.NORMAL);
frame.toFront();
}
else {
owner.removeWindowListener(this);
}
}
}); frame.setVisible(true);
try {
new EventPump(frame).start();
}
catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}}