我已经卡住试图画上一个JPanel.我已经学会了如何创建a JFrame,a JPanel,添加事件处理程序,在applet中执行相同的操作以及其他一些基本的GUI开发.但每当我尝试JPanel使用paintComponent(Graphics g)或paint(Graphics g)方法时,都没有显示出来.按钮,以及其他JComponents,显示就好了.但是g.drawString,g.drawRect等等导致一个空白的JFrame窗口.
这是我的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Game extends JPanel {
public static void main(String[] args) {
new Game().game();
}
JPanel panel;
JButton button2;
JButton button;
public void game() {
panel = new JPanel();
button = new JButton("Ok");
panel.setLayout(new FlowLayout());
panel.add(button);
button2 = new JButton("Cancel");
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setResizable(false); …Run Code Online (Sandbox Code Playgroud)