有人可以告诉我我的代码有什么问题吗?
我正在尝试创建一个具有bin,dec等的UI。每次单击时,用户只能单击对应于数字系统的适当数字,也可以在textField中显示该数字,例如用户单击bin只能在文本字段上显示0和1。
我尝试使用嵌套的if语句,但似乎不起作用。请帮忙谢谢你。
以下是我的代码:
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;
import java.awt.FlowLayout;
public class Trial extends JFrame implements ActionListener
{
private JTextField textField;
private JButton n1, n2, n3, n4, n5, n6, n7, n8, n9, n0, add, subtract, multiply, divide, clear, backspace, bin, dec, oct, hex;
private JPanel panel, panel1, p;
public Trial(String str)
{
super(str);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
panel.add(n1 = new JButton("1"));
panel.add(n2 = new JButton("2"));
panel.add(n3 = new JButton("3"));
panel.add(backspace = new JButton("C"));
panel.add(n4 = new JButton("4"));
panel.add(n5 = new JButton("5"));
panel.add(n6 = new JButton("6"));
panel.add(bin = new JButton("BIN"));
panel.add(n7 = new JButton("7"));
panel.add(n8 = new JButton("8"));
panel.add(n9 = new JButton("9"));
panel.add(dec = new JButton("DEC"));
panel.add(add = new JButton("+"));
panel.add(n0 = new JButton("0"));
panel.add(subtract = new JButton("-"));
panel.add(oct = new JButton("OCT"));
panel.add(multiply = new JButton("*"));
panel.add(clear = new JButton("AC"));
panel.add(divide = new JButton("/"));
panel.add(hex = new JButton("HEX"));
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(textField = new JTextField(20));
panel.setBounds(20,20,300,150);
panel1.setBounds(0,0,300,20);
add(panel, BorderLayout.SOUTH);
add(panel1, BorderLayout.NORTH);
n1.addActionListener(this);
n2.addActionListener(this);
n3.addActionListener(this);
n4.addActionListener(this);
n5.addActionListener(this);
n6.addActionListener(this);
n7.addActionListener(this);
n8.addActionListener(this);
n9.addActionListener(this);
n0.addActionListener(this);
add.addActionListener(this);
subtract.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
clear.addActionListener(this);
backspace.addActionListener(this);
bin.addActionListener(this);
dec.addActionListener(this);
oct.addActionListener(this);
hex.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == bin){
if(event.getSource() == n1){
textField.setText(textField.getText() + "1");
}
else if(event.getSource() == n0){
textField.setText(textField.getText() + "0");
}
else if(event.getSource() == add){
textField.setText(textField.getText() + " + ");
}
else if(event.getSource() == subtract){
textField.setText(textField.getText() + " - ");
}
else if(event.getSource() == multiply){
textField.setText(textField.getText() + " * ");
}
else if(event.getSource() == divide){
textField.setText(textField.getText() + " / ");
}
else if(event.getSource() == clear){
textField.setText("");
}
else{
textField.setText("");
}
}
else if(event.getSource() == dec){
if(event.getSource() == n1){
textField.setText(textField.getText() + "1");
}
else if(event.getSource() == n2){
textField.setText(textField.getText() + "2");
}
else if(event.getSource() == n3){
textField.setText(textField.getText() + "3");
}
else if(event.getSource() == n4){
textField.setText(textField.getText() + "4");
}
else if(event.getSource() == n5){
textField.setText(textField.getText() + "5");
}
else if(event.getSource() == n6){
textField.setText(textField.getText() + "6");
}
else if(event.getSource() == n7){
textField.setText(textField.getText() + "7");
}
else if(event.getSource() == n8){
textField.setText(textField.getText() + "8");
}
else if(event.getSource() == n9){
textField.setText(textField.getText() + "9");
}
else if(event.getSource() == n0){
textField.setText(textField.getText() + "0");
}
else if(event.getSource() == add){
textField.setText(textField.getText() + " + ");
}
else if(event.getSource() == subtract){
textField.setText(textField.getText() + " - ");
}
else if(event.getSource() == multiply){
textField.setText(textField.getText() + " * ");
}
else if(event.getSource() == divide){
textField.setText(textField.getText() + " / ");
}
else if(event.getSource() == clear){
textField.setText("");
}
}
}
public static void main(String [] args)
{
new Trial("Le Frame");
}
}
以上代码的问题是它在文本字段中根本不显示任何字符。
以下是该程序的摘要: ![1]:(https://i.stack.imgur.com/R2Len.png)
答案 0 :(得分:1)
当event.getSource() == bin
为true时,嵌套的if语句都不能为true:
if(event.getSource() == n1){
此外,如果赋值语句始终为true,则也是如此
pass = 1; if(pass == 1){
答案 1 :(得分:0)
在更新后尝试重新粉刷或重新验证:
textField.repaint();
或者可能:
textField.revalidate();
签出:https://docs.oracle.com/javase/tutorial/uiswing/painting/problems.html
答案 2 :(得分:0)
因此,您需要一个单独的变量来保存当前视图类型(二进制/八进制/十进制/六进制)
在您的代码中,它应类似于:
<a href="{{ path('lesseeRegistration', {'invitationToken': invitationToken}) }}" target="_blank">ici</a>
请注意:最好为任何按钮提供单独的处理程序。例如,您可以使用lambdas来实现。在这种情况下,您不需要巨大的if-else-if块