gpt4 book ai didi

java - JTextArea 退格和清除

转载 作者:行者123 更新时间:2023-12-01 18:28:08 24 4
gpt4 key购买 nike

我正在开发一个计算器 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 为 false。但是,删除条件会导致引发异常。

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

最佳答案

Document#remove 似乎只适合我......

在这种情况下我更喜欢使用 Document#remove 的原因是因为它不会创建一堆临时 String,从而提高效率,但是(永远)稍微)快一点;)

backspace

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]) { will be true...,这同样适用于 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("");
}

}

关于java - JTextArea 退格和清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com