2013-06-29 109 views
2

是否有一種方法會將字符串添加到JLabel中,而無需編輯已存在的內容?我知道setText方法,但在我的程序中,一個計算器,我想將按鈕添加到JLabel中,而不是覆蓋已有的按鈕。我在做這個對嗎?這裏是我的代碼(如果需要的話):添加到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(); 


    } 
} 
+1

使用GridLayout,並替換JFormattedTextField(使用數字格式化程序)而不是JLabel – mKorbel

回答

3

剛剛拿到原單文本,添加"blah"(或其他任何東西),並設置了回去。

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

謝謝!那創造了奇蹟 – Col1107

2

請勿使用真正用於顯示文本的JLabel。相反,您可以使用旨在進行更新的不可編輯的JTextField。然後你可以使用:

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

和字符會被追加到文本字段的末尾。

另外,不要使用null佈局和setBounds()方法。 Swing旨在與佈局經理一起使用。