【问题标题】:Color changing using Scrollbar in java在java中使用滚动条改变颜色
【发布时间】:2013-03-18 07:22:43
【问题描述】:

我想根据在 3 滚动条中选择的值更改框架的背景颜色? 但这并没有发生在这里是我的代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class changeColor extends JFrame implements AdjustmentListener
{
    JScrollBar red;
    JScrollBar green;
    JScrollBar blue;


    changeColor()
    {
        super("SCROLLBAR DEMO");
        setLayout(new FlowLayout());
        setVisible(true);
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        red=new JScrollBar(JScrollBar.HORIZONTAL);
        green=new JScrollBar(JScrollBar.HORIZONTAL);
        blue=new JScrollBar(JScrollBar.HORIZONTAL);
        add(red);
        add(green);
        add(blue);
        red.addAdjustmentListener(this);
        green.addAdjustmentListener(this);
        blue.addAdjustmentListener(this);   
    }

    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
        int cr=0;
        int cg=0;
        int cb=0;
        if(ae.getSource()==red)
            cr=ae.getValue();
        else if(ae.getSource()==green)
            cg=ae.getValue();
        else if(ae.getSource()==blue)
            cb=ae.getValue();

        setBackground(new Color(cr,cg,cb)); 
    }


    public static void main(String args[])
    {
        changeColor obj=new changeColor();  
    }
}

问题是背景颜色没有改变。 我想知道是什么问题以及如何解决?

【问题讨论】:

  • 为什么不改用JSlider
  • 我只想用 JScrollBar 来做。我想知道背景颜色没有变化的原因是什么?

标签: java scrollbar adjustment


【解决方案1】:

这是一个很好的解决方案。首先,您正在创建 JFrame 使用它的常规方法,例如 setDefaultCloseOperation()setBounds()getContentPane()。然后从您的类中创建一个对象,然后使用它在整个程序中调用所有其他方法,在这种情况下,我创建了名为app 的对象。您必须记住的一件事是不要忘记使用AdjustmentEvent e 而不是ActionListener e :)。

此外,所有颜色更改都必须与 panel.setBackground(new Color(sbar1.getValue(),sbar2.getValue(), sbar3.getValue()in AdjustmentEvent 一起进行,因为一旦 Scrollbar 被更改,它的值将通过 getValue() 方法获取并添加到 new Color() 方法中的 @ 987654333@方法。

    import javax.swing.*;  
    import java.awt.event.*;
    import java.awt.*;

    public class Main implements AdjustmentListener {

        private static void createAndShowGUI() {
            // make frame..
          JFrame frame = new JFrame("JScrollBar");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setBounds(20,30,200,250);
          frame.getContentPane().setLayout(null);
          Main app = new Main();
          app.sbar1 = new JScrollBar(java.awt.Adjustable.VERTICAL, 127, 1,0,255);
          app.sbar1.setBounds(10,20, 10, 200);
          app.sbar1.setBackground(Color.red);
          app.sbar1.addAdjustmentListener(app);
          frame.getContentPane().add(app.sbar1);
          app.sbar2 = new JScrollBar(java.awt.Adjustable.VERTICAL, 127, 1,0,255);
          app.sbar2.setBounds(30,20, 10, 200);
          app.sbar2.setBackground(Color.green);
          app.sbar2.addAdjustmentListener(app);
          frame.getContentPane().add(app.sbar2);
          app.sbar3 = new JScrollBar(java.awt.Adjustable.VERTICAL, 127, 1,0,255);
          app.sbar3.setBounds(50,20, 10, 200);
          app.sbar3.setBackground(Color.blue);
          app.sbar3.addAdjustmentListener(app);
          frame.getContentPane().add(app.sbar3);

          app.panel = new JPanel();
          app.panel.setBounds(80,20,50,200);
          app.panel.setBackground(new Color(0,0,0));
          frame.getContentPane().add(app.panel);

          frame.setVisible(true); 
      }

      public void adjustmentValueChanged(AdjustmentEvent e)
      {
          panel.setBackground(new Color(sbar1.getValue(),sbar2.getValue(), sbar3.getValue()));
      }

      public static void main(String[] args) {
        // start off..
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
      }

      // application object fields    
      JScrollBar sbar1;
      JScrollBar sbar2;
      JScrollBar sbar3;
      JPanel panel;
}

我希望这对你有很大帮助。!!!

【讨论】:

    【解决方案2】:

    我在你发布代码时运行了你的代码,它有点工作,背景颜色确实改变了。

    问题 1:cr、cg 和 cb 需要是类变量。这样,您选择的颜色将混合在一起。它的书写方式,一次只会改变一种颜色。

    问题 2:要获得完整的颜色范围,您需要更改选择,使范围从 0 到 255。使用 JScrollBar 方法,我只能看到从 0 到 90 的值。这很容易通过移动到 JSlider 修复

    问题 3:您必须在 JFrame 的内容窗格上设置颜色。 (见评论)

    注释:Java 约定是用大写字母命名类,仅供参考。

    以下是可能会产生您正在寻找的结果的更改:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class changeColor extends JFrame implements AdjustmentListener
    {
        JScrollBar red;
        JScrollBar green;
        JScrollBar blue;
        int cr=0;
        int cg=0;
        int cb=0;
    
    
        changeColor()
        {
            super("SCROLLBAR DEMO");
            setLayout(new FlowLayout());
            setVisible(true);
            setSize(300,300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            red=new JScrollBar(JScrollBar.HORIZONTAL);
            green=new JScrollBar(JScrollBar.HORIZONTAL);
            blue=new JScrollBar(JScrollBar.HORIZONTAL);
            add(red);
            add(green);
            add(blue);
            red.addAdjustmentListener(this);
            green.addAdjustmentListener(this);
            blue.addAdjustmentListener(this);   
        }
    
        public void adjustmentValueChanged(AdjustmentEvent ae)
        {
    
            if(ae.getSource()==red)
                cr=ae.getValue();
            else if(ae.getSource()==green)
                cg=ae.getValue();
            else if(ae.getSource()==blue)
                cb=ae.getValue();
            System.out.println(cr + ":" + cg + ":" + cb);
    
            // add color to content pane
            this.getContentPane().setBackground(new Color(cr,cg,cb)); 
        }
    
    
        public static void main(String args[])
        {
            changeColor obj=new changeColor();  
        }
    }
    

    【讨论】:

    • 颜色仍然没有变化。我一次只想要一种颜色。
    • 这真的很奇怪,因为您编写的代码对我有用。你在做什么系统? Windows、Mac?
    • 约定我第一次知道它下次我将遵循命名约定
    • 你看到了什么?你看到相框了吗?所以你看到滚动条了吗?你能滑动滚动条吗?如果可以滑动它们,背景是什么颜色?白/黑,其他?
    • 我看到了框架。我也看到了滚动条。我可以滑动它们。颜色是灰色的,我认为框架的默认值。一切都在发生,但颜色没有改变?
    猜你喜欢
    • 2011-02-26
    • 1970-01-01
    • 2012-03-22
    • 2011-03-05
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多