java - 如何更改 Szymon 类中标签(在 Window 类中)的值并将其显示在窗口中?

标签 java swing variables label

我是 Java 初学者,所以请不要在回答中过于严厉。我试图通过编写对我来说相对困难的程序来提高我的 java 技能,但这个让我陷入困境。

public class Szymon {

    public static void reply(String name) {

        switch(name){
        case "michal":name="Niedzwiedz!";
              break;
        default:System.out.println("wot ?");
        }

     }

    public static void greet(String name){
        System.out.printf("Elo %s co tam u ciebie ? \n",name);
    }


}

我花了几个小时写这篇文章,尽管不是很多,但我从中获得了很多经验。

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class Window<label> extends JFrame{
       private static final long serialVersionUID=1L;

       public Window(){
           super("Display of Input & Output");
           setSize(600,400);
           setDefaultCloseOperation(EXIT_ON_CLOSE);


           JPanel p= new JPanel();


           JButton b= new JButton("reply()");
           final JTextField tf=new JTextField();
           tf.setColumns(10);
           JButton b2= new JButton("greet()");
           final JTextField tf2=new JTextField();
           tf2.setColumns(10);
           final JLabel label=new JLabel();


           p.add(b);
           p.add(tf);
           p.add(b2);
           p.add(tf2);
           p.add(label);

           new Szymon();

           b.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent ae){
                  String tfVal = tf.getText();
                  Szymon.reply(tfVal);


               }
            });
           b2.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent ae){
                   String tf2Val=tf2.getText();
                      Szymon.greet(tf2Val);
               }
            });

           add(p);

       }





}

最佳答案

不要尝试从您正在调用的方法内修改调用者(这是过度扩展责任的问题),而是返回调用者应应用的值...

public class Szymon {

    public static String reply(String name) {
        switch(name){
        case "michal":name="Niedzwiedz!";
              break;
        default:name = "wot ?";
        }
        // Return the value to be applied...
        return name;
     }

    public static void greet(String name){
        System.out.printf("Elo %s co tam u ciebie ? \n",name);
    }
}

然后,当您调用该方法时,将更改应用于返回...

b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
        String tfVal = tf.getText();
        String reVal = Szymon.reply(tfVal);
        label.setText(reValue);
    }
});

例如...

关于java - 如何更改 Szymon 类中标签(在 Window 类中)的值并将其显示在窗口中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18811570/

相关文章:

java - 无法使用 gmail-smtp 和 javamail 发送电子邮件?

java - 通过java执行pl sql block 的问题

java - 为什么未选择的 JButton 显示突出显示的图标?

c++ - 在 C++ 中是否有等效的 Java equals 方法?

mongodb - 使用变量本身更新 Mongo 变量值

java - Mapper 和 Reducer for K 意思是 Java Hadoop 中的算法

java - 尝试将多个 JPanel 添加到一个主 JPanel 中并设置边框布局

java - JFreeChart - Java 堆空间问题

java - 如何调整 JPanel 的大小

java - Java 是否有类成员的动态变量?