Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击按钮时,我希望更改按钮背景_Java_Swing_Jframe_Jbutton - Fatal编程技术网

Java 单击按钮时,我希望更改按钮背景

Java 单击按钮时,我希望更改按钮背景,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,所以当我点击JFrame上的一个按钮时,我希望这个按钮只要点击就可以改变它的背景。有人能帮忙吗 谢谢 单击按钮时,我希望更改按钮背景 按下按钮前: import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingU

所以当我点击JFrame上的一个按钮时,我希望这个按钮只要点击就可以改变它的背景。有人能帮忙吗

谢谢

单击按钮时,我希望更改按钮背景

按下按钮前:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        createAndShowJFrame();
    }

    public static void createAndShowJFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JFrame frame = createJFrame();
                frame.setVisible(true);

            }
        });
    }

    private static JFrame createJFrame() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {

                JButton btn = ((JButton) ae.getSource());//get the button that was clicked

                //set its background and foreground
                btn.setBackground(Color.RED);
                btn.setForeground(Color.GREEN);
            }
        };

        JButton b = new JButton("Test");
        b.addActionListener(al);

        frame.add(b);

        frame.pack();

        return frame;
    }
}

按下按钮后:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        createAndShowJFrame();
    }

    public static void createAndShowJFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JFrame frame = createJFrame();
                frame.setVisible(true);

            }
        });
    }

    private static JFrame createJFrame() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {

                JButton btn = ((JButton) ae.getSource());//get the button that was clicked

                //set its background and foreground
                btn.setBackground(Color.RED);
                btn.setForeground(Color.GREEN);
            }
        };

        JButton b = new JButton("Test");
        b.addActionListener(al);

        frame.add(b);

        frame.pack();

        return frame;
    }
}


您可以通过多种方式实现这一点。以下是“为所有按钮应用按钮选择颜色”的示例代码段

UIManager.put("Button.select", new ColorUIResource(255, 0, 0));
JButton button = new JButton("Submit");
JFrame frame = new JFrame("JButton select color");
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 80);
frame.setVisible(true);

JToggleButton
可能是实现这一点的理想选择,如注释所示,您需要添加一些代码以确保按钮只能单击一次


或者,您可以使用标准的
JButton
并在单击时调用Disable(禁用)按钮,它将翻转到备用图标。

尚未尝试任何功能。我是新手。所以我不知道该怎么做。读这篇文章,试试看,如果你遇到任何问题,可以问问题。1)Swing组件应该在EDT上创建和操作。2) 调用
JFrame
setSize
不是推荐的做法,请改用
LayoutManager
并调用
pack()
代替
setSize
。3) 而是
JFrame.DISPOSE\u ON\u CLOSE
或者它将完全退出应用程序,而不管其他窗口或线程是否打开。4) OP并不是说所有的按钮都是一样的(当然他/她可能只是说了一下)+1尽管我不确定OP是想在点击后更改按钮,还是只在按住按钮时更改down@WhiplashOne请参阅,或者在jbuttonapi中实现相同的方法,isPressed