第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何延遲 Java 代碼直到另一部分代碼完成?

如何延遲 Java 代碼直到另一部分代碼完成?

繁華開滿天機 2022-06-23 16:47:26
所以我嘗試了 java.awt.Graphics 庫并在我的代碼中遇到了一個問題。下面的代碼返回空指針異常,因為在執(zhí)行時未定義 Graphics 對象“g”。我可以循環(huán)調(diào)用并檢查 g != null 但是否有更好的方法來執(zhí)行此操作?謝謝你的幫助。這是我的代碼:package gui;import java.awt.Color;import java.awt.Graphics;import javax.swing.JFrame;import javax.swing.JPanel;@SuppressWarnings("serial")public class Gui extends JPanel{Graphics g;public Gui() {    JFrame frame = new JFrame("test");    frame.setSize(700, 700);    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.setVisible(true);    frame.add(this);}public void paintComponent(Graphics g) {    super.paintComponent(g);    this.g = g;    g.setColor(Color.blue);    for(int x = 0;x < 700; x += 20)     {        g.drawLine(x, 0, x, 700);    }    for(int y = 0;y < 700; y += 20)     {        g.drawLine(0, y, 700, y);    }}public void draw(Tuple xy) {    g.setColor(Color.blue);   // <--- null pointer exception    g.fillOval(xy.x, xy.y, 5, 5);}}和主要課程:package gui;public class Main {public static void main(String[] args){    new Gui().draw(new Tuple(200,200)); //Tuple is a custom class I wrote}}
查看完整描述

3 回答

?
qq_花開花謝_0

TA貢獻1835條經(jīng)驗 獲得超7個贊

找到了我的空問題的解決方案。我修改了方法“draw()”來接受一個元組,然后將它傳遞給paintComponent()函數(shù)并調(diào)用repaint()。


package gui;


import java.awt.Color;

import java.awt.Graphics;


import javax.swing.JFrame;

import javax.swing.JPanel;


@SuppressWarnings("serial")

public class Gui extends JPanel{


    Graphics g;

    Tuple xy;


    public Gui() 

    {

        JFrame frame = new JFrame("test");

        frame.setSize(700, 700);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

        frame.setLocationRelativeTo(null);

        frame.add(this);

    }


    public void paintComponent(Graphics g) 

    {

        super.paintComponent(g);

        this.g = g;


        g.setColor(Color.blue);


        for(int x = 0;x < 700; x += 20) 

        {

            g.drawLine(x, 0, x, 700);

        }


        for(int y = 0;y < 700; y += 20) 

        {

            g.drawLine(0, y, 700, y);

        }


        if(xy != null) 

        {

            g.fillOval(xy.x, xy.y, 5, 5);

        }

    }


    public void draw(Tuple xy)

    {

        this.xy = xy;

        repaint();

    }


}

謝謝你的幫助


查看完整回答
反對 回復(fù) 2022-06-23
?
當(dāng)年話下

TA貢獻1890條經(jīng)驗 獲得超9個贊

你應(yīng)該把所有的畫都畫在

public void paintComponent(Graphics g)

方法如https://docs.oracle.com/javase/tutorial/uiswing/painting/problems.html所述。


查看完整回答
反對 回復(fù) 2022-06-23
?
慕婉清6462132

TA貢獻1804條經(jīng)驗 獲得超2個贊

使用回調(diào)函數(shù)。比polling物業(yè)好很多。你得到一個新的圖形對象。并且您的調(diào)用是在適當(dāng)?shù)纳舷挛闹袌?zhí)行的。


這是一般的想法:


實例化

new Gui((Graphics graphics)-> {

   // your code here

});

CTOR 或初始化函數(shù)

public Gui(GraphicsCallback callback) 

調(diào)用

調(diào)用回調(diào)。您可以檢查是否被調(diào)用,如果適合您的用例,則僅回調(diào)一次。或者回調(diào)實現(xiàn)可以管理多次調(diào)用。


public void paintComponent(Graphics g) 

{

    super.paintComponent(g);

    this.g = g;

    this.callback(g)


查看完整回答
反對 回復(fù) 2022-06-23
  • 3 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號