String s1 =t1.getText().trim(); int x= Integer.valueOf(s1, 2); if(x<0){ StringBuffer s12 =new StringBuffer(s1); s12.deleteCharAt(0); int l=s12.length()-1; char a=s12.charAt(l); for(int i=l;i>0;i--){ s12.setCharAt(i, s12.charAt(i-1)); } s12.setCharAt(0, a); String shuchu =s12.toString(); t2.setText("1"+shuchu); }else if(x>0){ StringBuffer s12 =new StringBuffer(s1); s12.deleteCharAt(0); int l=s12.length()-1; char a=s12.charAt(l); for(int i=l;i>0;i--){ s12.setCharAt(i, s12.charAt(i-1)); } s12.setCharAt(0, a); String shuchu =s12.toString(); t2.setText("0"+shuchu);这段代码的作用
时间: 2024-02-10 18:32:52 浏览: 69
这段代码的作用是将文本框t1中输入的二进制数转换为带符号的二进制数,并将结果显示在文本框t2中。具体来说,它先使用Java中的Integer类将输入的二进制数转换为十进制数x,然后判断x的正负性,如果x小于0,则表示输入的是负数,需要将其转换为带符号的二进制数并在最前面加上符号位"1",如果x大于0,则表示输入的是正数,需要在最前面加上符号位"0"。最后将转换后的结果显示在t2文本框中。
相关问题
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); } } 帮我在上面代码的基础上,实现点击加减按钮能改变颜色值中各色值的分量,下面面板的颜色同步改变
好的,你需要对代码进行一些修改。首先,你需要在每个加减按钮的监听器中获取当前颜色值的分量,然后根据加减操作改变分量值,并重新设置颜色。
下面是修改后的代码:
```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);
}
}
```
现在你可以运行代码并测试它。当你单击加减按钮时,颜色值的分量将随之改变,并且下面的面板颜色也会同步改变。
阅读全文