在小程序中添加组件

发布于 2024-11-19 23:36:14 字数 1082 浏览 2 评论 0原文

我正在制作一个小程序,作为我的小程序的一部分,我希望发生这种情况:当用户按“确定”时,旧组件(一些单选按钮)将被删除,并添加一个新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怎言笑 2024-11-26 23:36:15

您不应该重写 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 its remove method) and add the label in the applet in your actionPerformed method, the same way you add them in the init method.

You might have to call validate after.

北城半夏 2024-11-26 23:36:15

添加组件,然后调用容器的 validate()。在本例中yourApplet.validate()。这将触发所有元素的重新绘制和重新排列。

Add components and then call validate() of your container. In this case yourApplet.validate(). This will trigger repainting and rearranging of all elements.

淤浪 2024-11-26 23:36:15

你可以做类似的事情

JFrame fr= new JFrame();                    // global variables
JPanel panelToBeAdded = new JPanel();
JPanel initialPanel = new JPanel();
JTextField fieldToBeAdded = new JTextField(); 
panelToBeAdded.setPreferredSize( new Dimension(400,400));
initialPanel.setPreferredSize( new Dimension(400,400));
initialPanel.setVisible(true);
fr.add(initialPanel);
fr.setVisible(true);
fr.pack();


public void actionPerformed(ActionEvent ae) {
    initialPanel.setVisible(false);
    //radiobuttons.setVisible(false);---> hide the radio buttons
    panelToBeAddedd.add(fieldToBeAddedd);
    panelToBeAddedd.setVisible(true);
    fr.add(panelToBeAddedd);

}

public void repaint( Graphics g ) {
  // do something
} 

you could do something like

JFrame fr= new JFrame();                    // global variables
JPanel panelToBeAdded = new JPanel();
JPanel initialPanel = new JPanel();
JTextField fieldToBeAdded = new JTextField(); 
panelToBeAdded.setPreferredSize( new Dimension(400,400));
initialPanel.setPreferredSize( new Dimension(400,400));
initialPanel.setVisible(true);
fr.add(initialPanel);
fr.setVisible(true);
fr.pack();


public void actionPerformed(ActionEvent ae) {
    initialPanel.setVisible(false);
    //radiobuttons.setVisible(false);---> hide the radio buttons
    panelToBeAddedd.add(fieldToBeAddedd);
    panelToBeAddedd.setVisible(true);
    fr.add(panelToBeAddedd);

}

public void repaint( Graphics g ) {
  // do something
} 
半世蒼涼 2024-11-26 23:36:15

我做错了什么?

您的 repaint(Graphics) 方法与您在 actionPerformed 方法中调用的方法不同。

另外,对于添加新组件的方法来说,repaint 是一个非常糟糕的名称。

 public void swapComponents() 
 { 
     if (radio1.getState()) {
        remove(radio1);
        remove(radio2);
        remove(radio3);
        add(j);
        validate();
     }
 }

 public void actionPerformed(ActionEvent evt)  
 { 
     if (evt.getSource() == okButton) {
        swapComponents();
     }
 }

What am I doing wrong?

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.

 public void swapComponents() 
 { 
     if (radio1.getState()) {
        remove(radio1);
        remove(radio2);
        remove(radio3);
        add(j);
        validate();
     }
 }

 public void actionPerformed(ActionEvent evt)  
 { 
     if (evt.getSource() == okButton) {
        swapComponents();
     }
 }
灯角 2024-11-26 23:36:15

当用户按下“确定”时,旧组件(一些单选按钮)将被删除,并添加一个新的 JPanel,其中包含一堆文本字段。

使用CardLayout,如图此处。它非常适合这种情况。

游戏视图

When the user presses "OK", the old components (some radio buttons) are removed, and a new JPanel is added, with a bunch of textfields.

Use a CardLayout, as shown here. It is perfect for situations like this.

Game view High Scores view

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文