2017-06-28 45 views
2

我試圖在每次單擊按鈕時創建標籤。標籤的位置應該改變。每次點擊時它應該高於另一個。但它不起作用。不創建任何標籤。我甚至不能從按鈕點擊偵聽器代碼創建單個JLabel。但是執行發生在最內層的'if'聲明中。動態地將JLabel添加到java中的UI上使用單個對象進行按鈕單擊

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
class ChatClient{ 
static int y=600; 
static JLabel label=null; 
public static void main(String args[]) throws Exception{ 
    String defaultMessage = "Enter your message.."; 
    JFrame frame = new JFrame("FireFly"); 
    JTextField text = new JTextField(defaultMessage); 
    //manipulate the default message in the text field 
    text.addFocusListener(new FocusListener(){ 
    public void focusGained(FocusEvent ae){ 
     if(text.getText().equals(defaultMessage)){ 
      text.setText(""); 
     } 
    } 
    public void focusLost(FocusEvent ae){ 
     if(text.getText().isEmpty()){ 
     text.setText(defaultMessage); 
     } 
    } 
    }); 
    text.setBounds(10,620,295,40); 
    frame.add(text); 
    JButton button = new JButton("SEND"); 
    button.setBounds(310,620,80,40); 
    button.setForeground(Color.WHITE); 
    button.setBackground(Color.decode("#11A458")); 
    button.setFocusPainted(false); 
    button.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent ae){ 
     if(!text.getText().equals(defaultMessage)) 
     { 
     if(!text.getText().isEmpty()){ 
      label=new JLabel(text.getText()); 
      label.setBounds(10,y,380,20); 
      y=y-20; 
      frame.add(label); 
      } 
     } 
    } 
    }); 

    frame.add(button); 
    frame.setSize(400,700); 
    frame.setLayout(null); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 
} 
+1

你需要調用'重新驗證()'和'重繪()'在容器上你從中添加或刪除組件的任何時間。你的第一個嚴重錯誤是使用空佈局 - 避免這些因爲他們會回來困擾你,保證。 –

+2

另外,我不得不懷疑,如果您最好使用JList。 –

回答

2

調用重新驗證並添加您的JLabel會告訴GUI來定位新的組件和重繪容器,以便它顯示了在重畫在容器上。例如,

public void actionPerformed(ActionEvent ae) { 
    if (!text.getText().equals(defaultMessage)) { 
     if (!text.getText().isEmpty()) { 
      label = new JLabel(text.getText()); 
      label.setBounds(10, y, 380, 20); 
      y = y - 20; 
      frame.add(label); 
      frame.revalidate(); 
      frame.repaint(); 
     } 
    } 
} 

在你的情況,因爲你不使用佈局管理器,但它仍然是一個好主意,包括它的重新驗證是完全沒有必要,因爲你應該使用佈局管理器作爲被零布局是危險和脆弱的。

雖然使用JList更好。

例如,

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class ChatClient2 extends JPanel { 
    private static final int VISIBLE_ROW_COUNT = 30; 
    private DefaultListModel<String> listModel = new DefaultListModel<>(); 
    private JList<String> chatList = new JList<>(listModel); 
    private SendAction sendAction = new SendAction("Send"); 
    private JTextField textField = new JTextField(20); 
    private JButton sendButton = new JButton(sendAction); 

    public ChatClient2() { 
     chatList.setFocusable(false); 
     chatList.setVisibleRowCount(VISIBLE_ROW_COUNT); 
     JScrollPane scrollPane = new JScrollPane(chatList); 

     textField.setAction(sendAction); 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS)); 
     bottomPanel.add(textField); 
     bottomPanel.add(sendButton); 

     setLayout(new BorderLayout()); 
     add(scrollPane); 
     add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    private class SendAction extends AbstractAction { 
     public SendAction(String name) { 
      super(name); 
      int mnemonic = (int) name.charAt(0); 
      putValue(MNEMONIC_KEY, mnemonic); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      String text = textField.getText(); 
      listModel.add(0, text); 
      textField.selectAll(); 
      textField.requestFocusInWindow(); 
     } 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Chat Client2"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new ChatClient2()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 
+0

它的工作。謝謝。 – draner

+0

@draner:不客氣 –

相關問題