【问题标题】:Removing the text from a JPanel when a certain button is clicked单击某个按钮时从 JPanel 中删除文本
【发布时间】:2018-09-29 11:10:38
【问题描述】:

我正在尝试为我正在开发的基于文本的冒险游戏创建一个 GUI。 我需要做的是,当单击按钮“jButton3”时, 它将删除“jText1”的文本。我尝试添加一个 ItemListener,但我似乎无法弄清楚。代码如下。为简单起见,我省略了所有的 Imports 以及我的包名称,只知道当我尝试运行这个程序时没有任何错误。

我搜索了与我的主题相关的其他帖子,但找不到我要找的确切内容,其中大部分是关于将文本从 TextField 替换为 JLabel

public class DemoGUI extends javax.swing.JFrame {
    public JLabel jLabel1;
    public JTextField jText1;
    public JButton jButton1;
    public JButton jButton2;
    public JButton jButton3;
    String string1;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                DemoGUI inst = new DemoGUI();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }
    public DemoGUI() {
        super();
        initGUI();
    }
    private void removeTextWhenClicked(JButton btn, ItemEvent ev) {
        if(ev.getStateChange() == ItemEvent.ITEM_STATE_CHANGED) {
            jText1.setText("");
        }
    }

    public void initGUI() {
        try {
            FlowLayout thisLayout = new FlowLayout();
            getContentPane().setLayout(thisLayout);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

                jLabel1 = new JLabel("Center", SwingConstants.CENTER);
                getContentPane().add(jLabel1);
                jLabel1.setPreferredSize(new Dimension(320, 250));
                jLabel1.setText(string1);
                jLabel1.setBorder(BorderFactory.createLineBorder(Color.BLACK));

                jButton1 = new JButton();
                getContentPane().add(jButton1);
                jButton1.setPreferredSize(new Dimension(100, 50));
                jButton1.setText("Map");

                jButton2 = new JButton();
                getContentPane().add(jButton2);
                jButton2.setPreferredSize(new Dimension(100, 50));
                jButton2.setText("Inventory");

                jText1 = new JTextField();
                getContentPane().add(jText1);
                jText1.setPreferredSize(new Dimension(320, 100));
                jText1.setBorder(BorderFactory.createLineBorder(Color.BLACK));

                jButton3 = new JButton();
                getContentPane().add(jButton3);
                jButton3.setPreferredSize(new Dimension(100, 40));
                jButton3.setText("Submit");
                jButton3.addItemListener(ev -> removeTextWhenClicked(jButton3, ev));

            pack();
            this.setSize(350, 500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

【问题讨论】:

    标签: java swing awt jbutton jtextfield


    【解决方案1】:

    不要使用 addItemListener,而是使用 addActionListener

     jButton3.addActionListener(new ActionListener(){  
        public void actionPerformed(ActionEvent e){  
              
            }  
        });  
    

    你可以在Java: What's the difference between ActionEvent and ItemEvent on a JRadioButton?找到区别

    当按钮的状态为 更改,无论是通过用户与按钮交互还是 以编程方式(通过 setSelected 方法)。

    用户与按钮交互时将调用 ActionListeners

    【讨论】:

    • 感谢您的帮助,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多