Java.Dialog

 

import java.awt.*; import javax.swing.*; public class sdsds { public static void main(String[] args){ JFrame f=new JFrame("color"); f.setSize(600,500); f.setLocation(300, 200); f.setLayout(new GridLayout(2,1,10,20)); Panel p1=new Panel(); Panel p2=new Panel(); p1.setLayout(new GridLayout(3,4,20,20)); Label red=new Label("红色",Label.CENTER); red.setFont(new Font(Font.DIALOG,Font.BOLD,20)); red.setBackground(Color.red); Button bt1=new Button("+"); bt1.setFont(new Font(Font.DIALOG,Font.BOLD,20)); TextField t1=new TextField("220"); t1.setFont(new Font(Font.DIALOG,Font.BOLD,50)); Button bt5=new Button("-"); bt5.setFont(new Font(Font.DIALOG,Font.BOLD,20)); p1.add(red); p1.add(bt1); p1.add(t1); p1.add(bt5); Label green=new Label("绿色",Label.CENTER); green.setFont(new Font(Font.DIALOG,Font.BOLD,20)); green.setBackground(Color.green); Button bt2=new Button("+"); bt2.setFont(new Font(Font.DIALOG,Font.BOLD,20)); TextField t2=new TextField("220"); t2.setFont(new Font(Font.DIALOG,Font.BOLD,50)); Button bt6=new Button("-"); bt6.setFont(new Font(Font.DIALOG,Font.BOLD,20)); p1.add(green); p1.add(bt2); p1.add(t2); p1.add(bt6); Label blue=new Label("蓝色",Label.CENTER); blue.setFont(new Font(Font.DIALOG,Font.BOLD,20)); blue.setBackground(Color.blue); Button bt3=new Button("+"); bt3.setFont(new Font(Font.DIALOG,Font.BOLD,20)); TextField t3=new TextField("220"); t3.setFont(new Font(Font.DIALOG,Font.BOLD,50)); Button bt7=new Button("-"); bt7.setFont(new Font(Font.DIALOG,Font.BOLD,20)); p1.add(blue); p1.add(bt3); p1.add(t3); p1.add(bt7); p2.setBackground(new Color(255, 255, 0)); f.add(p1); f.add(p2); f.setVisible(true); } } 帮我在上面代码的基础上,实现点击加减按钮能改变颜色值中各色值的分量,下面面板的颜色同步改变
最新发布
05-25
好的,你需要对代码进行一些修改。首先,你需要在每个加减按钮的监听器中获取当前颜色值的分量,然后根据加减操作改变分量值,并重新设置颜色。 下面是修改后的代码: ```java import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class sdsds { public static void main(String[] args) { JFrame f = new JFrame("color"); f.setSize(600, 500); f.setLocation(300, 200); f.setLayout(new GridLayout(2, 1, 10, 20)); Panel p1 = new Panel(); Panel p2 = new Panel(); p1.setLayout(new GridLayout(3, 4, 20, 20)); Label red = new Label("红色", Label.CENTER); red.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); red.setBackground(Color.red); Button bt1 = new Button("+"); bt1.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); TextField t1 = new TextField("220"); t1.setFont(new Font(Font.DIALOG, Font.BOLD, 50)); Button bt5 = new Button("-"); bt5.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); // 添加加减按钮的监听器 bt1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int value = Integer.parseInt(t1.getText()); if (value < 255) { value++; t1.setText(String.valueOf(value)); red.setBackground(new Color(value, Integer.parseInt(t2.getText()), Integer.parseInt(t3.getText()))); p2.setBackground(new Color(value, Integer.parseInt(t2.getText()), Integer.parseInt(t3.getText()))); } } }); bt5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int value = Integer.parseInt(t1.getText()); if (value > 0) { value--; t1.setText(String.valueOf(value)); red.setBackground(new Color(value, Integer.parseInt(t2.getText()), Integer.parseInt(t3.getText()))); p2.setBackground(new Color(value, Integer.parseInt(t2.getText()), Integer.parseInt(t3.getText()))); } } }); p1.add(red); p1.add(bt1); p1.add(t1); p1.add(bt5); Label green = new Label("绿色", Label.CENTER); green.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); green.setBackground(Color.green); Button bt2 = new Button("+"); bt2.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); TextField t2 = new TextField("220"); t2.setFont(new Font(Font.DIALOG, Font.BOLD, 50)); Button bt6 = new Button("-"); bt6.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); bt2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int value = Integer.parseInt(t2.getText()); if (value < 255) { value++; t2.setText(String.valueOf(value)); green.setBackground(new Color(Integer.parseInt(t1.getText()), value, Integer.parseInt(t3.getText()))); p2.setBackground(new Color(Integer.parseInt(t1.getText()), value, Integer.parseInt(t3.getText()))); } } }); bt6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int value = Integer.parseInt(t2.getText()); if (value > 0) { value--; t2.setText(String.valueOf(value)); green.setBackground(new Color(Integer.parseInt(t1.getText()), value, Integer.parseInt(t3.getText()))); p2.setBackground(new Color(Integer.parseInt(t1.getText()), value, Integer.parseInt(t3.getText()))); } } }); p1.add(green); p1.add(bt2); p1.add(t2); p1.add(bt6); Label blue = new Label("蓝色", Label.CENTER); blue.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); blue.setBackground(Color.blue); Button bt3 = new Button("+"); bt3.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); TextField t3 = new TextField("220"); t3.setFont(new Font(Font.DIALOG, Font.BOLD, 50)); Button bt7 = new Button("-"); bt7.setFont(new Font(Font.DIALOG, Font.BOLD, 20)); bt3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int value = Integer.parseInt(t3.getText()); if (value < 255) { value++; t3.setText(String.valueOf(value)); blue.setBackground(new Color(Integer.parseInt(t1.getText()), Integer.parseInt(t2.getText()), value)); p2.setBackground(new Color(Integer.parseInt(t1.getText()), Integer.parseInt(t2.getText()), value)); } } }); bt7.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int value = Integer.parseInt(t3.getText()); if (value > 0) { value--; t3.setText(String.valueOf(value)); blue.setBackground(new Color(Integer.parseInt(t1.getText()), Integer.parseInt(t2.getText()), value)); p2.setBackground(new Color(Integer.parseInt(t1.getText()), Integer.parseInt(t2.getText()), value)); } } }); p1.add(blue); p1.add(bt3); p1.add(t3); p1.add(bt7); p2.setBackground(new Color(255, 255, 0)); f.add(p1); f.add(p2); f.setVisible(true); } } ``` 现在你可以运行代码并测试它。当你单击加减按钮时,颜色值的分量将随之改变,并且下面的面板颜色也会同步改变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值