开发者

Can I add elements to a Java GUI?

I want to know how I can make a Java program where an unknown amount of objects can be added to a GUI depending on user input. I can program in objects one at a time within the program, but I haven't seen a more dynamic program.

Can I do that with Java? If not, what can I do it with?

For more information, here's a picture.

Can I add elements to a Java GUI?

There can be more than one question per question block, and each question can have开发者_JAVA技巧 it's own question block.


Yes you can dynamically add and remove components. The basic code would be:

panel.add( ... );
panel.revalidate();
panel.repaint();


Ofcourse you can do it with Java Swings. All you gotta do is based on user input you gotta take a decision to add new JPanels. From the picture you've given in the example, you would need to add a Q&A block dynamically. Simply attach that to any event handler within your application so that it gets added dynamically

public getQandAPanel(){
    JPanel questPanel = new JPanel();
    JPanel answerPanel = new JPanel();
    JPanel wrappingPanel = new JPanel();
    wrappingPanel.setLayout(new GridLayout(0,1));
    //CODE TO DECORATE question and answer panels should go here
    wrappingPanel.add(questPanel);
    wrappingPanel.add(answerPanel);
}

Now everytime when you call this getQandAPanel, this would return you a fresh JPanel everytime which you can add it to your parent JFrame. You should have a good idea of Java Swings to know what I'm talking about.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜