2014-03-28 28 views
0

如何在點擊後創建標籤? (我必須在actionPerformed方法內創建一個標籤,不要問我爲什麼)ty!如何在點擊後在actionPerformed內創建標籤?

public static void main (String [] args) 
{ 
     JFrame Frame = new JFrame(); 
     Frame.setSize(WIDTH_FRAME,HEIGHT_FRAME); 
     Frame.setLayout(null); 
     JButton Button = new JButton("x"); 
     Button.setBounds(a,b,c,d); 
     Button.addActionListener(new ActionListener() { 
           public void actionPerformed(ActionEvent e) { 

      JLabel v = new JLabel ("xxxxxxxxxx"); 
      v.setBounds(50,50,50,50); 
      Frame.add(v); 
      Frame.revalidate(); 
      Frame.repaint(); 
      } 
     }); 

     Frame.add(Button); 
     Frame.setVisible(true); 
+0

*「不要問我爲什麼。」 *爲什麼呢? (並且不要告訴我/我們該做什麼。) –

+0

遵循Java命名約定。變量名稱不應以大寫字符開頭。 – camickr

回答

2

你是否理解範圍的概念? JLabel v在本地範圍內,不能從actionPerformed以外訪問。您可以將Frame.add(v);放入actionPerformed。然後,你需要revalidate()repaint()框架,你應該做的,當在運行時添加組件


旁註

  • 空佈局引起許多問題,所以你應該考慮使用佈局經理。請參閱Laying out Components Within a Container瞭解更多詳情。

  • Swing應用程序應該在Event Dispatch Thread上運行。您可以通過將main中的代碼包裝在SwingUtilities.invokeLater(...)中來完成此操作。查看更多內容Initial Threads

  • 請注意您的硬編碼值setBounds。這會導致只有一個添加的標籤可見。我強烈建議尋找像FlowLayoutBoxLayout這樣的佈局管理器。一個佈局,將動態添加組件「自然」


與盒佈局

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.Box; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 

public class BoxLayoutDemo { 

    private Box box; 
    private int count = 1; 
    public BoxLayoutDemo() { 
     box = Box.createVerticalBox(); 
     JButton button = createButton(); 

     JScrollPane scroll = new JScrollPane(box); 
     scroll.setPreferredSize(new Dimension(200, 300)); 
     JFrame frame = new JFrame(); 
     frame.add(scroll); 
     frame.add(button, BorderLayout.NORTH); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private JButton createButton() { 
     JButton button = new JButton("Add Label"); 
     button.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       box.add(new JLabel("JLabel " + count)); 
       box.add(Box.createVerticalStrut(10)); 
       box.revalidate(); 
       box.repaint(); 
       count++; 
      } 
     }); 
     return button; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       new BoxLayoutDemo(); 
      } 
     }); 
    } 
} 
+0

我試過了,但它不起作用... – user3456426

+0

你需要調用'Frame.revalidate(); Frame.repaint();'在運行時添加組件。 –

+0

你能舉個例子嗎? – user3456426

2

你似乎並不有一個事件驅動環境的概念,並以程序的方式思考...

本例...

Button.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     JLabel v = new JLabel ("xxxxxxxxxx"); 
     v.setBounds(50,50,50,50); 
    } 
}); 

不打電話不是由時間產生的actionPerformed方法時,它的執行,有脫穎而出v你打

Frame.add(v); // this does not work 

儘管v具有本地內容的actionPerformed方法,不能外部引用。

actionPerformed只會在Button在某些支付行爲(即用戶點擊它)時被調用。

相反,你應該做更多的東西一樣......

Button.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     JLabel v = new JLabel ("xxxxxxxxxx"); 
     v.setBounds(50,50,50,50); 
     Frame.add(v); 
    } 
}); 

但現在你有另外一個問題,Frame只在本地範圍內的main方法。您可以通過聲明Framefinal ...

final JFrame Frame = new JFrame(); 

注:

許多這樣迭代,並支持peeskillet(+1),並做是因爲它是重要的,讓這個答案

  • 不要使用null佈局。 Swing被設計爲與佈局管理器一起工作,如果沒有這些佈局管理器,您將無法完成更新屏幕的任務,除此之外,像素完美的佈局是現代用戶界面設計中的幻想,您不控制字體,渲染管線或其他目標系統可能會影響文本大型元素的渲染方面。
  • 通過閱讀並理解Initial Threads
  • 通過閱讀和使用Code Conventions for the Java Programming Language
+0

我把最後,但當我點擊按鈕,什麼都不顯示。我也嘗試刪除Frame.setLayout(null);,但按鈕變得超大.... – user3456426

+0

嘗試設置佈局管理器爲'FlowLayout'('Frame.setLayout(new FlowLayout())',添加'Frame。在添加標籤後重新驗證()'和可能的'Frame.repaint()'。 – MadProgrammer

相關問題