2

有没有一种方法可以将字符串添加到 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();


    }
}
4

2 回答 2

3

只需获取原始文本,添加"blah"(或其他任何内容),然后将其设置回来。

results.setText(results.getText() + "blah");
于 2013-06-29T05:15:17.673 回答
2

不要使用真正设计为仅显示文本的 JLabel。相反,您可以使用设计为更新的不可编辑的 JTextField。然后你可以使用:

String name = al.getActionCommand();
textField.replaceSelection(name);

并且该字符将附加到文本字段的末尾。

此外,不要使用空布局和 setBounds() 方法。Swing 旨在与布局管理器一起使用。

于 2013-06-29T15:01:47.553 回答