【问题标题】:Changing a String data with Jbuttons in Java在 Java 中使用 Jbuttons 更改字符串数据
【发布时间】:2014-03-27 22:30:23
【问题描述】:

我有一个String,叫做s

我有四个JButtons b1 b2 b3 b4 默认情况下String sHello World b1 使String s toUpperCase b2 使String s toLowerCase b3 允许用户更改字符串 b4 将字符串重置为Hello World

当我运行程序时,我将s 设置为 Hello World,但是当我单击按钮时,字符串并没有更改为它的新属性。

import java.awt.EventQueue;
import java.awt.event.*;
import javax.swing.*;

public class Driver {
    String s = "Hell World";
    String up;
    String low;

    private JFrame f;
    private JPanel p;

    JFrame frame = new JFrame("Add Some Text");

    JButton b1 = new JButton("Uppercase");
    JButton b2 = new JButton("Lowercase");
    JButton b3 = new JButton("New");
    JButton b4 = new JButton("Reset");

    JLabel jl = new JLabel(s);

    public Driver () {      
        gui();  

    }

    public void gui() { 
        f = new JFrame("Hello World");      
        p = new JPanel();   
        f.add(p);
        p.setLayout(null);

        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(b4);

        p.add(jl);

        b1.setLocation(27, 80);
        b2.setLocation(300, 80);
        b3.setLocation(27, 180);
        b4.setLocation(300, 180);

        jl.setLocation(240, 20);

        b1.setSize(230, 80);
        b2.setSize(230, 80);
        b3.setSize(230, 80);
        b4.setSize(230, 80);

        jl.setSize(230, 20);

        // pack the frame for better cross platform support
        f.pack();
        // Make it visible
        f.setVisible(true);
        f.setSize(560,300); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                 up = s.toUpperCase();
          }
      });

        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                low = s.toLowerCase();    
          }
      });

        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                s = JOptionPane.showInputDialog(frame, "");          
          }
      });

        b4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
             s = "Hello World";      
          }
      });

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                new Driver();

           }
        });
    } // End main Method

       } // End class Driver

【问题讨论】:

    标签: java string


    【解决方案1】:

    您必须将文本设置为标签! 试试这个:

    b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                 jl.setText(s.toUpperCase());
          }
      });
    
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                jl.setText(s.toLowerCase());    
          }
      });
    
        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                s = JOptionPane.showInputDialog(frame, "");   
                jl.setText(s);
          }
      });
    
        b4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                 jl.setText("Hello World");
          }
      });
    

    【讨论】:

      【解决方案2】:

      实际上字符串正在改变,这里发生的情况是您没有使用字符串的新值更新JLabel

      所以你的代码应该是这样的:

      b3.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  s = JOptionPane.showInputDialog(frame, "");
                  jl.setText(s);       
            }
        });
      

      然后在每个 Action ActionListener 中添加以下行。

      jl.setText(newValue);// newValue depends on what the ActionListener do.
      

      【讨论】:

        猜你喜欢
        • 2017-10-24
        • 1970-01-01
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        相关资源
        最近更新 更多