我无法在我的Java程序中使用图形在我的面板/画布/窗口上绘制(摆动)。有任何想法吗? [英] I can't get graphics to draw on my panel/canvas/window in my Java program (swing). Any Ideas?

查看:154
本文介绍了我无法在我的Java程序中使用图形在我的面板/画布/窗口上绘制(摆动)。有任何想法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在我的Java程序中使用图形/文本在我的面板/画布/窗口上绘制(使用swing)。

我甚至试图把它分成两类,一个是paintComponent(扩展JPanel),另一个是其他类(扩展JFrame)。

我尝试了一个使用Canvas而没有Canvas的面板(两者结果相同)。



我无法在蓝色区域画任何东西。如果我没有记错的话,当我尝试没有面板时,我确实看到了图形。我已经在文本(无窗口)中成功完成了该程序,甚至还有一个Android应用程序(我认为Android应用程序很难,但与此相比,这很容易)。



我意识到我可以使用JLabel来完成我需要的文本,或者可能是编辑框/编辑文本(无论是什么类似的swing),但我是一个面向图形的人,所以我想知道如何在Java中执行图形(对于任何未来的项目)。



这是我的代码(我只用了4天的Java编码,所以原谅了任何不好的风格,等等。有些变量可能会像全局一样让公共事物试图让它起作用,所以请原谅:-)另外,我最初有单独的程序来创建按钮,菜单等,但后来我把它们全部放在一个程序中以防万一那就是问题所在,所以也请原谅。)

  import java.util.Random; 
import java.util.Date;
import java.io. *;
import javax.swing。*;
import java.awt。*;
import java.awt.geom。*;
import java.awt.event。*;

公共类cookiewin扩展JPanel
{

static int randc = 1;
静态JButton b1,b2;
JMenuBar menubar = null;
static JFrame win = null;
static JPanel panel = null,drawa = null;
static Graphics2D g2n = null;
//静态图形g2 = null;

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
g2.setColor(Color.BLACK);
g2.drawString(This is a test,20,100);
g2.drawLine(10,10,290,290);


protected ImageIcon createImageIcon(String path){
java.net.URL URL = TestPaint.class.getResource(path);
if(URL!= null){
return(new ImageIcon(URL));
} else {
System.err.println(Can not open'+ path +'\\\
);
return(null);



public TestPaint(){
Graphics g3;
容器con;
win = new JFrame(Fortune Cookie);
win.setSize(640,480);
win.setLocation(100,100);
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
win.setDefaultLookAndFeelDecorated(true);
menubar = new JMenuBar();
JMenu menu = new JMenu(Options);
menubar.add(menu);
JMenuItem menuitem = new JMenuItem(Quit);
menuitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.out.println(Menuitem1\\\
);
win.dispose ();
}
});
menu.add(menuitem);
menu.addSeparator();
JMenuItem menuitem2 = new JMenuItem(About);
menuitem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.out.println(Menuitem2\\\
);
}
});
menu.add(menuitem2);
win.setJMenuBar(菜单栏);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
颜色backcol =新颜色(90,0,0);
panel.setBackground(backcol);
con = win.getContentPane();
con.add(panel,BorderLayout.EAST);
drawa = new JPanel();
drawa.setPreferredSize(new Dimension(462,0));
drawa.setBorder(BorderFactory.createLineBorder(Color.yellow,2));
drawa.setBackground(Color.blue);
win.setBackground(Color.magenta);
JPanel panel2 = new JPanel();
panel2.setBackground(backcol);
panel2.setPreferredSize(new Dimension(400,80));
panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
尝试{
InputStream is = new FileInputStream(deng_th_.ttf);
Font font = Font.createFont(Font.TRUETYPE_FONT,is);
颜色textcol =新颜色(255,255,0);
JLabel nulltext = new JLabel();
nulltext.setFont(font.deriveFont(38f));
panel2.add(nulltext);
JLabel titletext = new JLabel(Fortune Cookie:);
titletext.setFont(font.deriveFont(24f));
titletext.setForeground(textcol);
panel2.add(titletext);
} catch(IOException ex){
System.out.println(Font File Error:);
} catch(FontFormatException ex){
System.out.println(Font Error:);
}
con.add(panel2,BorderLayout.NORTH);
JPanel panel3 = new JPanel();
panel3.setBackground(backcol);
panel3.setPreferredSize(new Dimension(400,10));
con.add(panel3,BorderLayout.SOUTH);
JPanel panel4 = new JPanel();
panel4.setBackground(backcol);
panel4.setPreferredSize(new Dimension(10,400));
con.add(panel4,BorderLayout.WEST);
b1 =新JButton(Another);
b1.setToolTipText(Get another fortune cookie);
b1.setPreferredSize(new Dimension(150,48));
b1.setMinimumSize(new Dimension(150,48));
b1.setMaximumSize(new Dimension(150,48));
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.out.println(Button1\\\
);
}
});
ImageIcon b2i = createImageIcon(rand.jpg);
b2 =新JButton(非随机,b2i);
b2.setToolTipText(切换随机选择);
b2.setPreferredSize(new Dimension(150,48));
b2.setMinimumSize(new Dimension(150,48));
b2.setMaximumSize(new Dimension(150,48));
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.out.println(Button2 \ n);
randc = 1 - randc;
if(randc == 0){
b2.setText(Random);
} else {
b2.setText(Non-Random);
}
}
});
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
panel.add(b1);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(b2);
Canvas can = new Canvas();
颜色cancol =新颜色(220,220,220);
can.setBackground(cancol);
drawa.add(can);
con.add(drawa,BorderLayout.CENTER);
win.setIconImage(new ImageIcon(rand.png)。getImage());
win.setVisible(true);


public static void main(String [] args){
int i,numcook = 0,x,y;
int [] cookiepos = new int [500];
Random ranGen2 = new Random();
种子;
文件fp =新文件(cookie.idx);

new TestPaint();



$ b


解决方案

你基本上完全忽略了使用自定义绘画的组件。您无需添加面板即可构建此UI。



这意味着您的 paintComponent 方法永远不会被调用。



将所有UI创建代码从 cookiewin 窗格中取出,并将其放置在其他位置,然后添加 cookiewin 面板进入某个地方...



然后用示例

  public class TestPaint {

protected static ImageIcon createImageIcon(String path){
java.net.URL URL = TestPaint.class.getResource(path);
if(URL!= null){
return(new ImageIcon(URL));
} else {
System.err.println(Can not open'+ path +'\\\
);
return(null);
}
}
私人JFrame获胜;
私人JMenuBar菜单栏;
私人JPanel面板;
私人容器con;
private JPanel drawa;
私人JButton b1;
私人JButton b2;
$ b $ public TestPaint(){

win = new JFrame(Fortune Cookie);
win.setSize(640,480);
win.setLocation(100,100);
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
win.setDefaultLookAndFeelDecorated(true);

menubar = new JMenuBar();
JMenu menu = new JMenu(Options);
menubar.add(menu);
JMenuItem menuitem = new JMenuItem(Quit);
menuitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.out.println(Menuitem1\\\
);
win.dispose ();
}
});
menu.add(menuitem);
menu.addSeparator();
JMenuItem menuitem2 = new JMenuItem(About);
menuitem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.out.println(Menuitem2\\\
);
}
});
menu.add(menuitem2);
win.setJMenuBar(菜单栏);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
颜色backcol =新颜色(90,0,0);
panel.setBackground(backcol);
con = win.getContentPane();
con.add(panel,BorderLayout.EAST);
drawa = new JPanel();
drawa.setPreferredSize(new Dimension(462,0));
drawa.setBorder(BorderFactory.createLineBorder(Color.yellow,2));
drawa.setBackground(Color.blue);
win.setBackground(Color.magenta);
JPanel panel2 = new JPanel();
panel2.setBackground(backcol);
panel2.setPreferredSize(new Dimension(400,80));
panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
尝试{
InputStream is = new FileInputStream(deng_th_.ttf);
Font font = Font.createFont(Font.TRUETYPE_FONT,is);
颜色textcol =新颜色(255,255,0);
JLabel nulltext = new JLabel();
nulltext.setFont(font.deriveFont(38f));
panel2.add(nulltext);
JLabel titletext = new JLabel(Fortune Cookie:);
titletext.setFont(font.deriveFont(24f));
titletext.setForeground(textcol);
panel2.add(titletext);
} catch(IOException ex){
System.out.println(Font File Error:);
} catch(FontFormatException ex){
System.out.println(Font Error:);
}
con.add(panel2,BorderLayout.NORTH);
JPanel panel3 = new JPanel();
panel3.setBackground(backcol);
panel3.setPreferredSize(new Dimension(400,10));
con.add(panel3,BorderLayout.SOUTH);
JPanel panel4 = new JPanel();
panel4.setBackground(backcol);
panel4.setPreferredSize(new Dimension(10,400));
con.add(panel4,BorderLayout.WEST);
b1 =新JButton(Another);
b1.setToolTipText(Get another fortune cookie);
b1.setPreferredSize(new Dimension(150,48));
b1.setMinimumSize(new Dimension(150,48));
b1.setMaximumSize(new Dimension(150,48));
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.out.println(Button1\\\
);
}
});
ImageIcon b2i = createImageIcon(rand.jpg);
b2 =新JButton(非随机,b2i);
b2.setToolTipText(切换随机选择);
b2.setPreferredSize(new Dimension(150,48));
b2.setMinimumSize(new Dimension(150,48));
b2.setMaximumSize(new Dimension(150,48));
b2.addActionListener(new ActionListener(){
private int randc;
public void actionPerformed(ActionEvent evt){
System.out.println(Button2 \\\
) ;
randc = 1 - randc;
if(randc == 0){
b2.setText(Random);
} else {
b2.setText (非随机);
}
}
});
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
panel.add(b1);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(b2);
// Canvas can = new Canvas();
颜色cancol =新颜色(220,220,220);
// can.setBackground(cancol);
// drawa.add(can);
con.add(drawa,BorderLayout.CENTER);
con.add(new CustomPaint(),BorderLayout.NORTH);
win.setIconImage(new ImageIcon(rand.png)。getImage());
win.setVisible(true);


public static void main(String [] args){
int i,numcook = 0,x,y;
int [] cookiepos = new int [500];
Random ranGen2 = new Random();
种子;
文件fp =新文件(cookie.idx);

new TestPaint();


$ b public class CustomPaint extends JPanel {
$ b @Override
public Dimension getPreferredSize(){

返回新维(100,100); (



@Override
public Dimension getMinimumSize(){
return getPreferredSize();


@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
g2.setColor(Color.BLUE);
g2.fillRect(0,0,getWidth(),getHeight());
g2.setColor(Color.WHITE);

FontMetrics fontMetrics = g2.getFontMetrics();

g2.drawString(This is a test,20,fontMetrics.getAscent());
g2.drawLine(10,10,290,290);
}
}
}


I can't get graphics/text to draw on my panel/canvas/window in my Java program (using swing).

I even tried breaking it up into two classes, with the paintComponent in one (extends JPanel) and other stuff in another class (extends JFrame).

I've tried a panel with a Canvas and without a Canvas (both the same results).

I can't get anything to draw in the blue area. If I remember correctly, when I tried it with no panels at all, I did see graphics. I've successfully done this program in text (no window), and even an Android app (I thought the android app was hard, but that was easy compared to this).

I realize I could use JLabel's to do the text I need, or probably an editbox/edittext (whatever the swing equivalent is), but I'm a graphics oriented person, so I would like to know how to do graphics in Java (for any future projects).

Here's my code (I've only been coding in Java for 4 days, so forgive any bad style, etc. BTW, I changed some variables to global-like and made things public trying to get it to work, so forgive that too :-) Also, I originally had separate procedures for creating buttons, menus, etc. but I later put them all in one procedure in case that was the problem, so forgive that too.)

import java.util.Random;
import java.util.Date;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

public class cookiewin extends JPanel
{

    static int randc = 1;
    static JButton b1, b2;
    JMenuBar menubar = null;
    static JFrame win = null;
    static JPanel panel = null, drawa = null;
    static Graphics2D g2n = null;
//static Graphics g2=null;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.drawString("This is a test", 20, 100);
        g2.drawLine(10, 10, 290, 290);
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL URL = TestPaint.class.getResource(path);
        if (URL != null) {
            return (new ImageIcon(URL));
        } else {
            System.err.println("Can't open '" + path + "'\n");
            return (null);
        }
    }

    public TestPaint() {
        Graphics g3;
        Container con;
        win = new JFrame("Fortune Cookie");
        win.setSize(640, 480);
        win.setLocation(100, 100);
        win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        win.setDefaultLookAndFeelDecorated(true);
        menubar = new JMenuBar();
        JMenu menu = new JMenu("Options");
        menubar.add(menu);
        JMenuItem menuitem = new JMenuItem("Quit");
        menuitem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Menuitem1\n");
                win.dispose();
            }
        });
        menu.add(menuitem);
        menu.addSeparator();
        JMenuItem menuitem2 = new JMenuItem("About");
        menuitem2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Menuitem2\n");
            }
        });
        menu.add(menuitem2);
        win.setJMenuBar(menubar);
        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        Color backcol = new Color(90, 0, 0);
        panel.setBackground(backcol);
        con = win.getContentPane();
        con.add(panel, BorderLayout.EAST);
        drawa = new JPanel();
        drawa.setPreferredSize(new Dimension(462, 0));
        drawa.setBorder(BorderFactory.createLineBorder(Color.yellow, 2));
        drawa.setBackground(Color.blue);
        win.setBackground(Color.magenta);
        JPanel panel2 = new JPanel();
        panel2.setBackground(backcol);
        panel2.setPreferredSize(new Dimension(400, 80));
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        try {
            InputStream is = new FileInputStream("deng_th_.ttf");
            Font font = Font.createFont(Font.TRUETYPE_FONT, is);
            Color textcol = new Color(255, 255, 0);
            JLabel nulltext = new JLabel("    ");
            nulltext.setFont(font.deriveFont(38f));
            panel2.add(nulltext);
            JLabel titletext = new JLabel("        Fortune Cookie:");
            titletext.setFont(font.deriveFont(24f));
            titletext.setForeground(textcol);
            panel2.add(titletext);
        } catch (IOException ex) {
            System.out.println("Font File Error:");
        } catch (FontFormatException ex) {
            System.out.println("Font Error:");
        }
        con.add(panel2, BorderLayout.NORTH);
        JPanel panel3 = new JPanel();
        panel3.setBackground(backcol);
        panel3.setPreferredSize(new Dimension(400, 10));
        con.add(panel3, BorderLayout.SOUTH);
        JPanel panel4 = new JPanel();
        panel4.setBackground(backcol);
        panel4.setPreferredSize(new Dimension(10, 400));
        con.add(panel4, BorderLayout.WEST);
        b1 = new JButton("Another");
        b1.setToolTipText("Get another fortune cookie");
        b1.setPreferredSize(new Dimension(150, 48));
        b1.setMinimumSize(new Dimension(150, 48));
        b1.setMaximumSize(new Dimension(150, 48));
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Button1\n");
            }
        });
        ImageIcon b2i = createImageIcon("rand.jpg");
        b2 = new JButton("Non-Random", b2i);
        b2.setToolTipText("Toggle random selection");
        b2.setPreferredSize(new Dimension(150, 48));
        b2.setMinimumSize(new Dimension(150, 48));
        b2.setMaximumSize(new Dimension(150, 48));
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Button2\n");
                randc = 1 - randc;
                if (randc == 0) {
                    b2.setText("Random");
                } else {
                    b2.setText("Non-Random");
                }
            }
        });
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panel.add(b1);
        panel.add(Box.createRigidArea(new Dimension(0, 10)));
        panel.add(b2);
        Canvas can = new Canvas();
        Color cancol = new Color(220, 220, 220);
        can.setBackground(cancol);
        drawa.add(can);
        con.add(drawa, BorderLayout.CENTER);
        win.setIconImage(new ImageIcon("rand.png").getImage());
        win.setVisible(true);
    }

    public static void main(String[] args) {
        int i, numcook = 0, x, y;
        int[] cookiepos = new int[500];
        Random ranGen2 = new Random();
        long seed;
        File fp = new File("cookie.idx");

        new TestPaint();

    }

}

解决方案

You're basically completely ignoring the component with the custom paint. You build this UI without ever adding the panel to it.

This means your paintComponent method is never called.

Take ALL the UI creation code out of the cookiewin pane and place it somewhere else and THEN add the cookiewin panel into somewhere ...

And with example

public class TestPaint {

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL URL = TestPaint.class.getResource(path);
        if (URL != null) {
            return (new ImageIcon(URL));
        } else {
            System.err.println("Can't open '" + path + "'\n");
            return (null);
        }
    }
    private JFrame win;
    private JMenuBar menubar;
    private JPanel panel;
    private Container con;
    private JPanel drawa;
    private JButton b1;
    private JButton b2;

    public TestPaint() {

        win = new JFrame("Fortune Cookie");
        win.setSize(640, 480);
        win.setLocation(100, 100);
        win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        win.setDefaultLookAndFeelDecorated(true);

        menubar = new JMenuBar();
        JMenu menu = new JMenu("Options");
        menubar.add(menu);
        JMenuItem menuitem = new JMenuItem("Quit");
        menuitem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Menuitem1\n");
                win.dispose();
            }
        });
        menu.add(menuitem);
        menu.addSeparator();
        JMenuItem menuitem2 = new JMenuItem("About");
        menuitem2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Menuitem2\n");
            }
        });
        menu.add(menuitem2);
        win.setJMenuBar(menubar);
        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        Color backcol = new Color(90, 0, 0);
        panel.setBackground(backcol);
        con = win.getContentPane();
        con.add(panel, BorderLayout.EAST);
        drawa = new JPanel();
        drawa.setPreferredSize(new Dimension(462, 0));
        drawa.setBorder(BorderFactory.createLineBorder(Color.yellow, 2));
        drawa.setBackground(Color.blue);
        win.setBackground(Color.magenta);
        JPanel panel2 = new JPanel();
        panel2.setBackground(backcol);
        panel2.setPreferredSize(new Dimension(400, 80));
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        try {
            InputStream is = new FileInputStream("deng_th_.ttf");
            Font font = Font.createFont(Font.TRUETYPE_FONT, is);
            Color textcol = new Color(255, 255, 0);
            JLabel nulltext = new JLabel("    ");
            nulltext.setFont(font.deriveFont(38f));
            panel2.add(nulltext);
            JLabel titletext = new JLabel("        Fortune Cookie:");
            titletext.setFont(font.deriveFont(24f));
            titletext.setForeground(textcol);
            panel2.add(titletext);
        } catch (IOException ex) {
            System.out.println("Font File Error:");
        } catch (FontFormatException ex) {
            System.out.println("Font Error:");
        }
        con.add(panel2, BorderLayout.NORTH);
        JPanel panel3 = new JPanel();
        panel3.setBackground(backcol);
        panel3.setPreferredSize(new Dimension(400, 10));
        con.add(panel3, BorderLayout.SOUTH);
        JPanel panel4 = new JPanel();
        panel4.setBackground(backcol);
        panel4.setPreferredSize(new Dimension(10, 400));
        con.add(panel4, BorderLayout.WEST);
        b1 = new JButton("Another");
        b1.setToolTipText("Get another fortune cookie");
        b1.setPreferredSize(new Dimension(150, 48));
        b1.setMinimumSize(new Dimension(150, 48));
        b1.setMaximumSize(new Dimension(150, 48));
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Button1\n");
            }
        });
        ImageIcon b2i = createImageIcon("rand.jpg");
        b2 = new JButton("Non-Random", b2i);
        b2.setToolTipText("Toggle random selection");
        b2.setPreferredSize(new Dimension(150, 48));
        b2.setMinimumSize(new Dimension(150, 48));
        b2.setMaximumSize(new Dimension(150, 48));
        b2.addActionListener(new ActionListener() {
            private int randc;
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Button2\n");
                randc = 1 - randc;
                if (randc == 0) {
                    b2.setText("Random");
                } else {
                    b2.setText("Non-Random");
                }
            }
        });
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panel.add(b1);
        panel.add(Box.createRigidArea(new Dimension(0, 10)));
        panel.add(b2);
//        Canvas can = new Canvas();
        Color cancol = new Color(220, 220, 220);
//        can.setBackground(cancol);
//        drawa.add(can);
        con.add(drawa, BorderLayout.CENTER);
        con.add(new CustomPaint(), BorderLayout.NORTH);
        win.setIconImage(new ImageIcon("rand.png").getImage());
        win.setVisible(true);
    }

    public static void main(String[] args) {
        int i, numcook = 0, x, y;
        int[] cookiepos = new int[500];
        Random ranGen2 = new Random();
        long seed;
        File fp = new File("cookie.idx");

        new TestPaint();

    }

    public class CustomPaint extends JPanel {

        @Override
        public Dimension getPreferredSize() {

            return new Dimension(100, 100);

        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.BLUE);
            g2.fillRect(0, 0, getWidth(), getHeight());
            g2.setColor(Color.WHITE);

            FontMetrics fontMetrics = g2.getFontMetrics();

            g2.drawString("This is a test", 20, fontMetrics.getAscent());
            g2.drawLine(10, 10, 290, 290);
        }
    }
}

这篇关于我无法在我的Java程序中使用图形在我的面板/画布/窗口上绘制(摆动)。有任何想法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆