我正在尝试制作键盘,当我运行它时,一切正常。问题是我最多只能点击4个按钮,然后点击不再有效。此外,每次单击jtable时都不会填充它,每次都会更改字母。
JButton myButton1 = new JButton("<html><center> 2 <br /> ABC </center> </html>");
JButton myButton2 = new JButton("<html><center> 3 <br /> DEF </center> </html>");
JButton myButton3 = new JButton("<html><center> 4 <br /> GHI </center> </html>");
JButton myButton4 = new JButton("<html><center> 5 <br /> JKL </center> </html>");
JButton myButton5 = new JButton("<html><center> 6 <br /> MNO </center> </html>");
JButton myButton6 = new JButton("<html><center> 7 <br /> PQRS </center> </html>");
JButton myButton7;
JButton myButton8;
JButton myButton9;
JButton myButton10;
JButton myButton11;
JLabel t1;
JTextArea text1 = new JTextArea(2, 12);
// CLASS INSTANCES AND OBJECTS
myApplication() {
setLayout(null);
JPanel panel1 = new JPanel();
add(panel1);
// JLabel t1 = new JLabel("");
// panel1.add(t1);
panel1.add(text1);
panel1.setBounds(0, 130, 370, 40);
Font textFont1 = new Font("Arial Bold", Font.BOLD, 18);
JButton myButton = new JButton("1");
myButton.setBounds(20, 190, 60, 60);
// JButton myButton1 = new JButton("<html><center> 2 <br /> ABC </center> </html>");
myButton1.setBounds(85, 190, 60, 60);
myButton1.addActionListener(this);
// JButton myButton2 = new JButton("<html><center> 3 <br /> DEF </center> </html>");
myButton2.setBounds(150, 190, 60, 60);
myButton2.addActionListener(this);
// JButton myButton3 = new JButton("<html><center> 4 <br /> GHI </center> </html>");
myButton3.setBounds(20, 260, 60, 60);
myButton3.addActionListener(this);
// JButton myButton4 = new JButton("<html><center> 5 <br /> JKL </center> </html>");
myButton4.setBounds(85, 260, 60, 60);
myButton4.addActionListener(this);
// JButton myButton5 = new JButton("<html><center> 6 <br /> MNO </center> </html>");
myButton5.setBounds(150, 260, 60, 60);
myButton5.addActionListener(this);
// JButton myButton6 = new JButton("<html><center> 7 <br /> PQRS </center> </html>");
myButton6.setBounds(20, 330, 60, 60);
myButton6.addActionListener(this);
JButton myButton7 = new JButton("<html><center> 8 <br /> TUV </center> </html>");
myButton7.setBounds(85, 330, 60, 60);
JButton myButton8 = new JButton("<html><center> 9 <br /> WXYZ </center> </html>");
myButton8.setBounds(150, 330, 60, 60);
JButton myButton9 = new JButton("*");
myButton9.setBounds(20, 400, 60, 60);
JButton myButton10 = new JButton("0");
myButton10.setBounds(85, 400, 60, 60);
JButton myButton11 = new JButton("#");
myButton11.setBounds(150, 400, 60, 60);
add(myButton);
add(myButton1);
add(myButton2);
add(myButton3);
add(myButton4);
add(myButton5);
add(myButton6);
add(myButton7);
add(myButton8);
add(myButton9);
add(myButton10);
add(myButton11);
setTitle("Keypad");
setSize(300, 500);
setLocation(250, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private int clickCounter = 0;
public void actionPerformed(ActionEvent e) {
// Object src = e.getSource();
if(e.getSource()== myButton1)
if(clickCounter == 0)
text1.setText("2");
else if (clickCounter == 1)
text1.setText("A");
else if (clickCounter == 2)
text1.setText("B");
else if(clickCounter == 3)
text1.setText("C");
if(e.getSource() == myButton2)
if(clickCounter == 0)
text1.setText("3");
else if (clickCounter == 1)
text1.setText("D");
else if (clickCounter == 2)
text1.setText("E");
else if(clickCounter == 3)
text1.setText("F");
if(e.getSource()== myButton3)
if(clickCounter == 0)
text1.setText("4");
else if (clickCounter == 1)
text1.setText("G");
else if (clickCounter == 2)
text1.setText("H");
else if(clickCounter == 3)
text1.setText("I");
if(e.getSource()== myButton4)
if(clickCounter == 0)
text1.setText("5");
else if (clickCounter == 1)
text1.setText("J");
else if (clickCounter == 2)
text1.setText("K");
else if(clickCounter == 3)
text1.setText("L");
if(e.getSource()==myButton5)
if(clickCounter == 0)
text1.setText("6");
else if (clickCounter == 1)
text1.setText("M");
else if (clickCounter == 2)
text1.setText("N");
else if(clickCounter == 3)
text1.setText("O");
if(e.getSource()== myButton6)
if(clickCounter == 0)
text1.setText("7");
else if (clickCounter == 1)
text1.setText("P");
else if (clickCounter == 2)
text1.setText("Q");
else if(clickCounter == 3)
text1.setText("R");
else if(clickCounter == 4)
text1.setText("S");
clickCounter++;
}
}
答案 0 :(得分:0)
这看起来并不像是完整的来源,但似乎您没有重置clickCounter
,因此您的所有转换语句都无处可去。计数器达到4。
你不会问这个问题,但是我邀请你考虑创建你自己的类,PhoneButton或其他东西,它们被构造成在数组或字符串中有它的标签和它的多击字符。然后你可以处理一个地方的任何点击,if-then-else子句的整个列表将折叠成几行。