如何通过单击按钮使文本(使用drawString)出现在JFrame上?

时间:2018-04-05 03:39:12

标签: java swing user-interface jbutton

我有这个项目,其中我有一个按钮,当我点击它时,一个形状移动10个像素(它的汁液从玻璃杯中啜饮)。在一定的点击间隔(第五次点击,第十次点击等),我想使用drawString方法在JFrame上显示一些文本(类似于漫画人物打人的方式,一个巨大的POW出现在屏幕上。到目前为止,我和# 39;我实例化了代码,以便它(使用System.out.println)将这个单词打印到控制台上。如何使它不是打印到控制台上,而是在框架上显示单词(作为drawString)而不是?

类DrawingFrame:

class ButtonClickListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            JButton click = (JButton) ae.getSource(); 

            if (click==sipButton) //one click on sipButton = juice gets sipped and glass slowly empties 
            {
                clickCounter = clickCounter+1;
                if (clickCounter==5) //on the 5th click (the 5th sip), an expression flashes on screen
                {
                    System.out.println("NICE"); //this works. now turn the console output into a drawString
                }
                else if (clickCounter==10)
                {
                    System.out.println("WOW");
                }
                //...

                dc.nextScene(); //dc = drawing component
            }
            dc.repaint();
        }

class DrawingComponent:

@Override
    protected void paintComponent(Graphics g)
    {
        Graphics2D g2d = (Graphics2D)g;
        AffineTransform reset = g2d.getTransform();

        //...
        //other shapes

        glass.draw(g2d,reset);
        juice.draw(g2d, reset);
        straw.draw(g2d,reset);
    }

    public void nextScene()
    {
        juice.setScene(); //calls for a method in class juice that contains the code for the juice diminishing per click/sip
    }

    //here is where i'm unsure. i don't know if this is right
    //my idea is to call this method in the DrawingFrame class and place it where "System.out.println("NICE") is right now, but i don't know
    public void shouldBeWord(Graphics g)
    {
        Graphics2D g2d = (Graphics2D)g;
        AffineTransform reset = g2d.getTransform();
        g2d.drawString("NICE", 300, 300);
        g2d.setColor(Color.RED);
    }
}

先谢谢了!真的可以使用帮助:)

0 个答案:

没有答案