用eclipse RCP写一个长方形绘图,点击这个长方形弹出消息框的代码
时间: 2024-03-09 19:45:26 浏览: 37
好的,以下是一个基本的长方形绘图的代码示例,并且在点击长方形时弹出一个消息框:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class RectangleDrawingExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Rectangle Drawing Example");
shell.setLayout(new FillLayout());
// Create a custom canvas to draw the rectangle
CustomCanvas customCanvas = new CustomCanvas(shell, SWT.NONE);
customCanvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
customCanvas.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
// Check if the mouse click was inside the rectangle
Rectangle rectangle = customCanvas.getRectangle();
if (rectangle.contains(event.x, event.y)) {
// If the click was inside the rectangle, show a message box
MessageBox messageBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
messageBox.setText("Rectangle Clicked");
messageBox.setMessage("You clicked on the rectangle!");
messageBox.open();
}
}
});
shell.setSize(400, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
class CustomCanvas extends org.eclipse.swt.widgets.Canvas {
private Rectangle rectangle;
public CustomCanvas(org.eclipse.swt.widgets.Composite parent, int style) {
super(parent, style);
addListener(SWT.Paint, new Listener() {
@Override
public void handleEvent(Event event) {
GC gc = event.gc;
gc.setAntialias(SWT.ON);
gc.setForeground(new Color(getDisplay(), 0, 0, 0));
gc.drawRectangle(rectangle);
}
});
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
redraw();
}
@Override
public org.eclipse.swt.graphics.Point computeSize(int wHint, int hHint, boolean changed) {
return new org.eclipse.swt.graphics.Point(100, 100);
}
@Override
public org.eclipse.swt.graphics.Point computeSize(int wHint, int hHint) {
return new org.eclipse.swt.graphics.Point(100, 100);
}
@Override
public org.eclipse.swt.graphics.Point getSize() {
return new org.eclipse.swt.graphics.Point(100, 100);
}
@Override
public org.eclipse.swt.graphics.Point getSize(org.eclipse.swt.graphics.Point result) {
return new org.eclipse.swt.graphics.Point(100, 100);
}
@Override
public boolean isReparentable() {
return false;
}
@Override
public boolean setFocus() {
return false;
}
@Override
public void setParent(org.eclipse.swt.widgets.Composite parent) {
throw new UnsupportedOperationException("Cannot reparent CustomCanvas");
}
@Override
public void setSize(int width, int height) {
super.setSize(100, 100);
}
@Override
public void setSize(org.eclipse.swt.graphics.Point size) {
super.setSize(100, 100);
}
@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, 100, 100);
rectangle = new Rectangle(x + 10, y + 10, width - 20, height - 20);
}
@Override
public void setBounds(org.eclipse.swt.graphics.Rectangle rect) {
super.setBounds(rect.x, rect.y, 100, 100);
rectangle = new Rectangle(rect.x + 10, rect.y + 10, rect.width - 20, rect.height - 20);
}
}
```
这个例子使用了一个自定义的 Canvas 控件来绘制长方形。在点击长方形时,会弹出一个消息框来显示提示信息。你可以将这段代码复制到 Eclipse RCP 项目中,然后运行它来查看效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)