货币兑换 API Java 图形界面

huangapple go评论69阅读模式
英文:

Currency Exchange API Java GUI

问题

以下是翻译好的代码部分:

public class GUI extends JFrame {

    // ...(其余部分未翻译)

    public GUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1121, 765);
        contentPane = new JPanel();
        contentPane.setBackground(Color.BLACK);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JTextPane FXRate = new JTextPane();
        FXRate.setForeground(new Color(255, 255, 255));
        FXRate.setBackground(new Color(0, 0, 0));
        FXRate.setEditable(false);
        FXRate.setFont(new Font("Tahoma", Font.BOLD, 11));

        panel_1.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
        FXRate.setText("FX Rates\r\n\r\nEUR-AUD 汇率: " + arb.fxEURAUD + "\r\nEUR-USD 汇率: " + arb.fxEURUSD);
        panel_1.add(FXRate);
    }
}

请注意,由于您要求只翻译代码部分,因此我只提供了代码的翻译。如果您需要进一步的帮助或解释,请随时提问。

英文:

I am fairly new to programming of this level and I was wondering if someone could help me with this.

So I am trying to create a currency exchange app using Java, and I have a problem updating the values on the GUI to reflect the new value on the API. Essentially ever so often the values change and it shows on the console, however, the GUI value never updates and stays the same.

I thought ActionListener would help solve this problem but either I have not implemented it properly or I haven't googled and come up with a solution properly.

Thank you in advance for any help 货币兑换 API Java 图形界面

Here is my code:
GUI.java

public class GUI extends JFrame {
	
	static Arb arb = new Arb();

	private JPanel contentPane;
  public static void main(String[] args) throws IOException, InterruptedException {
 		
 		ActionListener taskPerformer = new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                 try {
 					arb.runUpdate_fx("anAPI");
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}
             }
         };
         Timer timer = new Timer(100 ,taskPerformer);
         timer.setRepeats(true);
         timer.start();
 
         Thread.sleep(5000);
 		
 		EventQueue.invokeLater(new Runnable() {
 			public void run() {
 				try {
 					GUI frame = new GUI();
 					frame.setVisible(true);
 				} catch (Exception e) {
 					e.printStackTrace();
 				}
 			}
 		});
 	}

    public GUI() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  setBounds(100, 100, 1121, 765);
	  contentPane = new JPanel();
	  contentPane.setBackground(Color.BLACK);
	  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	  contentPane.setLayout(new BorderLayout(0, 0));
	  setContentPane(contentPane);

      JTextPane FXRate = new JTextPane();
      FXRate.setForeground(new Color(255, 255, 255));
      FXRate.setBackground(new Color(0, 0, 0));
      FXRate.setEditable(false);
      FXRate.setFont(new Font("Tahoma", Font.BOLD, 11));
      panel_1.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
      FXRate.setText("FX Rates\r\n\r\nEUR-AUD FX Rate: " + arb.fxEURAUD + "\r\nEUR-USD FX Rate: " + arb.fxEURUSD);
      panel_1.add(FXRate);
    }
}

Result:
EUR-AUD: 1.646659
after sometime
EUR-AUD: 1.646659

Expected Result:
EUR-AUD: 1.646659
after sometime
EUR-AUD: 1.80102

答案1

得分: 1

在Java中,引用按值传递

JTextField textField = new JTextField();
String text = "初始文本";
textField.setText(text); // 不会显示"初始文本";
text = "更新后的文本"; // 不会改变面板显示的内容
// 面板仍然保存对旧文本的引用
textField.setText(text); // 更新面板保存的引用为新文本

在事件侦听器中您需要调用`setText`,*使用更新后的字符串*来实际使文本字段显示该内容
英文:

References are passed by value in Java.

JTextField textField = new JTextField();
String text = "Initial text";
textField.setText(text); // no displays "Initial text";
text = "Updated text"; // doesn't change what the panel displays
// the panel still holds a reference to the old text
textField.setText(text); // updates the reference the panel holds to your new text

In your event listener, you need to call setText with the updated string to actually make the textfield display that.

答案2

得分: 0

你的定时器和事件处理程序看起来不错,但是 update 方法只是将新值获取到 Arb 对象中,没有任何东西将这些值提取出来放到 GUI 中。你可以在 update 方法返回后,在事件处理程序中显式地执行这个操作。为了实现这一点,你可能希望将 FXRate 声明为成员变量,这样你可以从动作监听器中访问它。

英文:

Your timer and event handler look good, but the update method only fetches new values into the Arb object; nothing takes those values and puts them into the GUI. You can do that explicitly in your event handler.after the update method returns. To enable that, you may want to make FXRate a member variable, so you can access it from the action listener.

huangapple
  • 本文由 发表于 2020年7月27日 22:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63116950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定