我有两个按钮的接口,一个用于保存单词,另一个用于存储字母。将值从一个对象传递到另一个对象java
为了让我管理单词1,我有一个班级,单词班。
在这个类中,有getter和setters以及方法。
方法“表”允许我检索值,我会得到我的按钮1,然后将其保存为一个选项卡char []
。
我希望我能采取同样的char array []
(按钮1),在我的第二个按钮的值相同
综上所述,我想用在按钮1输入的词,按钮2
但我不知道它是如何做到的?
// BUTTON 1
final JFrame popup = new JFrame();
//create new instance of JButton
final Mot monMot = new Mot();
newButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
String name = JOptionPane.showInputDialog(popup, "Enter one word", null);
monMot.setMot(name);
monMot.tableau();
try {
monMot.affichage();
} catch (Exception e) {
System.out.println(e);
}
}
//按钮2一键
final JFrame popup = new JFrame();
Mot monMot = new Mot();
boolean flag = false;
String key = JOptionPane.showInputDialog(popup, "Enter one key",null);
try {
while (flag == false) {
if (key.length() == 1) {
flag = true;
} else {
key = JOptionPane.showInputDialog(popup, "Enter one key",null);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
而且我 类public class蒙特{
private String mot;
private char[] tab;
//getter et setter
public String getMot() {
return mot;
}
public void setMot(String mot) {
this.mot = mot;
}
//constructeur plein
public Mot(String mot, char[] tab) {
this.mot = mot;
this.tab = tab;
}
//constructeur vide
public Mot() {
}
//methodes
public void affichage() {
for (int i = 0; i < this.tab.length; i++) {
System.out.println(this.tab[i]);
}
}
//placage de chaque lettre dans un tableau
public void tableau() {
this.tab = this.mot.toCharArray();
}
}
的JOptionPane可以与空返回。这就是为什么我建议你将while语句改为while(flag =(key == null))。通过这种方式,您可以为用户添加输入的旗帜和检查添加新值。 – bitli