import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Dialog { private String icon; private String title; private String message; private Button submit; private Button cancel; private Button close; private Dialog(){ } private Dialog(String icon, String title, String message, Button submit, Button cancel, Button close) { this.icon = icon; this.title = title; this.message = message; this.submit = submit; this.cancel = cancel; this.close = close; this.submit = new Button(); // 初始化submit按钮 this.cancel = new Button(); // 初始化cancel按钮 this.close = new Button(); // 初始化close按钮 } public void show(){ //弹出 } public void dismiss(){ //关闭 } static class Builder{ private Dialog dialog = new Dialog(); public Builder title(String title){ dialog.setTitle(title); return this; } public Builder icon(String icon){ dialog.setIcon(icon); return this; } public Builder message(String message){ dialog.setMessage(message); return this; } public Dialog build(){ return dialog; } } public void setSubmitText(String text){ submit.setLabel(text); } public void setCancelText(String text){ cancel.setLabel(text); } public void setCloseText(String text){ close.setLabel(text); } /** * 回调函数 * @param callback */ public void addListenerOnSubmit(SubmitCallBackListener callback){ submit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //当用户对按钮执行了动作操作以后, //程序会到此处执行 callback.submitCallback(e); } }); } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }这段代码怎么在myeclipse中运行出对话框
时间: 2023-12-30 12:03:32 浏览: 43
这段代码只是定义了一个 Dialog 类,它包含了对话框的基本属性和方法,但并没有实现对话框的显示和交互逻辑。你需要在这个类的 show() 方法中添加对话框的显示逻辑,可以使用 Java 的 GUI 库(如 Swing 或 JavaFX)来创建对话框的 UI,并在 submit、cancel、close 按钮的点击事件中添加对应的交互逻辑。具体实现方式可以参考 Java GUI 相关的教程和示例代码。
相关问题
java利用awt和swing实现通讯录管理系统
要实现通讯录管理系统,可以使用 Java 的 AWT(Abstract Window Toolkit)和 Swing 库来设计用户界面。以下是一个简单的示例:
1. 首先,创建一个 JFrame 窗口,设置其大小和布局:
```
import javax.swing.*;
import java.awt.*;
public class AddressBook extends JFrame {
public AddressBook() {
setTitle("Address Book");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
}
}
```
2. 在 JFrame 窗口中添加一个 JPanel,用于放置通讯录条目。使用 JScrollPane 包装 JPanel,以便在需要时可以滚动:
```
import javax.swing.*;
import java.awt.*;
public class AddressBook extends JFrame {
public AddressBook() {
setTitle("Address Book");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(contentPane);
add(scrollPane, BorderLayout.CENTER);
}
}
```
3. 创建一个通讯录条目的类 AddressEntry,包括姓名、电话和电子邮件等信息:
```
public class AddressEntry {
private String name;
private String phoneNumber;
private String email;
public AddressEntry(String name, String phoneNumber, String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getEmail() {
return email;
}
}
```
4. 在 JPanel 中添加一个按钮,用于添加新的通讯录条目。创建一个对话框,让用户输入新条目的信息:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AddressBook extends JFrame {
private JPanel contentPane;
public AddressBook() {
setTitle("Address Book");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(contentPane);
add(scrollPane, BorderLayout.CENTER);
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Create a dialog to get the new entry information
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
JTextField nameField = new JTextField();
JTextField phoneField = new JTextField();
JTextField emailField = new JTextField();
panel.add(new JLabel("Name:"));
panel.add(nameField);
panel.add(new JLabel("Phone:"));
panel.add(phoneField);
panel.add(new JLabel("Email:"));
panel.add(emailField);
int result = JOptionPane.showConfirmDialog(null, panel, "Add New Entry", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
// Add the new entry to the content pane
AddressEntry entry = new AddressEntry(nameField.getText(), phoneField.getText(), emailField.getText());
addEntry(entry);
}
}
});
add(addButton, BorderLayout.SOUTH);
}
private void addEntry(AddressEntry entry) {
JLabel label = new JLabel(entry.getName() + " " + entry.getPhoneNumber() + " " + entry.getEmail());
contentPane.add(label);
contentPane.revalidate();
contentPane.repaint();
}
}
```
5. 启动应用程序,显示通讯录管理系统窗口:
```
public class Main {
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
addressBook.setVisible(true);
}
}
```
这是一个基本的通讯录管理系统,可以通过添加新的通讯录条目来扩展功能。当然,这只是一个简单的示例,真正的通讯录管理系统可能需要更复杂的功能和更多的界面元素。
只用awt不用Swing写以下代码package chap03; import javax.swing.; import java.awt.; import java.awt.event.*; public class GuessNumber extends JFrame implements ActionListener { private JLabel promptLabel, resultLabel; private JTextField inputField; private JButton guessButton, exitButton; private int targetNumber, guessCount, maxTryTime; public GuessNumber () { targetNumber = (int)(Math.random()*10)+1; guessCount = 0; maxTryTime = 5; promptLabel = new JLabel("请输入一个0~10之间的数字:"); inputField = new JTextField(10); guessButton = new JButton("猜!"); guessButton.addActionListener(this); exitButton = new JButton("退出"); exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); resultLabel = new JLabel(""); JPanel panel = new JPanel(); panel.add(promptLabel); panel.add(inputField); panel.add(guessButton); panel.add(exitButton); panel.add(resultLabel); this.add(panel); this.setTitle("猜数字游戏界面"); this.setSize(300, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e) { String inputText = inputField.getText(); try { int guessNumber = Integer.parseInt(inputText); guessCount++; if (guessCount < maxTryTime) { int c = maxTryTime - guessCount; if (guessNumber > targetNumber) { resultLabel.setText("输入的过大,您还有" + c + "次机会"); } else if (guessNumber < targetNumber) { resultLabel.setText("输入的过小,您还有" + c + "次机会"); } else { resultLabel.setText("恭喜你,猜对了!你一共猜了" + guessCount + "次"); guessButton.setEnabled(false); } } else { resultLabel.setText("抱歉游戏失败"); guessButton.setEnabled(false); } } catch (NumberFormatException ex) { resultLabel.setText("输入异常,请重新输入!!!"); } } public static void main(String[] args) { new GuessNumber(); } }运行时能给文字足够的显示空间
以下是只用 AWT 不用 Swing 写 GuessNumber 类的代码,增加了一些布局和字体设置,以保证文字有足够的显示空间:
```
package chap03;
import java.awt.*;
import java.awt.event.*;
public class GuessNumber extends Frame implements ActionListener {
private Label promptLabel, resultLabel;
private TextField inputField;
private Button guessButton, exitButton;
private int targetNumber, guessCount, maxTryTime;
public GuessNumber () {
targetNumber = (int)(Math.random()*10)+1;
guessCount = 0;
maxTryTime = 5;
promptLabel = new Label("请输入一个0~10之间的数字:");
promptLabel.setFont(new Font("Dialog", Font.PLAIN, 14));
inputField = new TextField(10);
inputField.setFont(new Font("Dialog", Font.PLAIN, 14));
guessButton = new Button("猜!");
guessButton.addActionListener(this);
guessButton.setFont(new Font("Dialog", Font.PLAIN, 14));
exitButton = new Button("退出");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exitButton.setFont(new Font("Dialog", Font.PLAIN, 14));
resultLabel = new Label("");
resultLabel.setFont(new Font("Dialog", Font.PLAIN, 14));
Panel panel = new Panel(new FlowLayout(FlowLayout.CENTER, 10, 10));
panel.add(promptLabel);
panel.add(inputField);
panel.add(guessButton);
panel.add(exitButton);
setLayout(new BorderLayout(10, 10));
add(panel, BorderLayout.CENTER);
add(resultLabel, BorderLayout.SOUTH);
setTitle("猜数字游戏界面");
setSize(300, 150);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String inputText = inputField.getText();
try {
int guessNumber = Integer.parseInt(inputText);
guessCount++;
if (guessCount < maxTryTime) {
int c = maxTryTime - guessCount;
if (guessNumber > targetNumber) {
resultLabel.setText("输入的过大,您还有" + c + "次机会");
} else if (guessNumber < targetNumber) {
resultLabel.setText("输入的过小,您还有" + c + "次机会");
} else {
resultLabel.setText("恭喜你,猜对了!你一共猜了" + guessCount + "次");
guessButton.setEnabled(false);
}
} else {
resultLabel.setText("抱歉游戏失败");
guessButton.setEnabled(false);
}
} catch (NumberFormatException ex) {
resultLabel.setText("输入异常,请重新输入!!!");
}
}
public static void main(String[] args) {
new GuessNumber();
}
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)