【问题标题】:How can I call for an info to be shown in JTextField()如何要求在 JTextField() 中显示信息
【发布时间】:2013-05-01 14:53:59
【问题描述】:

这里有两个类我想在 JTextField 中显示“端口”,谢谢

第一类

System.out.println("using port "+portId.getName());

2 级

textField = new JTextField();
frame.getContentPane().add(textField, "5, 3, left, default");
textField.setColumns(10);

【问题讨论】:

  • 向类 2 添加文本字段更新方法

标签: java swing jtextfield


【解决方案1】:

将以下方法添加到您的类 2 并从类 1 调用它。

public void updatePort(final String port) {
    // SwingUtilities.invokeLater is only needed if the method is called from outside the EDT
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            textField.setText(port);
        }
    });
}

【讨论】:

    【解决方案2】:

    我想在 JTextField 中显示“端口”

    您是希望端口显示在JTextField 中还是要获取端口显示在JTextField 中,这有点令人困惑。我在这里提供了实现这两项任务的方法。 在class 2中添加方法如下:

    pubic String getPort()//to get port shown in JTextField
    {
      return textField.getText();
    }
    public void setPort(String port)//to show the port in JTextField
    {
       textField.setText(port);
    }
    

    class 1 内你可以这样写:

    Class2 obj = new Class2();
    String port = obj.getPort();//to get Port from JTextField
    obj.setPort(port);//to set port in JTextField
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-05
      • 2012-09-11
      • 1970-01-01
      • 2013-02-01
      • 2015-03-26
      • 1970-01-01
      • 2012-10-11
      • 2019-03-23
      相关资源
      最近更新 更多