小编twi*_*ing的帖子

在try catch中访问变量

我一直在返回menuFont行上遇到编译错误,它说没有变量menuFont.有人可以告诉我如何解决这个问题.

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class loadFiles {
Font getFont(){
            try {
                Font menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));

            } catch (FileNotFoundException e) {
                System.out.println("Cant find file.");
                e.printStackTrace();
            } catch (FontFormatException e) {
                System.out.println("Wrong file type.");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("Unknown error.");
                e.printStackTrace();
            }
            return menuFont;
    }
}
Run Code Online (Sandbox Code Playgroud)

java try-catch

6
推荐指数
1
解决办法
4万
查看次数

如何重新运行paint方法以使JPanel动画化?

我想我需要在注释所在的位置放一些代码(或者使用非静态方法,但我不确定).main方法创建窗口,然后启动图形方法.我希望蓝色方块能够闪现.

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class paintTest extends JPanel{
private static JFrame theWindow = new JFrame("Window");
static boolean blueSqr = false;

public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.fillRect(10, 10, 10, 10);

    if(blueSqr){
        g.setColor(Color.BLUE);
        g.fillRect(10, 10, 10, 10);
    }
}

public static void main(String[] args){
    createWindow();
    theWindow.getContentPane().add(new paintTest());
    while(true){
        blueSqr = false;

        System.out.println("off");

        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}

        blueSqr = true;
        // Needs something here
        System.out.println("on");

        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
}

public …
Run Code Online (Sandbox Code Playgroud)

java swing jpanel

3
推荐指数
1
解决办法
1923
查看次数

标签 统计

java ×2

jpanel ×1

swing ×1

try-catch ×1