[英]Color changing using Scrollbar in java
我想根据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();
}
}
问题是背景颜色没有改变。 我想知道问题出在哪里,我该如何解决?
这是一个很好的解决方案。 首先,您要使用其常规方法(例如setDefaultCloseOperation()
, setBounds()
和getContentPane()
创建JFrame
。 然后从您的类中创建一个对象,然后使用该对象在整个程序中调用所有其他方法,在本例中,我创建了一个名为app
对象。 您要记住的一件事是,不要忘记使用AdjustmentEvent e
而不是ActionListener e
:)。
而且所有颜色更改都必须与此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;
}
我希望这对您有很大帮助。
当您发布代码时,我运行了您的代码,并且该代码可以正常工作,背景颜色确实发生了变化。
问题1:cr,cg和cb必须是类变量。 这样,您选择的颜色将混合在一起。 书写方式一次只能更改一种颜色。
问题2:要获得完整的颜色范围,您将需要更改选择,以使颜色范围从0到255。使用JScrollBar方法,我只能看到0到90的值。移至JSlider
问题3:您必须在JFrame的内容窗格上设置颜色。 (见评论)
注释:Java约定是使用大写字母FYI命名类。
这是可以为您提供所需结果的更改:
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.