有没有一种方法可以将字符串添加到 JLabel 而不编辑已经存在的内容?我知道 setText 方法,但是在我的程序(计算器)中,我想将单击的按钮添加到 JLabel 中,而不覆盖已经存在的内容。我这样做对吗?这是我的代码(如果需要):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
public class calc extends JFrame implements ActionListener {
private JPanel contentPane;
public JLabel results;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
calc frame = new calc();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public calc() {
setTitle("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 255, 179);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
panel.setToolTipText("Numbers will appear here once clicked!");
panel.setBackground(Color.WHITE);
contentPane.add(panel, BorderLayout.NORTH);
results = new JLabel("");
panel.add(results);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(null);
JButton one = new JButton("1");
one.addActionListener(this);
one.setBounds(6, 19, 61, 29);
panel_1.add(one);
JButton two = new JButton("2");
two.addActionListener(this);
two.setBounds(67, 19, 61, 29);
panel_1.add(two);
JButton three = new JButton("3");
three.addActionListener(this);
three.setBounds(127, 19, 61, 29);
panel_1.add(three);
JButton four = new JButton("4");
four.addActionListener(this);
four.setBounds(6, 48, 61, 29);
panel_1.add(four);
JButton five = new JButton("5");
five.addActionListener(this);
five.setBounds(67, 48, 61, 29);
panel_1.add(five);
JButton six = new JButton("6");
six.addActionListener(this);
six.setBounds(127, 48, 61, 29);
panel_1.add(six);
JButton seven = new JButton("7");
seven.addActionListener(this);
seven.setBounds(6, 75, 61, 29);
panel_1.add(seven);
JButton eight = new JButton("8");
eight.addActionListener(this);
eight.setBounds(67, 75, 61, 29);
panel_1.add(eight);
JButton nine = new JButton("9");
nine.addActionListener(this);
nine.setBounds(127, 75, 61, 29);
panel_1.add(nine);
JButton zero = new JButton("0");
zero.addActionListener(this);
zero.setBounds(6, 102, 122, 29);
panel_1.add(zero);
JButton decimal = new JButton(".");
decimal.addActionListener(this);
decimal.setBounds(127, 102, 61, 29);
panel_1.add(decimal);
JButton add = new JButton("+");
add.addActionListener(this);
add.setBounds(184, 19, 61, 29);
panel_1.add(add);
JButton sub = new JButton("-");
sub.addActionListener(this);
sub.setBounds(184, 48, 61, 29);
panel_1.add(sub);
JButton times = new JButton("x");
times.addActionListener(this);
times.setBounds(184, 75, 61, 29);
panel_1.add(times);
JButton div = new JButton("\u00F7");
div.addActionListener(this);
div.setBounds(184, 102, 61, 29);
panel_1.add(div);
JSeparator separator = new JSeparator();
separator.setBounds(6, 6, 239, 12);
panel_1.add(separator);
}
public void actionPerformed(ActionEvent al) {
String name = al.getActionCommand();
}
}