我正在研究计算器GUI,我需要帮助编写退格和清除按钮。
以下是代码,names[0][3]
是退格符,names[0][4]
用于清除整个区域。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class CalcGUI extends JFrame
{
private Container contents;
private final JPanel entryPanel, buttonPanel;
private final JTextArea entryTA;
private final JButton equalJB;
private JButton [][] buttons;
private final String [][] names =
{{"7", "8", "9", "\u2190", "C"},
{"4", "5", "6", "+", "-"},
{"1", "2", "3", "*", "/"},
{"0", ".", "(", ")", "^"}};
public CalcGUI()
{
super("Calculator");
contents = getContentPane();
contents.setLayout(new BorderLayout());
buttons = new JButton [4][5];
buttonPanel = new JPanel(new GridLayout(4, 5));
ButtonHandler bh = new ButtonHandler();
for (int i = 0; i < names.length; i++)
{
for (int j = 0; j < names[i].length; j++)
{
buttons[i][j] = new JButton(names[i][j]);
buttons[i][j].addActionListener(bh);
buttonPanel.add(buttons[i][j]);
}
}
contents.add(buttonPanel, BorderLayout.CENTER);
entryTA = new JTextArea(3, 1);
entryTA.setLineWrap(true);
entryPanel = new JPanel(new GridLayout(1, 1));
entryPanel.add(entryTA);
contents.add(entryPanel, BorderLayout.NORTH);
equalJB = new JButton("=");
equalJB.addActionListener(bh);
contents.add(equalJB, BorderLayout.SOUTH);
setSize(300, 300);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed (ActionEvent ae)
{
for (int i = 0; i < names.length; i++)
{
for (int j = 0; j < names[i].length; j++)
{
if (ae.getSource() == buttons[i][j])
{
if (ae.getSource() != buttons[0][3]
&& ae.getSource() != buttons[0][4])
{
entryTA.append(names[i][j]);
}
}
//backspace
else if (ae.getSource() == buttons[0][3])
{
try
{
Document doc = entryTA.getDocument();
if (doc.getLength() > 0)
{
doc.remove(doc.getLength() - 1, 1);
}
}
catch (BadLocationException ble)
{
ble.printStackTrace();
}
}
//clear - works, as per @Frostsoft's solution
else if (ae.getSource() == buttons[0][4])
{
entryTA.setText("");
}
}
}
}
}
public static void main(String [] args)
{
CalcGUI calc = new CalcGUI();
}
}
任何帮助或建议表示赞赏。谢谢!
编辑:我认为问题是它是退缩,直到doc.getLength > 0
为假。但是,删除条件会导致抛出异常。
Document doc = entryTA.getDocument();
if (doc.getLength() > 0)
{
System.out.println(doc.getLength());
doc.remove(doc.getLength() - 1, 1);
System.out.println("removed");
}
对于输入789
,仅按退格键JButton一次:
3
removed
2
removed
1
removed
答案 0 :(得分:3)
Document#remove
似乎只适合我...
我更喜欢在这种情况下使用Document#remove
的原因是因为它不会创建一堆临时String
,从而提高效率,但(更轻微)更快;)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class Test1 {
public static void main(String[] args) {
new Test1();
}
public Test1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
final JTextArea ta = new JTextArea(10, 60);
ta.setText("Bananas in pajamas are coming down the stairs,"
+ "\n"
+ "Bananas in pajamas are coming down in pairs,");
JButton back = new JButton("Backspace");
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Document doc = ta.getDocument();
if (doc.getLength() > 0) {
doc.remove(doc.getLength() - 1, 1);
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(ta));
frame.add(back, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
考虑提供展示您问题的runnable example。这将减少混淆和更好的响应
<强>更新强>
问题在于你的循环......
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[i].length; j++) {
//...
else if (ae.getSource() == buttons[0][3]) {
try {
Document doc = entryTA.getDocument();
if (doc.getLength() > 0) {
doc.remove(doc.getLength() - 1, 1);
}
} catch (BadLocationException ble) {
ble.printStackTrace();
}
} //clear - works, as per @Frostsoft's solution
else if (ae.getSource() == buttons[0][4]) {
entryTA.setText("");
}
}
}
在循环的每次迭代中,当按下后退空格按钮时,else if (ae.getSource() == buttons[0][3]) {
将为真......当按下清除按钮时,else if (ae.getSource() == buttons[0][4]) {
也是如此
相反,从循环中删除清除和退格检查...
public void actionPerformed(ActionEvent ae) {
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[i].length; j++) {
if (ae.getSource() == buttons[i][j]) {
if (ae.getSource() != buttons[0][3]
&& ae.getSource() != buttons[0][4]) {
entryTA.append(names[i][j]);
}
}
}
}
if (ae.getSource() == buttons[0][3]) {
try {
Document doc = entryTA.getDocument();
if (doc.getLength() > 0) {
doc.remove(doc.getLength() - 1, 1);
}
} catch (BadLocationException ble) {
ble.printStackTrace();
}
} else if (ae.getSource() == buttons[0][4]) {
entryTA.setText("");
}
}
答案 1 :(得分:1)
我会尝试entryTA.setText("");
清除TextArea,然后使用entryTA.setText = (entryTA.getText()).substring(0, entryTA.getText().length()-1)
删除TextArea中的最后一个字符。这只是我头脑中的代码,但它应该可以正常工作。