2011-12-01 62 views
2

我正在研究一個程序,其中我使用了JTextArea,JButton,JLabelJPanel
我要實現的邏輯是:用戶在給定的textArea中鍵入文本,然後單擊button。點擊按鈕後,我將從textArea中檢索文本,並創建一個帶有書寫文本的label(如textArea),並將其顯示在panel上。
我之前所做的一切都是正確的,但問題出在labelpanellabelpanel上不可見。JLabel在Jpanel上不可見

的代碼片段是:

import java.awt.BorderLayout; 
import java.awt.HeadlessException; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.ScrollPaneConstants; 
import javax.swing.border.BevelBorder; 

/** 
* 
* @author mohammadfaisal 
* http://ermohammadfaisal.blogspot.com 
* http://facebook.com/m.faisal6621 
* 
*/ 
public class CodeMagnets extends JFrame{ 
    private JTextArea area4Label; 
    private JLabel codeLabel; 
    private JButton createButton; 
    private JPanel magnet; 

    public CodeMagnets(String title) throws HeadlessException { 
     super(title); 
     magnet=new JPanel(null); 
     JScrollPane magnetScroller=new JScrollPane(magnet); 
       magnetScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     magnetScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     add(BorderLayout.CENTER, magnetScroller); 
     JPanel inputPanel=new JPanel(); 
     area4Label=new JTextArea(5, 30); 
     area4Label.setTabSize(4); 
     JScrollPane textScroller=new JScrollPane(area4Label); 
     inputPanel.add(textScroller); 
     createButton=new JButton("Create code magnet"); 
     createButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       //codeLabel=new JLabel(area4Label.getText()); 
       codeLabel=new MyLabel(area4Label.getText());//this is for my new question 
       codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 
       codeLabel.setLocation(50, 20); 
       codeLabel.setVisible(true); 
       magnet.add(codeLabel); 
       area4Label.setText(""); 
       //pack(); 
      } 
     }); 
     inputPanel.add(createButton); 
     add(BorderLayout.SOUTH, inputPanel); 
     //pack(); 
     setSize(640, 480); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public static void main(String[] args) { 
     new CodeMagnets("Code Magnets"); 
    } 
} 

回答

4

你需要它動態地添加新的組件後重繪()/驗證()你panle。所以,在此之後:

magnet.add(codeLabel); 

補充一點:

magnet.validate(); 

magnet.repaint(); 

另外一件事,你正在使用的磁鐵面板空佈局。因此,在將其添加到磁盤面板之前,必須先設置jLable的setBounds()。因此,它成爲

public void actionPerformed(ActionEvent e) { 
    codeLabel=new JLabel(area4Label.getText()); 
    codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 
    codeLabel.setBounds(50, 20, 100, 100); 
    magnet.add(codeLabel); 
    magnet.repaint(); 
    area4Label.setText(""); 
} 

不建議使用null佈局,您應該根據您的需要使用適當的佈局像BorderLayout的或網格佈局,甚至簡單的FlowLayout。


至於說通過@Andrew使用類似:的

codeLabel.setSize(codeLabel.getPreferredSize()); 
codeLabel.setLocation(50, 20); 

代替

codeLabel.setBounds(50, 20, 100, 100); 

這將解決的JLabel的大小問題。

+0

增加'magnet.validate()'但仍然無法正常工作。 'reapint()'和'validate()'都不起作用。我會在''面板'上顯示'label'。 –

+1

@MohammadFaisal查看我的編輯。 –

+0

我已經使用了'setLocation()'現在已經足夠了,因爲'setBounds()'可以修剪文本。 'setBounds()'也不起作用。我請求你通過執行來檢查代碼。 –