[英]Color changing using Scrollbar in java
i want to change the background color of frame as per the value selected in 3 scrollbar? 我想根据3滚动条中选择的值更改框架的背景色吗? But it is not happening here is my code. 但这没有发生,这是我的代码。
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();
}
}
The problem is that the background color is not changing. 问题是背景颜色没有改变。 I want to know what is the problem and how can i fix it? 我想知道问题出在哪里,我该如何解决?
This is a good solution. 这是一个很好的解决方案。 First you are creating JFrame
with it's normal methods such as setDefaultCloseOperation()
, setBounds()
, getContentPane()
. 首先,您要使用其常规方法(例如setDefaultCloseOperation()
, setBounds()
和getContentPane()
创建JFrame
。 Then create an object from your class then use that to call all the other methods through out the program, in this case I created object called app
. 然后从您的类中创建一个对象,然后使用该对象在整个程序中调用所有其他方法,在本例中,我创建了一个名为app
对象。 One thing you have to keep in mind is that don't forget to use AdjustmentEvent e
instead of ActionListener e
:). 您要记住的一件事是,不要忘记使用AdjustmentEvent e
而不是ActionListener e
:)。
Also all the color changes must go with this panel.setBackground(new Color(sbar1.getValue(),sbar2.getValue(), sbar3.getValue()
in AdjustmentEvent
, because once the Scrollbar
gets changed, it's value gets by the getValue()
method and added to the new Color()
method with in the setBackground()
method. 而且所有颜色更改都必须与此panel.setBackground(new Color(sbar1.getValue(),sbar2.getValue(), sbar3.getValue()
在AdjustmentEvent
,因为一旦Scrollbar
被更改,其值就由getValue()
方法,并使用setBackground()
方法将其添加到new Color()
方法中。
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;
}
I hope this helped you alot.!!! 我希望这对您有很大帮助。
I ran your code as you posted it and it sort of works, the background color does change. 当您发布代码时,我运行了您的代码,并且该代码可以正常工作,背景颜色确实发生了变化。
Issue 1: The cr, cg, and cb need to be class variables. 问题1:cr,cg和cb必须是类变量。 In this way, the colors from your selections will mix together. 这样,您选择的颜色将混合在一起。 The way it is written, only one color will change at a time. 书写方式一次只能更改一种颜色。
Issue 2: To get the full range of color, you will need to change your selections so that the range goes from 0 to 255. With the JScrollBar approach, I am only seeing values from 0 to 90. That could easily be fixed with a move to JSlider 问题2:要获得完整的颜色范围,您将需要更改选择,以使颜色范围从0到255。使用JScrollBar方法,我只能看到0到90的值。移至JSlider
Issue 3: You must set the color on the content pane of a JFrame. 问题3:您必须在JFrame的内容窗格上设置颜色。 (See comment) (见评论)
Comment: Java convention is to name classes with an uppercase, FYI. 注释:Java约定是使用大写字母FYI命名类。
Here is the change which may give the result you are looking for: 这是可以为您提供所需结果的更改:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.