【问题标题】:Pass one string from one frame to another将一个字符串从一帧传递到另一帧
【发布时间】:2013-07-28 18:35:01
【问题描述】:

我在一帧中有一个名为 uname 的字符串。

uname = usrNameTxt.getText();
char[] pword = pwordTxt.getPassword();
String password = new String(pword);

并将其指向下一帧

 this.dispose();
 new SectionsFclty(uname).setVisible(true);

在我的另一个(如下所示)框架中想要字符串 uname...

  public SectionsFclty() {
    initComponents();   
}

 public SectionsFclty(String uname) {
    initComponents();
    jLabelUsername.setText(uname);
}

但在我的第二帧 (SectionsFclty.java) 中出现错误

  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jLabelUsername = new javax.swing.JLabel();..............

 }  // shows an error now

跟随错误

  error: illegal start of expression
private void initComponents() {
            new SectionsFclty().setVisible(true);

必需:字符串 发现:没有参数 原因:实际参数列表和形式参数列表的长度不同 注意:某些输入文件使用未经检查或不安全的操作。 注意:重新编译时使用 -Xlint:unchecked for details。

【问题讨论】:

标签: java string swing


【解决方案1】:

您需要使用String 参数调用构造函数。

SectionsFclty fclt = new SectionsFclty(uname);
fclt.setVisible(true);

使用两行并不是一个严格的要求,但它有助于查看发生错误的位置。链接可以很方便并使代码更短,但至少只要您是初学者,请尽量保持简单。

我不建议将uname 设为静态。静态通常不需要,而且往往会导致比它解决的问题更多的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2020-06-16
    • 2017-01-19
    • 2015-10-10
    • 2019-07-15
    • 2015-03-13
    相关资源
    最近更新 更多