我还在学习制作这个计算器程序,而不是很少 我想问一下如何删除一组数字的最后一位数
这是我目前的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class frame {
int num1,num2,ans;
public static void main(String[] args) {
JFrame gui = new JFrame("Kalkulator");
final JTextField output = new JTextField(100);
JPanel panel = new JPanel();
JButton hapus = new JButton("<-");
JButton satu = new JButton("1");
JButton dua = new JButton("2");
JButton tiga = new JButton("3");
JButton empat = new JButton("4");
JButton lima = new JButton("5");
JButton enam = new JButton("6");
JButton tujuh = new JButton("7");
JButton delapan = new JButton("8");
JButton sembilan = new JButton("9");
JButton nol = new JButton("0");
JButton kali = new JButton("X");
JButton bagi = new JButton("/");
JButton kurang = new JButton("-");
JButton tambah = new JButton("+");
JButton samadengan = new JButton("=");
JButton titik = new JButton(".");
JButton clear = new JButton("C");
JLabel nama = new JLabel("Zulham azwar achmad (5302414040)");
nama.setHorizontalAlignment(SwingConstants.CENTER);
output.setBounds(10,11,290,61); //(jarak geser ke-kanan , jarak geser kebawah , panjang , tinggi )
clear.setBounds(250,121,50,50);
nama.setBounds(10,77,290,34);
hapus.setBounds(190,121,50,50);
satu.setBounds(10, 121, 50, 50);
dua.setBounds(70,121,50,50);
tiga.setBounds(130,121,50,50);
empat.setBounds(10,181,50,50);
lima.setBounds(70,181,50,50);
enam.setBounds(130,181,50,50);
tujuh.setBounds(10,241,50,50);
delapan.setBounds(70,241,50,50);
sembilan.setBounds(130,241,50,50);
nol.setBounds(10,301,110,50);
titik.setBounds(130,301,50,50);
kali.setBounds(190,181,50,50);
bagi.setBounds(190,241,50,50);
kurang.setBounds(250,181,50,50);
tambah.setBounds(250,241,50,50);
samadengan.setBounds(190,301,110,50);
panel.setLayout(null);
panel.add(output);
panel.add(hapus);
panel.add(satu);
panel.add(dua);
panel.add(tiga);
panel.add(empat);
panel.add(lima);
panel.add(enam);
panel.add(tujuh);
panel.add(delapan);
panel.add(sembilan);
panel.add(nol);
panel.add(titik);
panel.add(kali);
panel.add(bagi);
panel.add(tambah);
panel.add(kurang);
panel.add(samadengan);
panel.add(clear);
panel.add(nama);
gui.getContentPane().add(panel);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(317,390);
gui.setVisible(true);
gui.setResizable(false);
satu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"1");
}
});
dua.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"2");
}
});
tiga.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"3");
}
});
empat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"4");
}
});
lima.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"5");
}
});
enam.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"6");
}
});
tujuh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"7");
}
});
delapan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"8");
}
});
sembilan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"9");
}
});
nol.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"0");
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(null);
}
});
hapus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(); <-- how to backspace ?
}
});
}
}
答案 0 :(得分:2)
您可以使用git merge
:
substring
答案 1 :(得分:0)
您可以使用substring
这样的方法:
String strwithoutLastDigit = someString.substring(0,someString.length()-1)
答案 2 :(得分:0)
你可以做同样的事情,
hapus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(output.getText().length()>0){
output.setText(output.getText().substring(0,output.getText().length()-1));
}
}
});
答案 3 :(得分:0)
*开始时的字符
* str.substring(开始,结束)
*最多但不包括结束
有一个更复杂的substring()版本,它接受起始索引号和结束索引号:substring(int start,int end)返回从起始索引号开始并运行的字符串,但不包括结束指数。
示例:
String str = "Hello";
String a = str.substring(2, 4); // a is "ll" (not "llo")
String b = str.substring(0, 3); // b is "Hel"
String c = str.substring(4, 5); // c is "o" -- the last char
hapus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = output.getText();
output.setText(0,text.length()-2));//will return all string but the last one
}
});