首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >repaint()不更新JPanel

repaint()不更新JPanel
EN

Stack Overflow用户
提问于 2018-11-14 18:44:48
回答 1查看 69关注 0票数 0

因此,我制作了一个程序,其中有一个GUI,用户可以在那里输入烟花发射的参数,在屏幕中央有一个面板,当按下按钮时,启动应该被绘制出来。然而,在我的程序中,JPanel没有更新,也没有绘制任何内容。代码如下

代码语言:javascript
复制
//This is the method that build the JPanel in the center for the launch
public JPanel buildCenter() {
    JPanel center=new JPanel();
    center.setBackground(Color.black);
    center.setVisible(true);
    return center;
}


//This is the method the build the GUI, the buttons and such are in the other panels labeled top, west, east, etc.
public void buildGUI(){
    configureSliders();
    configureRadioButtons();
    JFrame frame=new JFrame();
    JPanel panel=new JPanel();
    JPanel top=new JPanel();
    panel.setLayout(new BorderLayout());
    Fireworks.setFont(new Font("Helvetica", Font.BOLD, 30));
    frame.setLayout(new BorderLayout());
    top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
    top.add(Box.createHorizontalGlue());
    top.add(Fireworks);
    top.add(Box.createHorizontalGlue());
    frame.add(top, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(1920,1080);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    panel.add(buildCenterTop(),BorderLayout.NORTH);
    panel.add(buildCenter(), BorderLayout.CENTER);
    panel.add(buildwest(), BorderLayout.WEST);
    panel.add(buildeast(),BorderLayout.EAST);
    panel.add(Launch, BorderLayout.SOUTH);
    Launch.addActionListener(this);
}
//this is the action performed method where repaint won't work. fire is my fireworks object with the paintcomponent method for the launch.
@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(Launch)) {
        setColor();
        setTime();
        setExplosion();
        fire.setVelocity(veloslider.getValue());
        fire.setTheta(thetaslider.getValue());
        buildCenter().add(fire);
        buildCenter().repaint();
        buildCenter().validate();

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-14 20:08:08

buildCenter()方法所做的就是创建一个带有黑色背景的空面板。

然后将这个空面板添加到框架中:

代码语言:javascript
复制
panel.add(buildCenter(), BorderLayout.CENTER);

那么在您的ActionListener中,您需要:

代码语言:javascript
复制
buildCenter().add(fire);
buildCenter().repaint();
buildCenter().validate();

所做的就是再创建3个空面板。你不想再创建3个面板。要将组件添加到现有面板中。

您需要做的是创建“中心”面板的单个实例,然后保留一个引用该面板的变量,以便将来可以更新面板。

因此,需要在类中定义一个实例变量:

代码语言:javascript
复制
private JPanel centerPanel;

然后在buildGui()方法中创建面板:

代码语言:javascript
复制
//panel.add(buildCenter(), BorderLayout.CENTER);
centerPanel = buildCenter();
panel.add(buildCenter, BorderLayout.CENTER);

然后,在您的ActionListener中,您可以向中心面板添加组件:

代码语言:javascript
复制
//buildCenter().add(fire);
//buildCenter().repaint();
//buildCenter().validate();
centerPanel.add( fire );
centerPanel.revalidate();
centerPanel.repaint();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53306855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档