在小程序中添加组件
我正在制作一个小程序,作为我的小程序的一部分,我希望发生这种情况:当用户按“确定”时,旧组件(一些单选按钮)将被删除,并添加一个新的 JPanel,其中包含一堆文本字段。
但是,我不知道如何在小程序启动后向其添加新组件。我通过忽略删除部分(我知道该怎么做)并仅添加一个简单的 JLabel 来使问题变得更简单,但即使这样也不会添加!
到目前为止,这是我的代码:
// imports omitted
public class Class extends Applet implements ActionListener
{
Button okButton;
CheckboxGroup radioGroup;
Checkbox radio1;
Checkbox radio2;
Checkbox radio3;
JLabel j;
public void init()
{
setLayout(new FlowLayout());
okButton = new Button("OK");
j = new JLabel("hello");
radioGroup = new CheckboxGroup();
radio1 = new Checkbox("Red", radioGroup,false);
radio2 = new Checkbox("Blue", radioGroup,true);
radio3 = new Checkbox("Green", radioGroup,false);
add(okButton);
add(radio1);
add(radio2);
add(radio3);
okButton.addActionListener(this);
}
public void repaint(Graphics g)
{
if (radio1.getState()) add(j);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == okButton) repaint();
}
}
我做错了什么?
I am making an applet and as part of my applet, I want this to happen: When the user presses "OK", the old components (some radio buttons) are removed, and a new JPanel is added, with a bunch of textfields.
However, I cannot figure out how to add a new component to the applet after it has started. I made the problem simpler by ignoring the removal part (Which I know how to do) and just adding a simple JLabel instead, but even that won't add!
Here is my code so far:
// imports omitted
public class Class extends Applet implements ActionListener
{
Button okButton;
CheckboxGroup radioGroup;
Checkbox radio1;
Checkbox radio2;
Checkbox radio3;
JLabel j;
public void init()
{
setLayout(new FlowLayout());
okButton = new Button("OK");
j = new JLabel("hello");
radioGroup = new CheckboxGroup();
radio1 = new Checkbox("Red", radioGroup,false);
radio2 = new Checkbox("Blue", radioGroup,true);
radio3 = new Checkbox("Green", radioGroup,false);
add(okButton);
add(radio1);
add(radio2);
add(radio3);
okButton.addActionListener(this);
}
public void repaint(Graphics g)
{
if (radio1.getState()) add(j);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == okButton) repaint();
}
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不应该重写
repaint
方法,当然也不应该在此方法中添加组件。只需从小程序中删除单选按钮(使用其remove
方法),然后在actionPerformed
方法中将标签添加到小程序中,与在中添加它们的方式相同>init
方法。您可能必须在之后调用
validate
。You shouldn't override the
repaint
method, and certainly not add a component in this method. Just remove the radio buttons from the applet (using itsremove
method) and add the label in the applet in youractionPerformed
method, the same way you add them in theinit
method.You might have to call
validate
after.添加组件,然后调用容器的
validate()
。在本例中yourApplet.validate()
。这将触发所有元素的重新绘制和重新排列。Add components and then call
validate()
of your container. In this caseyourApplet.validate()
. This will trigger repainting and rearranging of all elements.你可以做类似的事情
you could do something like
您的
repaint(Graphics)
方法与您在 actionPerformed 方法中调用的方法不同。另外,对于添加新组件的方法来说,
repaint
是一个非常糟糕的名称。Your
repaint(Graphics)
method is not the same method you are calling in your actionPerformed method.Also,
repaint
is a pretty bad name for a method which is adding a new component.使用CardLayout,如图此处。它非常适合这种情况。
Use a CardLayout, as shown here. It is perfect for situations like this.