我必须在某个动作上显示一个消息框,我能够这样做但是消息对话框看起来尺寸较小。有没有办法增加java中消息框的大小?
这是我的代码:
Display.getDefault().syncExec(new Runnable () {
@Override
public void run() {
// Get active window page
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
Shell parent = window.getShell();
MessageBox dialog =
new MessageBox(shell,SWT.RESIZE|SWT.ICON_INFORMATION|SWT.OK);
dialog.setText("Project info");
dialog.setMessage("Project info Text will come here");
int returnCode = dialog.open();
System.out.println("returnCode "+ returnCode);
}
});
答案 0 :(得分:1)
您可以创建自己的对话框。看一下示例here。我已经复制了相关代码,以防链接停止工作:
public class DialogExample extends Dialog {
public DialogExample(Shell parent) {
super(parent);
}
public String open() {
Shell parent = getParent();
Shell dialog = new Shell(parent, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
dialog.setSize(100, 100);
dialog.setText("Java Source and Support");
dialog.open();
Display display = parent.getDisplay();
while (!dialog.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return "After Dialog";
}
public static void main(String[] argv) {
new DialogExample(new Shell());
}
}
答案 1 :(得分:0)
据我所知,SWT使用OS系统消息框,这意味着您只能更改标题/文本和图标类型。
但你可以使用JFace,它是SWT的扩展,并尝试使用org.eclipse.jface.dialogs.MessageDialog API。
答案 2 :(得分:0)
这是我的版本:您不需要对对话框大小进行硬编码,而是根据用户的屏幕大小进行变换。
我在下面添加了我的个人帮助方法,并展示了它们的用法。
这比操作系统MessageBox
更灵活,但你必须手动添加图标(我认为你甚至可以覆盖MessageDialog
而不是Dialog
- 你的调用)
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
/**
*
* @author stackoverflow.com/users/1774643/ggrec
*
*/
public class CustomMessageBox extends Dialog
{
// ==================== 1. Static Fields ========================
/*
* Default ratio values for big dialogs
*/
private static final double DEFAULT_DIALOG_WIDTH_SCREEN_PERCENTAGE = 0.63;
private static final double DEFAULT_DIALOG_HEIGHT_SCREEN_PERCENTAGE = 0.75;
// ==================== Static Helper Methods ====================
/**
* <pre>
* Very useful for dialogs. Resizes the dialog shell to a size proportional
* to the primary monitor bounds.
*
* <b>1920x1080 monitor && 0.63x0.75 ratios => 1400x800 shell</b>
*
* This means 63% of screen's width, and 75% of screen's height.
*
* The advantage of this method is that the dialog will always be proportional
* to the screen, no matter what the screen size is (e.g. big LG flat TV screen,
* or MacOS display).
* </pre>
*
* @param shell Dialog shell (e.g. the 'newShell' object in configureShell API from Dialog)
* @param widthRatio A percentage of current screen's size (i.e. smaller than 1, bigger than 0)
* @param heightRatio
*/
public static void resizeAndCenterShellOnScreen(final Shell shell, final double widthRatio, final double heightRatio)
{
final Display display = shell.getDisplay();
final Monitor primaryMonitor = display.getPrimaryMonitor();
final Rectangle monitorRect = primaryMonitor.getBounds();
// 1.) Resize the Shell
final int newWidth = (int) Math.floor(monitorRect.width * widthRatio);
final int newHeight = (int) Math.floor(monitorRect.height * heightRatio);
shell.setSize(newWidth, newHeight);
centerShellOnScreen(shell);
}
public static void centerShellOnScreen(final Shell shell)
{
final Rectangle shellBounds = shell.getBounds();
final Display display = shell.getDisplay();
final Monitor primaryMonitor = display.getPrimaryMonitor();
final Rectangle monitorRect = primaryMonitor.getBounds();
final int x = monitorRect.x + (monitorRect.width - shellBounds.width) / 2;
final int y = monitorRect.y + (monitorRect.height - shellBounds.height) / 2;
shell.setLocation(x, y);
}
// ==================== 3. Static Methods ====================
public static void main(final String[] args)
{
new CustomMessageBox().open();
}
// ==================== 4. Constructors ====================
public CustomMessageBox()
{
super(Display.getDefault().getActiveShell());
}
// ==================== 5. Creators ====================
@Override
protected Control createDialogArea(final Composite parent)
{
final Composite dialogArea = (Composite) super.createDialogArea(parent);
final Label label = new Label(dialogArea, SWT.NULL);
label.setText("YOLO");
label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
return dialogArea;
}
@Override
protected void configureShell(final Shell newShell)
{
super.configureShell(newShell);
resizeAndCenterShellOnScreen(newShell, DEFAULT_DIALOG_WIDTH_SCREEN_PERCENTAGE, DEFAULT_DIALOG_HEIGHT_SCREEN_PERCENTAGE);
}
}
干杯。