简体   繁体   中英

Add to JLabel method?

Is there a method that will add a string to JLabel without editing what was already there? I know about the setText method but in my program, a calculator, I want to add the button clicked to the JLabel with out overriding what is already there. Im I doing this right? Here's my code (if you need it):

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();


    }
}

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

results.setText(results.getText() + "blah");

Don't use a JLabel which is really designed to just display text. Instead you can use a non-editable JTextField which is designed to be updated. Then you can just use:

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

and the character will be appended to the end of the text field.

Also, don't use a null layout and the setBounds() method. Swing was designed to be used with layout managers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM