【问题标题】:Using JButton to change background color in JFrame使用 JButton 更改 JFrame 中的背景颜色
【发布时间】:2014-06-11 01:46:17
【问题描述】:

我正在编写一个使用 JButton 的程序。当用户单击一个按钮时,背景应该会改变颜色,但无法从 actionPerformed() 方法访问 JFrame。谁能告诉我如何让它工作?

import java.awt.event.*; 
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
public class HandlerClass implements ActionListener{

  public static void main(String[] args){
    HandlerClass handler = new HandlerClass();
     final JFrame f = new JFrame("Testing out these JPanels");
     f.setSize(400, 100); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLocationRelativeTo(null); 
     f.setLayout(new GridLayout(2, 3));
     JButton b = new JButton("button 1");
     b.addActionListener(new HandlerClass());
     JButton butt = new JButton("button 2");

     JButton bug = new JButton("button 3");

     JButton button = new JButton("button 4");

     JButton button5 = new JButton("button 5");

     JButton button6 = new JButton("button 6");

     JPanel p = new JPanel();
     p.setVisible(true);
     JPanel pnl = new JPanel();
     p.add(b);
     p.add(butt);
     p.add(bug);
     pnl.add(button);
     pnl.add(button5);
     pnl.add(button6);
     f.add(p, BorderLayout.CENTER);
     f.add(pnl, BorderLayout.SOUTH);
     f.setVisible(true);
     f.setBackground(Color.RED);
   }
     public void actionPerformed(ActionEvent e){
        f.setBackground(Color.WHITE);
       }

     }

【问题讨论】:

    标签: java swing colors jbutton


    【解决方案1】:
    1. 不要在一个巨大的 main 方法中编写所有代码。这是针对初学者程序的,如果您希望您的代码不仅仅是 hello world,那么您将不得不使其成为真正的 OOP 程序。
    2. 使用非静态字段和方法创建一个类。
    3. 让 ActionListener 调用一个方法,该方法在其主体中更改主 JPanel 的背景颜色,即添加到 JFrame 的 contentPane 的背景颜色。
    4. 考虑使用匿名内部侦听器类,然后将侦听器代码的内容卸载到单独的方法中,无论是在您的 GUI 中还是在控件类中。

    【讨论】:

      【解决方案2】:

      你有几个选择:

      1. JFrame 实例移出主体,使其成为实例变量,如下所示:

        public class HandlerClass {
            private JFrame frame;
        
            public HandlerClass() {
                ...
            }
        }
        
      2. ActionListener 移动到相同的方法中:

        public class HandlerClass {
            public HandlerClass() {
                final JFrame frame = new JFrame();
                JButton button = new JButton();
                ...
                button.addActionListener(new ActionerListener() {
                    @Override
                    public void actionPerformed(ActionEvent e){
                        frame.setBackground(Color.WHITE);
                    }
                }
            }
        }
        

        第二个选择的唯一问题是您必须使用final 修饰符来引用frame,这意味着you can't change the reference later

      3. 以上都做。

      【讨论】:

        【解决方案3】:

        你的问题:

        JFrame can't be accessed from the actionPerformed()
        

        解决方案是,您不能从其他方法访问局部变量 看看herehere

        现在重要的是: 您的JFrame 上有两个JPanels。所以如果你改变JFrame的颜色,那么你将无法 看颜色变化。所以我的建议是改变JPanel 的颜色。 以下面的代码为例:

        import java.awt.BorderLayout;
        import java.awt.Color;
        import java.awt.EventQueue;
        
        import javax.swing.JFrame;
        import javax.swing.JPanel;
        import javax.swing.border.EmptyBorder;
        import java.awt.GridBagLayout;
        import javax.swing.JButton;
        import java.awt.GridBagConstraints;
        import java.awt.Insets;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        
        public class HandlerClass extends JFrame {
        
            private JPanel contentPane;
        
            /**
             * Launch the application.
             */
            public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            HandlerClass frame = new HandlerClass();
                            frame.setVisible(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        
            /**
             * Create the frame.
             */
            public HandlerClass() {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                GridBagLayout gbl_contentPane = new GridBagLayout();
                gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
                gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0};
                gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
                gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
                contentPane.setLayout(gbl_contentPane);
        
                JButton btnRed = new JButton("Red");
                GridBagConstraints gbc_btnRed = new GridBagConstraints();
                gbc_btnRed.insets = new Insets(0, 0, 5, 5);
                gbc_btnRed.gridx = 3;
                gbc_btnRed.gridy = 1;
                contentPane.add(btnRed, gbc_btnRed);
        
                JButton btnBlue = new JButton("Blue");
                GridBagConstraints gbc_btnBlue = new GridBagConstraints();
                gbc_btnBlue.insets = new Insets(0, 0, 5, 0);
                gbc_btnBlue.gridx = 5;
                gbc_btnBlue.gridy = 1;
                contentPane.add(btnBlue, gbc_btnBlue);
        
                JButton btnWhite = new JButton("White");
                GridBagConstraints gbc_btnWhite = new GridBagConstraints();
                gbc_btnWhite.insets = new Insets(0, 0, 0, 5);
                gbc_btnWhite.gridx = 3;
                gbc_btnWhite.gridy = 2;
                contentPane.add(btnWhite, gbc_btnWhite);
        
                JButton btnGreen = new JButton("Green");
                GridBagConstraints gbc_btnGreen = new GridBagConstraints();
                gbc_btnGreen.gridx = 5;
                gbc_btnGreen.gridy = 2;
                contentPane.add(btnGreen, gbc_btnGreen);
        
                btnRed.addActionListener(new ActionListener() {
        
                    public void actionPerformed(ActionEvent e) {
                        contentPane.setBackground(Color.red);
        
                    }
                });
                btnBlue.addActionListener(new ActionListener() {
        
                    public void actionPerformed(ActionEvent e) {
        
                        contentPane.setBackground(Color.blue);
                    }
                });
                btnWhite.addActionListener(new ActionListener() {
        
                    public void actionPerformed(ActionEvent e) {
                        contentPane.setBackground(Color.WHITE);
        
                    }
                });
                btnGreen.addActionListener(new ActionListener() {
        
                    public void actionPerformed(ActionEvent e) {
        
                        contentPane.setBackground(Color.green);
                    }
                });
        
                pack();
            }
        
        }
        

        屏幕截图:

        【讨论】:

          猜你喜欢
          • 2014-05-11
          • 1970-01-01
          • 1970-01-01
          • 2011-01-28
          • 2014-05-28
          • 1970-01-01
          • 1970-01-01
          • 2015-11-16
          • 2017-02-23
          相关资源
          最近更新 更多