2013-04-06 67 views
0

我有一个简单的代码在这里添加一个标签被点击后。它工作正常,但为了添加标签,我必须在点击按钮后拖动或重新调整窗口大小。Java按钮添加标签后被重新或拖动或其他东西

这里是我的代码:

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 

public class server01 extends Applet implements ActionListener { 
    Label helloLabel = new Label("applet v 0.0.1 | created for testing purpose"); 
    Label hello2Label = new Label("this applet will be up-to-date."); 
    Button buttonButton = new Button("START" + " Button"); 
    Label buttonLabel = new Label("Starting server..."); 

    private static final long serialVersionUID = 1L; 

    public void init() { 
     setBackground(Color.black); 
     setForeground(Color.white); 
     buttonButton.setForeground(Color.black); 
     add(helloLabel); 
     add(hello2Label); 
     add(buttonButton); 
     buttonButton.addActionListener(this); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == buttonButton) { 
      add(buttonLabel); 
     } 
    } 
} 
+1

你会下载陌生人指向的文件吗?如果你想要一些帮助代码,然后将该代码作为纯文本发布。也尝试使[SSCCE](http://sscce.org/)。 – Pshemo 2013-04-06 11:59:45

+0

我在帖子中说过,我不能因为某些原因而不愿意接受它。也许它太长或什么? – 2013-04-06 12:04:54

+0

虐待尝试发布它 – 2013-04-06 12:05:19

回答

1

你需要调用GUI进行更改后的验证方法,以使小程序可以检查它是否仍然正确呈现。 做一个调整大小基本上会做同样的事情。

public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == buttonButton) { 
      add(buttonLabel); 
      validate(); 
     } 
} 
+0

让我看看它是否有效 – 2013-04-06 13:01:31

+0

是的,它的工作原理谢谢! – 2013-04-06 13:01:56

1

在actionPerformed()中使用repaint()方法 - (在方法结尾处)方法。 它将重新绘制小程序窗口,并将再次运行添加您的标签。

public void actionPerformed(ActionEvent ae) 
{ 
    /* 
     your code here.. 
    */ 
    repaint(); 
}