简体   繁体   English

按下按钮时 Netbeans GUI 背景颜色更改

[英]Netbeans GUI Background Color Change on Button Press

I'm using Netbeans 6.7.1.我正在使用 Netbeans 6.7.1。 I'm making a GUI application.我正在制作一个 GUI 应用程序。

What I want to have happen when I press a button (btnEnter, I named it) is for the background color of my application to change (not the color of the button, but of the mainPanel).当我按下一个按钮(btnEnter,我命名它)时,我想要发生的事情是改变我的应用程序的背景颜色(不是按钮的颜色,而是主面板的颜色)。 Does anyone know how to achieve this?有谁知道如何实现这一目标?

Thanks!谢谢!

Quicky one: i assume u have already created a jframe and inside u have the button.快速的一个:我假设你已经创建了一个 jframe 并且在你里面有按钮。

1.in design mode select the jButton and on the right property panel go to the events. 1.在设计模式中选择jButton并在右侧属性面板上转到事件。 find the onclick event of the jbutton 2. in the method the netbeans creates in your code window go and write :找到jbutton的onclick事件 2.在netbeans在你的代码窗口中创建的方法中去写:

jFrame.getContentPane().setBackground(Color.Green);

With this u tell the application when u click the button change the color of the jFrame.有了这个你告诉应用程序当你点击按钮改变jFrame的颜色。 Offcourse u have to change the name to what ur using ie i use jFrame but you may have called it xFramex.当然,您必须将名称更改为您使用的名称,即我使用jFrame,但您可能已将其称为xFramex。 so u have to write it as :所以你必须把它写成:

xFramex.getContentPane().setBackgroundcolor(Color.Green);

what u should know is that all containers(jPanel, JFrame etc) in java have the getContentPane() method for handing the colors and other styles.你应该知道的是,java 中的所有容器(jPanel、JFrame 等)都有 getContentPane() 方法来处理颜色和其他样式。 If u dont want the default colors like i have shown above (Color.Green) then u have to change it to something like this :如果您不想要上面显示的默认颜色(Color.Green),那么您必须将其更改为如下所示:

jFrame.getContentPane().setBackground(new Color(128, 234, 10));

The Color syntax is : Red (0-255), Green (0-255), Blue (0-255) U can easily find the combination desired if u open the Paint app of windows and look the numbers in the color palette there.颜色语法为:红色 (0-255)、绿色 (0-255)、蓝色 (0-255) 如果您打开 Windows 的 Paint 应用程序并查看调色板中的数字,您可以轻松找到所需的组合。 Hope i helped a bit.希望我能有所帮助。

This is what I used to change the background using a button add this to your jbutton that I called blue这就是我用来更改背景的按钮,将其添加到我称之为蓝色的 jbutton

blue.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    System.out.println("blue.actionPerformed, event="+evt);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);
    getContentPane().setBackground(newjava.awt.Color(51,98,118));
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM