我有兩個按鈕的接口,一個用於保存單詞,另一個用於存儲字母。將值從一個對象傳遞到另一個對象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