我在 Java 中使用 GUI 进行培训。所以我开始创建宠物游戏原型或类似的游戏。我创建了菜单来选择要做什么Register
、、、Info
或应用程序Exit
。创建字段和 dorpdownBox 以选择要注册的所有内容。还制作了sumbit
按钮(它的开始,所以我只在 petName 上添加了最大字符验证)。现在我被卡住了,我不知道如何从已选择并发送到其他的下拉框和文本框中获取所有信息class Pet
。我已经用谷歌搜索了,但还没有找到任何清楚的东西。
也许有人可以给我一些提示或为我的代码编写部分内容。我想将选定PetName
的 , PetType
,PetGender
带到其他class pet
.
Ps 我从谷歌复制了很多行,所以我只了解我的代码的 80-90%。
主.java
import javax.swing.*;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.*;
public class Main extends JFrame implements ActionListener {
public static void main (String []args){
new Main("Meniu"); // Create title
}
// Main class constructor
public Main(String title) {
super(title);
setMenu(); //create menu
setSize(300, 400);// size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close running program if window are closed
setLocationRelativeTo(null); // set window position at center
setResizable(false); //resizable or not
show();
}// Main class constructor
// menu choices
JMenuItem Registration, Apie, Exit;
// menu method for creation and style
private void setMenu() {
JMenuBar barObj = new JMenuBar(); // create menuBar obj
JMenu messagesObj = new JMenu("Meniu"); //create menu bar menu object
barObj.setBackground(Color.YELLOW); // set menu bar bg color
Registration = new JMenuItem("Registration");
Registration.setToolTipText("Push to register"); // write text when u hang mouse over
Registration.addActionListener(this);
Registration.setBackground(Color.WHITE); // set menu bar menu options bg color
messagesObj.add(Registration); // add Registration into messages
Apie = new JMenuItem("Apie");
Apie.setToolTipText("Push for information");
Apie.addActionListener(this);
Apie.setBackground(Color.WHITE);
messagesObj.add(Apie);
Exit = new JMenuItem("Exit");
Exit.setToolTipText("Here you will exit");
Exit.addActionListener(this);
Exit.setBackground(Color.WHITE);
messagesObj.add(Exit);
barObj.add(messagesObj);
setJMenuBar(barObj);
} //create menu end
// implemented method
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Registration){
int registReply = JOptionPane.showConfirmDialog(this, "Norite registruotis?",
"Išeiti", JOptionPane.YES_NO_OPTION);
if(registReply == JOptionPane.YES_OPTION){ //registReply is what u have choosen
petRegistration ();
}
}else if (e.getSource() == Apie)
JOptionPane.showMessageDialog(this, "Jus esate informacijos lange.", "Apie", JOptionPane.PLAIN_MESSAGE);
else if (e.getSource() == Exit){
int exitReply = JOptionPane.showConfirmDialog(this, "Ar norite Exit?",
"Išeiti", JOptionPane.YES_NO_OPTION);// exitReply is what u have choosen
if(exitReply == JOptionPane.YES_OPTION){// if its has been chose/ program will shutdown
System.exit(0);
}
} // if end
}// actionPerformed
public void petRegistration(){
Container container = getContentPane();
// petName textbox and label
JTextField jtfRegLabel = new JTextField("***Registration***", 25);
jtfRegLabel.setHorizontalAlignment(JTextField.CENTER);
jtfRegLabel.setEditable(false);
JTextField jtfText1 = new JTextField(7);
JTextField jtfNameLabel = new JTextField("Pet Name (min 3, max 16 letters)", 17);
jtfNameLabel.setEditable(false);
jtfText1.setDocument(new JTextFieldLimit(16)); // add limit to text box
// pettype combobox and label
Frame frame = new Frame("Choice");
Label label = new Label("What is your Choice:");
Choice choice = new Choice();
frame.add(choice);
choice.add("Cat ");
choice.add("Dog ");
choice.add("Fish ");
choice.add("Mouse ");
choice.add("Bird ");
choice.add("Horse ");
JTextField jtfTypeLabel = new JTextField("Pet Type, Choose one ", 17);
jtfTypeLabel.setEditable(false);
// petGender combobox and label
Choice choice1 = new Choice();
frame.add(choice1);
choice1.add("Male ");
choice1.add("Female ");
JTextField jtfGenderLabel = new JTextField("Pet Gender, Choose one ", 17);
jtfGenderLabel.setEditable(false);
// submit registration
JButton submitRegObj = new JButton("Submit");
container.add(jtfRegLabel);
container.add(jtfText1);
container.add(jtfNameLabel);
container.add(choice);
container.add(jtfTypeLabel);
container.add(choice1);
container.add(jtfGenderLabel);
container.add(submitRegObj);
container.setLayout(new FlowLayout());
setSize(300, 400); // set size of window
setVisible(true);// set it visible
}
}// Main clases end
宠物.java
public class Pet {
private String petName;
private String petType;
private String petGender;
public Pet(String petName, String petType, String petGender) {
super();
this.petName = petName;
this.petType = petType;
this.petGender = petGender;
}
}
我认为 JTextFieldLimit 类是必要的。它只是进行最大字符验证。
谢谢。