[英]Write the value of a JTextField to a variable of type String from another class (JAVA)
[英]JTextField to display string from another class
我有一個小問題:例如,我的第一堂課打印出一個名為“ average”的字符串。 字符串平均值說“你好”。
例如:
package downloadpage;
public class DownLoadPage {
public void main(String[] args) throws IOException {
string average;
average = "hello";
System.out.print(average);
我的第二個類app.java顯示一個文本字段。
package downloadpage;
public class app {
private JFrame frame;
JTextField textField1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
app window = new app();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public app() {
initialize();
}
/**
*
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField1 = new JTextField();
frame.getContentPane().add(textField1, BorderLayout.NORTH);
textField1.setColumns(10);
}
}
運行app.java
以使textField1
在downloadpage.java
顯示"string average"
中的數據時,如何得到它?
任何幫助將不勝感激。
另外,當我運行app.java
如何設置它首先運行downloadpage.java
以獲得平均信息? 這樣,當app.java
運行時app.java
能夠擁有適當的數據,因為只有downloadpage.java
運行時才生成數據?
問題:
編輯
例如:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
@SuppressWarnings("serial")
public class GuiClass extends JPanel {
private NonGuiClass nonGuiClass;
private JTextField field = new JTextField(15);
public GuiClass(NonGuiClass nonGuiClazz) {
this.nonGuiClass = nonGuiClazz;
add(field);
nonGuiClass.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (NonGuiClass.ADDRESS.equals(pcEvt.getPropertyName())) {
field.setText(nonGuiClass.getAddress());
}
}
});
}
private static void createAndShowGUI() {
final NonGuiClass nonGuiClass = new NonGuiClass();
GuiClass paintEg = new GuiClass(nonGuiClass);
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
final String[] testStrings = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
int timerDelay = 1000;
new Timer(timerDelay, new ActionListener() {
private int index = 0;
@Override
public void actionPerformed(ActionEvent evt) {
nonGuiClass.setAddress(testStrings[index]);
index++;
index %= testStrings.length;
}
}).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class NonGuiClass {
public static final String ADDRESS = "address";
private SwingPropertyChangeSupport propChangeSupport =
new SwingPropertyChangeSupport(this);
private String address = "";
public String getAddress() {
return address;
}
public void setAddress(String address) {
String oldValue = this.address;
String newValue = address;
this.address = address;
propChangeSupport.firePropertyChange(ADDRESS, oldValue, newValue);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propChangeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propChangeSupport.removePropertyChangeListener(listener);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.