showMessageDialog(null, "群中已有相同的昵称,请更改!");
时间: 2024-02-25 17:52:54 浏览: 6
return 0;
}
```
这段代码中,我们首先定义了一个 `Graph` 结构体,它包含了这是 Java Swing 中的一个方法,用于在 GUI 界面中显示一条消息框,内容为“群中已有相同的昵称,请更改!”。该方法的第一个参数为消息框所在的父组件,由于这里传入的是 null,表示消息框不依赖于任何父组件,会在屏幕中央显示。第邻接矩阵和每个顶点的优先级值。然后,我们实现了所需的各种函数,包括计算入度和出度、判断两个顶点之间是否有边、拓扑排序等等。最后,我们在 `main` 函数中构建了一个有向无环图,并对其进行了拓扑排序。
相关问题
一个带有天元的五子棋界面,javas
这是一个基于Java Swing的五子棋界面,包含天元和落子功能。代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gobang extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel jp1, jp2;
private JButton jb1, jb2, jb3;
private JLabel jl1, jl2, jl3, jl4;
private JTextField jt1, jt2;
private int[][] chessboard = new int[15][15];// 二维数组存储棋盘信息
private int player = 1;// 玩家1为黑方,玩家2为白方
private boolean isOver = false;// 判断游戏是否结束,false表示未结束,true表示结束
private int x = -1, y = -1;// 记录玩家当前落子的坐标
public Gobang() {
super("五子棋游戏");// 设置窗体标题
setSize(600, 600);// 设置窗体大小
setLocationRelativeTo(null);// 将窗体放置到屏幕中央
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体关闭方式
setLayout(new BorderLayout());// 设置窗体布局为BorderLayout
// 创建顶部面板
jp1 = new JPanel();
jl1 = new JLabel("玩家1:");
jt1 = new JTextField(10);
jl2 = new JLabel("玩家2:");
jt2 = new JTextField(10);
jp1.add(jl1);
jp1.add(jt1);
jp1.add(jl2);
jp1.add(jt2);
add(jp1, BorderLayout.NORTH);
// 创建中心面板
jp2 = new JPanel(new GridLayout(15, 15));
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
JButton jb = new JButton();
jb.setPreferredSize(new Dimension(30, 30));// 设置按钮大小
jb.addActionListener(this);// 添加按钮事件监听器
jp2.add(jb);
}
}
add(jp2, BorderLayout.CENTER);
// 创建底部面板
jb1 = new JButton("开始游戏");
jb1.addActionListener(this);
jb2 = new JButton("重新开始");
jb2.setEnabled(false);
jb2.addActionListener(this);
jb3 = new JButton("退出游戏");
jb3.addActionListener(this);
jl3 = new JLabel("当前玩家:黑方");
jl4 = new JLabel("天元:");
JPanel jp3 = new JPanel();
jp3.add(jb1);
jp3.add(jb2);
jp3.add(jb3);
jp3.add(jl3);
jp3.add(jl4);
add(jp3, BorderLayout.SOUTH);
setVisible(true);// 显示窗体
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {// 点击开始游戏按钮
String name1 = jt1.getText().trim();
String name2 = jt2.getText().trim();
if (name1.equals("") || name2.equals("")) {
JOptionPane.showMessageDialog(this, "玩家名称不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
jl3.setText("当前玩家:黑方");
jt1.setEditable(false);
jt2.setEditable(false);
jb1.setEnabled(false);
jb2.setEnabled(true);
isOver = false;
} else if (e.getSource() == jb2) {// 点击重新开始按钮
int result = JOptionPane.showConfirmDialog(this, "确定要重新开始吗?", "提示", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
reset();
}
} else if (e.getSource() == jb3) {// 点击退出游戏按钮
int result = JOptionPane.showConfirmDialog(this, "确定要退出游戏吗?", "提示", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
System.exit(0);
}
} else {// 点击棋盘按钮
JButton jb = (JButton) e.getSource();
int index = jp2.getComponentZOrder(jb);// 获取按钮在面板中的索引
x = index / 15;// 计算按钮在棋盘中的行坐标
y = index % 15;// 计算按钮在棋盘中的列坐标
if (isOver) {// 游戏已结束,不能再落子
JOptionPane.showMessageDialog(this, "游戏已结束,请重新开始!", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (chessboard[x][y] != 0) {// 该位置已有棋子,不能再落子
JOptionPane.showMessageDialog(this, "该位置已有棋子,请重新选择!", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (player == 1) {
jb.setIcon(new ImageIcon("images/black.png"));// 设置按钮图标为黑子
chessboard[x][y] = 1;// 该位置值设为1,表示黑子
jl3.setText("当前玩家:白方");
player = 2;
} else {
jb.setIcon(new ImageIcon("images/white.png"));// 设置按钮图标为白子
chessboard[x][y] = 2;// 该位置值设为2,表示白子
jl3.setText("当前玩家:黑方");
player = 1;
}
if (checkWin()) {// 检测是否有一方胜利
if (player == 1) {
JOptionPane.showMessageDialog(this, jt2.getText().trim() + " 获胜!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, jt1.getText().trim() + " 获胜!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
}
isOver = true;
}
}
}
// 重置游戏
private void reset() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
JButton jb = (JButton) jp2.getComponent(i * 15 + j);
jb.setIcon(null);// 清空按钮图标
chessboard[i][j] = 0;// 清空棋盘信息
}
}
jt1.setText("");
jt2.setText("");
jt1.setEditable(true);
jt2.setEditable(true);
jl3.setText("当前玩家:黑方");
jb1.setEnabled(true);
jb2.setEnabled(false);
isOver = false;
player = 1;
x = -1;
y = -1;
}
// 检测是否有一方胜利
private boolean checkWin() {
int count = 1;// 记录连续相同颜色的棋子数
int color = chessboard[x][y];// 当前落子颜色
// 横向检测
for (int i = y - 1; i >= 0; i--) {// 向左检测
if (chessboard[x][i] == color) {
count++;
} else {
break;
}
}
for (int i = y + 1; i < 15; i++) {// 向右检测
if (chessboard[x][i] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 纵向检测
count = 1;
for (int i = x - 1; i >= 0; i--) {// 向上检测
if (chessboard[i][y] == color) {
count++;
} else {
break;
}
}
for (int i = x + 1; i < 15; i++) {// 向下检测
if (chessboard[i][y] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 左上到右下斜向检测
count = 1;
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {// 向左上检测
if (chessboard[i][j] == color) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y + 1; i < 15 && j < 15; i++, j++) {// 向右下检测
if (chessboard[i][j] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 左下到右上斜向检测
count = 1;
for (int i = x + 1, j = y - 1; i < 15 && j >= 0; i++, j--) {// 向左下检测
if (chessboard[i][j] == color) {
count++;
} else {
break;
}
}
for (int i = x - 1, j = y + 1; i >= 0 && j < 15; i--, j++) {// 向右上检测
if (chessboard[i][j] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
public static void main(String[] args) {
new Gobang();
}
}
```
需要注意的是,代码中用到了黑子和白子的图片,可以根据自己的需求修改为其他图片,或者直接使用文字表示棋子。图片文件需要放在与代码文件同一目录下。
java宠物商店项目代码,我需要若干个宠物的类如Cat,Dog用来输入宠物的颜色,年龄,名字和出售价格。还需要一个Pet的类用来修改宠物信息并且拥有删除功能,要有一个PetShop的类用来查看已有的宠物。还需要用java swing来实现用户界面,这个界面中用下拉列表以选择不同的宠物类后会跳转相应到的界面和PetShop类的查询功能。在跳转的界面中可以实现的类的功能
好的,根据您的需求,我为您编写了一个简单的Java宠物商店项目代码,包含Cat、Dog、Pet和PetShop类,并使用了Java Swing实现了用户界面。以下是代码:
Cat类:
```java
public class Cat {
private String color;
private int age;
private String name;
private double price;
public Cat(String color, int age, String name, double price) {
this.color = color;
this.age = age;
this.name = name;
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
Dog类:
```java
public class Dog {
private String color;
private int age;
private String name;
private double price;
public Dog(String color, int age, String name, double price) {
this.color = color;
this.age = age;
this.name = name;
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
Pet类:
```java
import java.util.ArrayList;
public class Pet {
private ArrayList<Cat> cats;
private ArrayList<Dog> dogs;
public Pet() {
this.cats = new ArrayList<>();
this.dogs = new ArrayList<>();
}
public ArrayList<Cat> getCats() {
return cats;
}
public ArrayList<Dog> getDogs() {
return dogs;
}
public void addCat(Cat cat) {
cats.add(cat);
}
public void addDog(Dog dog) {
dogs.add(dog);
}
public void removeCat(int index) {
cats.remove(index);
}
public void removeDog(int index) {
dogs.remove(index);
}
public void updateCat(int index, String color, int age, String name, double price) {
Cat cat = cats.get(index);
cat.setColor(color);
cat.setAge(age);
cat.setName(name);
cat.setPrice(price);
}
public void updateDog(int index, String color, int age, String name, double price) {
Dog dog = dogs.get(index);
dog.setColor(color);
dog.setAge(age);
dog.setName(name);
dog.setPrice(price);
}
}
```
PetShop类:
```java
import java.util.ArrayList;
public class PetShop {
private Pet pet;
public PetShop() {
this.pet = new Pet();
}
public ArrayList<Cat> getCats() {
return pet.getCats();
}
public ArrayList<Dog> getDogs() {
return pet.getDogs();
}
public void addCat(Cat cat) {
pet.addCat(cat);
}
public void addDog(Dog dog) {
pet.addDog(dog);
}
public void removeCat(int index) {
pet.removeCat(index);
}
public void removeDog(int index) {
pet.removeDog(index);
}
public void updateCat(int index, String color, int age, String name, double price) {
pet.updateCat(index, color, age, name, price);
}
public void updateDog(int index, String color, int age, String name, double price) {
pet.updateDog(index, color, age, name, price);
}
}
```
PetShopUI类(用户界面):
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class PetShopUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JComboBox<String> petComboBox;
private JLabel nameLabel, colorLabel, ageLabel, priceLabel;
private JTextField nameField, colorField, ageField, priceField;
private JButton addButton, removeButton, updateButton, queryButton;
private JList<String> petList;
private DefaultListModel<String> listModel;
private PetShop petShop;
public PetShopUI() {
petShop = new PetShop();
setTitle("Pet Shop");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new GridLayout(6, 2, 5, 5));
petComboBox = new JComboBox<>(new String[]{"Cat", "Dog"});
petComboBox.addActionListener(this);
mainPanel.add(new JLabel("Pet Type:"));
mainPanel.add(petComboBox);
nameLabel = new JLabel("Name:");
mainPanel.add(nameLabel);
nameField = new JTextField();
mainPanel.add(nameField);
colorLabel = new JLabel("Color:");
mainPanel.add(colorLabel);
colorField = new JTextField();
mainPanel.add(colorField);
ageLabel = new JLabel("Age:");
mainPanel.add(ageLabel);
ageField = new JTextField();
mainPanel.add(ageField);
priceLabel = new JLabel("Price:");
mainPanel.add(priceLabel);
priceField = new JTextField();
mainPanel.add(priceField);
addButton = new JButton("Add");
addButton.addActionListener(this);
mainPanel.add(addButton);
removeButton = new JButton("Remove");
removeButton.addActionListener(this);
mainPanel.add(removeButton);
updateButton = new JButton("Update");
updateButton.addActionListener(this);
mainPanel.add(updateButton);
queryButton = new JButton("Query");
queryButton.addActionListener(this);
mainPanel.add(queryButton);
listModel = new DefaultListModel<>();
petList = new JList<>(listModel);
JScrollPane scrollPane = new JScrollPane(petList);
add(mainPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == petComboBox) {
String petType = (String) petComboBox.getSelectedItem();
if (petType.equals("Cat")) {
nameLabel.setText("Cat Name:");
} else {
nameLabel.setText("Dog Name:");
}
} else if (e.getSource() == addButton) {
String petType = (String) petComboBox.getSelectedItem();
String name = nameField.getText();
String color = colorField.getText();
String ageStr = ageField.getText();
String priceStr = priceField.getText();
if (name.isEmpty() || color.isEmpty() || ageStr.isEmpty() || priceStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
int age = Integer.parseInt(ageStr);
double price = Double.parseDouble(priceStr);
if (petType.equals("Cat")) {
Cat cat = new Cat(color, age, name, price);
petShop.addCat(cat);
} else {
Dog dog = new Dog(color, age, name, price);
petShop.addDog(dog);
}
listModel.clear();
ArrayList<Cat> cats = petShop.getCats();
for (Cat cat : cats) {
listModel.addElement("Cat: " + cat.getName() + " " + cat.getColor() + " " + cat.getAge() + " " + cat.getPrice());
}
ArrayList<Dog> dogs = petShop.getDogs();
for (Dog dog : dogs) {
listModel.addElement("Dog: " + dog.getName() + " " + dog.getColor() + " " + dog.getAge() + " " + dog.getPrice());
}
} else if (e.getSource() == removeButton) {
int index = petList.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(this, "Please select a pet to remove", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
String selectedPet = petList.getSelectedValue();
if (selectedPet.startsWith("Cat")) {
petShop.removeCat(index);
} else {
petShop.removeDog(index);
}
listModel.remove(index);
} else if (e.getSource() == updateButton) {
int index = petList.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(this, "Please select a pet to update", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
String petType = (String) petComboBox.getSelectedItem();
String name = nameField.getText();
String color = colorField.getText();
String ageStr = ageField.getText();
String priceStr = priceField.getText();
if (name.isEmpty() || color.isEmpty() || ageStr.isEmpty() || priceStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
int age = Integer.parseInt(ageStr);
double price = Double.parseDouble(priceStr);
if (petType.equals("Cat")) {
petShop.updateCat(index, color, age, name, price);
} else {
petShop.updateDog(index, color, age, name, price);
}
listModel.clear();
ArrayList<Cat> cats = petShop.getCats();
for (Cat cat : cats) {
listModel.addElement("Cat: " + cat.getName() + " " + cat.getColor() + " " + cat.getAge() + " " + cat.getPrice());
}
ArrayList<Dog> dogs = petShop.getDogs();
for (Dog dog : dogs) {
listModel.addElement("Dog: " + dog.getName() + " " + dog.getColor() + " " + dog.getAge() + " " + dog.getPrice());
}
} else if (e.getSource() == queryButton) {
listModel.clear();
ArrayList<Cat> cats = petShop.getCats();
for (Cat cat : cats) {
listModel.addElement("Cat: " + cat.getName() + " " + cat.getColor() + " " + cat.getAge() + " " + cat.getPrice());
}
ArrayList<Dog> dogs = petShop.getDogs();
for (Dog dog : dogs) {
listModel.addElement("Dog: " + dog.getName() + " " + dog.getColor() + " " + dog.getAge() + " " + dog.getPrice());
}
}
}
public static void main(String[] args) {
PetShopUI petShopUI = new PetShopUI();
petShopUI.setVisible(true);
}
}
```
在这个用户界面中,您可以选择宠物类型,输入宠物的属性,使用按钮进行添加、删除和更新,以及查询已有宠物。当您添加、删除或更新宠物时,界面中的宠物列表将自动更新。