Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 使用按钮更改背景的GUI应用程序_Java_User Interface_Jframe_Jpanel_Jbutton - Fatal编程技术网

Java 使用按钮更改背景的GUI应用程序

Java 使用按钮更改背景的GUI应用程序,java,user-interface,jframe,jpanel,jbutton,Java,User Interface,Jframe,Jpanel,Jbutton,我正在尝试创建一个包含标签和按钮的JavaGUI应用程序。单击按钮时,第一个面板的背景色将更改。我有标签和按钮,但每次单击按钮都会出错。另外,我希望第一个面板最初有一个黄色的背景,然后切换到任何颜色。这是我的密码: import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import

我正在尝试创建一个包含标签和按钮的JavaGUI应用程序。单击按钮时,第一个面板的背景色将更改。我有标签和按钮,但每次单击按钮都会出错。另外,我希望第一个面板最初有一个黄色的背景,然后切换到任何颜色。这是我的密码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

public class ChangeDemo extends JFrame implements ActionListener 
{
public static final int WIDTH = 300;
public static final int HEIGHT= 200;
private JPanel biggerPanel;

public static void main(String[] args) 
{
    ChangeDemo gui = new ChangeDemo();
    gui.setVisible(true);
}

public ChangeDemo()
{
    super ("ChangeBackgroundDemo");
    setSize(WIDTH,HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridLayout(2,3));      

    JPanel biggerPanel = new JPanel();
    biggerPanel.setLayout(new BorderLayout());
    biggerPanel.setBackground(Color.YELLOW);

    JLabel namePanel = new JLabel("Click the button to change the background color");
    biggerPanel.add(namePanel, BorderLayout.NORTH);


    add(namePanel);
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());
    buttonPanel.setBackground(Color.LIGHT_GRAY);

    JButton changeButton = new JButton("Change Color");
    changeButton.addActionListener(this);
    buttonPanel.add(changeButton);

    add(buttonPanel); 
}

 public void actionPerformed(ActionEvent e)
{
    String buttonString = e.getActionCommand();

    if(buttonString.equals("Change Color"))
        biggerPanel.setBackground(Color.RED);            
    else
        System.out.println("Unexpected Error!");
}




}

这是一个基于代码修改的工作演示,我还没来得及整理它,但希望你能了解它的要点。问题是您没有手动将面板添加到北、南等边界,以便为其着色。希望这能有所帮助

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

public class ChangeDemo extends JFrame implements ActionListener 
{
public static final int WIDTH = 300;
public static final int HEIGHT= 200;
private JPanel biggerPanel = new JPanel();
private JPanel namePanel = new JPanel();

public static void main(String[] args) 
{
    ChangeDemo gui = new ChangeDemo();
    gui.setVisible(true);
}

public ChangeDemo()
{
super ("ChangeBackgroundDemo");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,3));      

//JPanel biggerPanel = new JPanel();
this.biggerPanel.setLayout(new BorderLayout());
this.biggerPanel.setBackground(Color.YELLOW);

JLabel nameLabel = new JLabel("Click the button to change the background color");
namePanel.add(nameLabel);
namePanel.setBackground(Color.YELLOW);
//this.biggerPanel.add(namePanel, BorderLayout.NORTH);


add(namePanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.LIGHT_GRAY);

JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(this);
changeButton.setActionCommand("Change Color");
buttonPanel.add(changeButton);

add(buttonPanel); 
}

 public void actionPerformed(ActionEvent e)
{
    String buttonString = e.getActionCommand();
        if(buttonString.equals("Change Color"))
            this.namePanel.setBackground(Color.RED);            
        else
        System.out.println("Unexpected Error!");
}

}

我对你的代码做了一些修改

首先,必须通过调用SwingUtilities.invokeLater来启动Swing应用程序

public static void main(String[] args) {
    SwingUtilities.invokeLater(new ChangeDemo());
}
其次,使用Swing组件。仅当要重写Swing组件的方法时,才可以扩展Swing组件

第三,我专门为您的JButton制作了一个动作侦听器。这样,您就不必检查特定的JButton字符串。您可以为GUI创建所需数量的操作侦听器

    JButton changeButton = new JButton("Change Color");
    changeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            isYellow = !isYellow;

            if (isYellow) buttonPanel.setBackground(Color.YELLOW);
            else buttonPanel.setBackground(Color.RED);
        }
    });
最后,我更改了JButton面板的背景色

这是整个ChangeDemo课程

package com.ggl.testing;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ChangeDemo implements Runnable {

    private boolean isYellow;

    private JFrame frame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ChangeDemo());
    }

    @Override
    public void run() {
        frame = new JFrame("Change Background Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

        JPanel namePanel = new JPanel();
        JLabel nameLabel = new JLabel(
                "Click the button to change the background color");
        nameLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        namePanel.add(nameLabel);

        mainPanel.add(namePanel);

        final JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.setBackground(Color.YELLOW);
        isYellow = true;

        JButton changeButton = new JButton("Change Color");
        changeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                isYellow = !isYellow;

                if (isYellow) buttonPanel.setBackground(Color.YELLOW);
                else buttonPanel.setBackground(Color.RED);
            }
        });

        buttonPanel.add(changeButton);

        mainPanel.add(buttonPanel);

        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

}

我试过了,但没用。当我点击按钮时仍然会出现错误。