【问题标题】:Proper method for creating splashscreen with drawImage(g)使用 drawImage(g) 创建启动画面的正确方法
【发布时间】:2015-04-25 16:01:06
【问题描述】:

我正在学习构建一个 JApplet,尽管到目前为止还没有深入了解。我有一个图像(.png),理想情况下,它会在“绘制”界面之前由paintComponent(s)绘制,或者更确切地说是从paint()方法中添加。

有趣的是,JApplet 仅在被 appletviewer 克隆后才按预期加载。似乎我缺少对生命周期或运营动态的理解。

这里是代码,swing comp.为简洁起见,已删除定义:

 public class JFriendsPhones extends JApplet implements ActionListener, MouseListener {  

public void init(){


    submit.addActionListener(this);
    queryByName.addActionListener(this);
    queryByNum.addActionListener(this);



    setVisible(true);

    introduction = getGraphics();

    prepareImage(intro, this);
    intro = getImage( getCodeBase(), "introduction.png");

    setVisible(true);

    validate();
}

public void start(){

}

public void paint(Graphics g){

    paintComponent(g);


    }


public void stop(){

}

public void destroy(){

}

public void actionPerformed( ActionEvent ev ){

    Object source = ev.getSource();

    returnLab.setVisible(false);
    resultsPan.setBackground(Color.white);

    if( true ){

        if( queryByName.getText().equals("") != true && queryByNum.getText().equals("") != true ){

                outcomeLab.setText("Search by only one term");
                queryByName.setText("");
                queryByNum.setText("");

        } else if( queryByName.getText().equals("") == true && queryByNum.getText().equals("") == true){
            outcomeLab.setText("Enter search term");

            } else {

                if(queryByName.getText().equals("") == false){

                    for( int i = 0; i < 5; i++ ){
                        if(queryByName.getText().equalsIgnoreCase(friends[i])){
                            resultsPan.setBackground(new Color(190,255,200));
                            outcomeLab.setText("1-result found:");
                            returnLab.setText(frNumbers[i]);    
                            returnLab.setVisible(true);
                            found = true;
                        } 

                    } if(found == false){
                        resultsPan.setBackground(new Color(240,100,100));
                        outcomeLab.setText("0-matches");
                    }
                } else {

                    for( int i = 0; i < 5; i++ ){

                        if(queryByNum.getText().equalsIgnoreCase(frNumbers[i])){
                            resultsPan.setBackground(new Color(190,255,200));
                            outcomeLab.setText("1-result found:");
                            returnLab.setText(friends[i]);  
                            returnLab.setVisible(true);
                            found = true;
                        }                                       
                    }

                        if( found == false){
                                resultsPan.setBackground(new Color(240,100,100));
                                outcomeLab.setText("0-matches");
                        }

        }

              }
    }

    found = false;
}

public void componentResized( ComponentEvent cEv){
        repaint();
}

@Override
public void componentMoved(ComponentEvent e) {
        contain.repaint();

}

@Override
public void componentShown(ComponentEvent e) {

    paintComponent(introduction);

}

@Override
public void componentHidden(ComponentEvent e) {
    // TODO Auto-generated method stub

}

public void mouseClicked( MouseEvent clicked){
    repaint();
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

public void paintComponent(Graphics gr){

    super.paintComponents(gr);

    if(initialized == false){

        gr.drawImage(intro, 10, 10, 280, 430, this);
        validate();
            initialized = true;
        } else {

        titl_instrPan.add(titleLab);
        titl_instrPan.add(instructLab);
        titl_instrPan.add(instructLab2);

        inputPan.add(byNameLab);
        inputPan.add(queryByName);
        inputPan.add(byNumLab);
        inputPan.add(queryByNum);
        inputPan.add(regionPan);
        inputPan.add(submit);

        resultsPan.add(outcomeLab, BorderLayout.NORTH);
        resultsPan.add(returnLab, BorderLayout.SOUTH);
        resultsPan.setBackground(Color.white);

        mainPanel.add(titl_instrPan, BorderLayout.NORTH);
        mainPanel.add(inputPan, BorderLayout.CENTER);
        mainPanel.add(resultsPan, BorderLayout.SOUTH);

        contain.add(mainPanel);
        setVisible(true);
        validate();

        }


} end class JFriendsPhones

【问题讨论】:

标签: java awt lifecycle japplet


【解决方案1】:

你已经通过不调用 super.paint(...) 搞乱了 Swing 的默认绘制机制。

无论如何,不​​要覆盖 JApplet 的paint(..) !!!没有必要这样做。

【讨论】:

  • 诚然,我需要在疲劳的影响下提高解决问题的能力。我已经更正了程序的这一方面,但行为几乎相同。克隆似乎包含了一个线索。
  • I'm learning to construct a JApplet, though as of yet haven't traveled far beneath the surface - 所以从一个包含单个 JLabel 的 JApplet 开始。让它工作。然后在 Applet 上尝试两个组件并使其正常工作。不要尝试第一次从头开始编写完整的小程序。保持代码简单,以便您在遇到问题时可以发布正确的SSCCE。发布随机代码并没有帮助,因为在问题解决之前,您不知道什么代码与问题相关或不相关。
  • 好吧,我确实说过 JApplets,但是我确实有足够的 Java 经验来辨别组件的定义与问题无关。正如我所说,我通过关注何时调用paint方法解决了这个问题。因此没有随机代码,因为添加的内容包括大部分 Japplet 类,相关部分。
【解决方案2】:

在复习了 Graphics 类的基础知识之后,特别是 公共无效油漆() 这是事件驱动的,我在paint方法中添加了一个计数器,以快速了解它在启动时调用了多少次,并使用该数字作为执行drawImage()方法的条件——成功。有趣的一点是,不同的编译器在对paint() 的初始调用中似乎有所不同,例如,Eclipse 的 5 个,javac 1.8 的 3 个。

【讨论】:

  • 这是错误的解决方案。你不应该覆盖paint(...)!!!你的小程序结构错误。
  • 忘了提到它已被更改为使用paint() 本身。当前代码中没有任何内容被覆盖。也许你应该去散散步什么的;想办法放松一下。
  • Forgot to mention it's been changed to use paint() itself. 你不应该直接调用paint()。你是那个问这个问题的人。了解如何解决问题并正确构建代码以避免将来出现问题。您甚至在使用您的方法时声明了您的随机数。
  • 你真的没在听。 JApplet 的生命周期包括对paint() 的事件驱动调用。我将 drawImage() 方法放在这个方法中,在对超级构造函数的调用下。将 JComponents 添加到 GUI 被转移到 init() 方法。没有直接调用,但是正如您所推荐的,paint() 被用作在主 UI 窗口中绘制“启动画面”的方法。
  • 不,你没有在听。你说。 I added a counter to the paint method to quickly learn how many times its called at startup, and used that number as the condition to execute the drawImage() method - 你不应该重写 paint() 并且你当然不应该使用计数器来确定什么时候做某事。这是一种可能有效但将来会给您带来问题的技巧。您可以只将包含图像的标签添加到食欲,或者您覆盖 JPanel 的 paintComponent() 方法并将面板添加到小程序。阅读 Swing 教程。
猜你喜欢
  • 2022-07-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多