使用Java 创建一个SWT项目,并创建可视化应用程序窗口,大小为520×300,窗口添加文字标签:这是一个演示作业。在窗口上添加菜单栏:取款,存钱,查询余额菜单栏,并且点击取款菜单栏弹出新窗口,窗口大小为520×300,在新窗口上可以进行输入取款金额和密码,并且点击取款可以提示取款成功。点击存钱菜单栏,弹出一个新窗口,窗口大小为520×300,用户可以进行输入需要存的金额和卡号,点击存钱按钮提示存入成功。点击余额查询菜单栏,弹出新窗口,窗口大小为300×520,用户输入卡号,点击查询按钮,提示内容:您还有30000000元,写出完整代码。
时间: 2024-03-19 20:40:40 浏览: 48
SWT.rar_SWT tutorial_java swt_swt 登陆窗口_swt tutori_swt-designer
很抱歉,作为一个语言模型AI,我无法为您提供完整的可运行的代码。但是,我可以提供一些使用Java SWT创建窗口和菜单栏的示例代码,供您参考:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
public class Main {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("演示作业");
shell.setSize(520, 300);
shell.setLayout(new FillLayout());
// 添加文字标签
Label label = new Label(shell, SWT.CENTER);
label.setText("这是一个演示作业");
// 添加菜单栏
Menu menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
// 取款菜单项
MenuItem withdrawItem = new MenuItem(menuBar, SWT.CASCADE);
withdrawItem.setText("取款");
Menu withdrawMenu = new Menu(shell, SWT.DROP_DOWN);
withdrawItem.setMenu(withdrawMenu);
MenuItem withdrawSubItem = new MenuItem(withdrawMenu, SWT.NONE);
withdrawSubItem.setText("取款窗口");
withdrawSubItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
dialog.setText("取款");
dialog.setSize(520, 300);
dialog.setLayout(new FillLayout());
// 添加取款界面控件
Label amountLabel = new Label(dialog, SWT.NONE);
amountLabel.setText("取款金额:");
Text amountText = new Text(dialog, SWT.BORDER);
Label passwordLabel = new Label(dialog, SWT.NONE);
passwordLabel.setText("密码:");
Text passwordText = new Text(dialog, SWT.BORDER | SWT.PASSWORD);
Button withdrawButton = new Button(dialog, SWT.PUSH);
withdrawButton.setText("取款");
withdrawButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 取款成功提示
MessageBox messageBox = new MessageBox(dialog, SWT.OK);
messageBox.setText("提示");
messageBox.setMessage("取款成功");
messageBox.open();
dialog.dispose();
}
});
dialog.open();
}
});
// 存钱菜单项
MenuItem depositItem = new MenuItem(menuBar, SWT.CASCADE);
depositItem.setText("存钱");
Menu depositMenu = new Menu(shell, SWT.DROP_DOWN);
depositItem.setMenu(depositMenu);
MenuItem depositSubItem = new MenuItem(depositMenu, SWT.NONE);
depositSubItem.setText("存钱窗口");
depositSubItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
dialog.setText("存钱");
dialog.setSize(520, 300);
dialog.setLayout(new FillLayout());
// 添加存钱界面控件
Label amountLabel = new Label(dialog, SWT.NONE);
amountLabel.setText("存款金额:");
Text amountText = new Text(dialog, SWT.BORDER);
Label cardNumberLabel = new Label(dialog, SWT.NONE);
cardNumberLabel.setText("卡号:");
Text cardNumberText = new Text(dialog, SWT.BORDER);
Button depositButton = new Button(dialog, SWT.PUSH);
depositButton.setText("存款");
depositButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 存款成功提示
MessageBox messageBox = new MessageBox(dialog, SWT.OK);
messageBox.setText("提示");
messageBox.setMessage("存款成功");
messageBox.open();
dialog.dispose();
}
});
dialog.open();
}
});
// 余额查询菜单项
MenuItem balanceInquiryItem = new MenuItem(menuBar, SWT.CASCADE);
balanceInquiryItem.setText("查询余额");
Menu balanceInquiryMenu = new Menu(shell, SWT.DROP_DOWN);
balanceInquiryItem.setMenu(balanceInquiryMenu);
MenuItem balanceInquirySubItem = new MenuItem(balanceInquiryMenu, SWT.NONE);
balanceInquirySubItem.setText("余额查询窗口");
balanceInquirySubItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
dialog.setText("余额查询");
dialog.setSize(300, 520);
dialog.setLayout(new FillLayout());
// 添加余额查询界面控件
Label cardNumberLabel = new Label(dialog, SWT.NONE);
cardNumberLabel.setText("卡号:");
Text cardNumberText = new Text(dialog, SWT.BORDER);
Button inquiryButton = new Button(dialog, SWT.PUSH);
inquiryButton.setText("查询");
inquiryButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 余额查询提示
MessageBox messageBox = new MessageBox(dialog, SWT.OK);
messageBox.setText("余额查询");
messageBox.setMessage("您还有30000000元");
messageBox.open();
}
});
dialog.open();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
```
以上代码是一个简单的可视化窗口应用程序,包含了文字标签和菜单栏,并且菜单栏的某些项可以弹出新窗口进行相关操作。您可以根据自己的需求进行修改和扩展。
阅读全文