Java 按钮侦听器中的随机面板颜色仅工作一次

Java 按钮侦听器中的随机面板颜色仅工作一次,java,swing,colors,jframe,jlabel,Java,Swing,Colors,Jframe,Jlabel,我的侦听器只执行了一次changeColor方法 尝试了不同版本的随机颜色创建者 代码: // Java program to create a blank text // field of definite number of columns. import java.awt.*; import java.awt.event.*; import javax.swing.*; class Main extends JFrame implements ActionListener { //

我的侦听器只执行了一次changeColor方法

尝试了不同版本的随机颜色创建者

代码:

// Java program to create a blank text
// field of definite number of columns.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame implements ActionListener {
    // JTextField
    static JTextField textField;

    // JFrame
    static JFrame frame;

    // JButton
    static JButton button;

    // label to display text
    static JLabel label;

    static JPanel panel;

    // main class
    public static void main(String[] args)
    {
        // create a new frame to stor text field and button
        frame = new JFrame("textfield");

        // create a label to display text
        label = new JLabel("nothing entered");

        // create a new button
        button = new JButton("submit");

        // create a panel
        panel = new JPanel();

        // create an object of the text class
        Main te = new Main();

        // addActionListener to button
        button.addActionListener(te);

        // create an object of JTextField with 16 columns
        textField = new JTextField(16);

        // add buttons and textfield to label and then to panel
        panel.add(textField);
        panel.add(button);
        panel.add(label);

        label.setOpaque(true);

        // add panel to frame
        frame.add(panel);

        // set the size of frame
        frame.setSize(300, 300);

        panel.setBackground(Color.cyan);

        frame.show();
    }

    // if the button is pressed
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("submit")) {

            // set the text of the label to the text of the field
            if(textField.getText().equals("hue")) {
                panel.setBackground(changeColor());
            }

            label.setText(textField.getText());

            // set the text of field to blank
            textField.setText(" ");
        }
    }
    public Color changeColor() {
        Color randomColor = new Color((int)(Math.random() * 0x1000000));
        return randomColor;
    }
}
我希望程序在文本字段中键入色调时,用按钮反复创建新颜色。 遗憾的是,这只适用于一次。

在第78行您的呼叫:

textField.setText(" ");
我猜你想打电话:

textField.setText("");
使您的文本字段真正为空。不一样

第一次按下按钮后,文本字段包含。如果在此之后键入色调,则文本字段的内容是色调而不是色调


因此,您的if语句在textField.getText.equalshue中不正确,并且您的方法没有被调用。

方法changeColor对我有效。这里肯定是出了什么问题。是否每次都输入if语句?你试过调试吗?你检查过随机颜色了吗?该方法是否按预期调用?它会像你期望的那样返回不同的颜色吗?我真的不知道你不知道是什么意思?你知道如何使用System.out.println。。。?在方法中添加这些语句,以查看该方法是否在预期执行时执行。因此actionPerformed方法中的第一行代码可以是System.out.PrintlActionPerformed;如果您看到该消息,您知道该按钮已工作。您应该在每次单击按钮时看到此消息,而不仅仅是第一个按钮。似乎只有在您还可以显示变量的值以查看它们是否包含您期望的值时,actionPerformed Listener中带有if语句的所有操作才会完成。一旦知道了值是什么,就可以确定执行或不执行if语句的原因。这是基本的问题解决101@卡米克现在我明白了。。。这是两个不同的东西。。。刚才已经碰到这个了!谢谢你的耐心和解释,让我学会了钓鱼;这是如何教授OP基本调试的?你没有读过我的评论,告诉他如何调试逻辑流,以确保它按照预期的方式工作吗?告诉他这个问题无助于他解决下一个问题。如果你给某人一条鱼,他们可以吃一天。如果你教某人钓鱼,他们会终生食用。@camickr尽管我很感谢你莫里茨,我想camickr是对的。我认为重要的不仅仅是表演,而是教学!Camickr,既然我现在没有主意了,你会有一个新的友好的swing任务开始吗?@Camickr虽然我非常同意“教人钓鱼”的哲学,但我注意到很多人反对在这样的情况下使用这种方法。每个人都看到了与外围设备相关的东西:看到了吗