我有一个小问题:我的第一堂课,例如打印出一个名为“average”的字符串。字符串平均值为“hello”。
例如:
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
显示来自"string average"
的{{1}}的数据时,我怎样才能获得它?
非常感谢任何帮助。
此外,当我运行downloadpage.java
时,如何将其设置为首先运行app.java
以获取平均信息?这种方式当downloadpage.java
运行时能够获得正确的数据,因为数据仅在app.java
运行后生成?
答案 0 :(得分:3)
问题:
修改强>
例如:
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);
}
}