将JPanel背景设置为透明,Set不透明()不';别让我画画 导入java.awt.Color; 导入java.awt.Font; 导入java.awt.Graphics; 导入java.awt.GridBagConstraints; 导入java.awt.GridBagLayout; 导入javax.swing.JFrame; 导入javax.swing.JPanel; 公共类俄罗斯方块扩展JFrame{ 公共俄罗斯方块(){ 添加(新游戏面板()); setDefaultCloseOperation(关闭时退出); 可设置大小(假); 设置大小(800600); setVisible(真); setLocationRelativeTo(空); setTitle(“俄罗斯方块”); } 公共类游戏面板扩展了JPanel{ 公共游戏小组(){ TetrisBoard tetraBoard=新TetrisBoard(); GridBagLayout=新的GridBagLayout(); 这个.setLayout(布局); GridBagConstraints c=新的GridBagConstraints(); c、 gridx=2; c、 gridy=1; c、 ipadx=190; c、 ipady=390; c、 插图左=360; 布局。设置约束(四板,c); 添加(四板); 挫折地面(颜色:白色); } @凌驾 公共空间涂料(图g){ 超级油漆(g); g、 setFont(新字体(“出生标准”,字体为普通,12)); g、 setColor(颜色:浅灰色); g、 抽绳(200、36、63); g、 抽绳(200、36、88); g、 抽绳(200、36、114); } }//游戏面板类 公共类TetrisBoard扩展JPanel实现Runnable{ 私有线程animator=新线程(此); 私人最终整数延迟=50; 公共电视台{ 设置聚焦(真); //挫折地面(颜色:白色); setDoubleBuffered(真); //这个.背景(颜色.黑色); 设置不透明(假); } @凌驾 public void addNotify(){ super.addNotify(); animator=新线程(此); animator.start(); }//添加通知 @凌驾 公共空间涂料(图g){ 超级油漆(g); g、 drawRect(20,30,130,50); }//油漆 @凌驾 公开募捐{ 很久以前的时间,时差,睡眠; beforeTime=System.currentTimeMillis(); while(true){ 重新油漆(); timeDiff=System.currentTimeMillis()-beforeTime; 睡眠=延迟-时间差; 如果(睡眠
} 使用此代码的结果是它根本不绘制任何内容。我只希望背景是透明的,而不是在背景上绘制的图像,但是如果我设置不透明(false),则绘制方法看起来不会绘制将JPanel背景设置为透明,Set不透明()不';别让我画画 导入java.awt.Color; 导入java.awt.Font; 导入java.awt.Graphics; 导入java.awt.GridBagConstraints; 导入java.awt.GridBagLayout; 导入javax.swing.JFrame; 导入javax.swing.JPanel; 公共类俄罗斯方块扩展JFrame{ 公共俄罗斯方块(){ 添加(新游戏面板()); setDefaultCloseOperation(关闭时退出); 可设置大小(假); 设置大小(800600); setVisible(真); setLocationRelativeTo(空); setTitle(“俄罗斯方块”); } 公共类游戏面板扩展了JPanel{ 公共游戏小组(){ TetrisBoard tetraBoard=新TetrisBoard(); GridBagLayout=新的GridBagLayout(); 这个.setLayout(布局); GridBagConstraints c=新的GridBagConstraints(); c、 gridx=2; c、 gridy=1; c、 ipadx=190; c、 ipady=390; c、 插图左=360; 布局。设置约束(四板,c); 添加(四板); 挫折地面(颜色:白色); } @凌驾 公共空间涂料(图g){ 超级油漆(g); g、 setFont(新字体(“出生标准”,字体为普通,12)); g、 setColor(颜色:浅灰色); g、 抽绳(200、36、63); g、 抽绳(200、36、88); g、 抽绳(200、36、114); } }//游戏面板类 公共类TetrisBoard扩展JPanel实现Runnable{ 私有线程animator=新线程(此); 私人最终整数延迟=50; 公共电视台{ 设置聚焦(真); //挫折地面(颜色:白色); setDoubleBuffered(真); //这个.背景(颜色.黑色); 设置不透明(假); } @凌驾 public void addNotify(){ super.addNotify(); animator=新线程(此); animator.start(); }//添加通知 @凌驾 公共空间涂料(图g){ 超级油漆(g); g、 drawRect(20,30,130,50); }//油漆 @凌驾 公开募捐{ 很久以前的时间,时差,睡眠; beforeTime=System.currentTimeMillis(); while(true){ 重新油漆(); timeDiff=System.currentTimeMillis()-beforeTime; 睡眠=延迟-时间差; 如果(睡眠,java,swing,transparency,paint,paintcomponent,Java,Swing,Transparency,Paint,Paintcomponent,} 使用此代码的结果是它根本不绘制任何内容。我只希望背景是透明的,而不是在背景上绘制的图像,但是如果我设置不透明(false),则绘制方法看起来不会绘制 编辑:根据要求,我发布了一个简单的代码,TetraBoard被添加到GamePanel(使用GridBagLayout),GamePanel被添加到框架中,这3个类是独立的文件。我希望TetraBoard有一个透明的背景,这样我就可以看到GamePanel的背景,但我在TetraBoard上画的东西必须是可见的。如果我设置不透明(false),
编辑:根据要求,我发布了一个简单的代码,TetraBoard被添加到GamePanel(使用GridBagLayout),GamePanel被添加到框架中,这3个类是独立的文件。我希望TetraBoard有一个透明的背景,这样我就可以看到GamePanel的背景,但我在TetraBoard上画的东西必须是可见的。如果我设置不透明(false),则TetraBoard是透明的,但它将我在其上绘制的所有内容都设置为透明。编辑:假设我了解您试图执行的操作,请替换TetraBoard构造函数中的以下行:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tetris extends JFrame {
public Tetris() {
add(new GamePanel());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setSize(800, 600);
setVisible(true);
setLocationRelativeTo(null);
setTitle("Tetris");
}
public class GamePanel extends JPanel {
public GamePanel(){
TetrisBoard tetraBoard= new TetrisBoard();
GridBagLayout layout= new GridBagLayout();
this.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 1;
c.ipadx = 190;
c.ipady = 390;
c.insets.left= 360;
layout.setConstraints(tetraBoard, c);
this.add(tetraBoard);
setBackground(Color.WHITE);
}
@Override
public void paint(Graphics g){
super.paint(g);
g.setFont(new Font("Birth Std", Font.PLAIN, 12));
g.setColor(Color.LIGHT_GRAY);
g.drawString("200", 36, 63);
g.drawString("200", 36, 88);
g.drawString("200", 36, 114);
}
}//GamePanel class
public class TetrisBoard extends JPanel implements Runnable{
private Thread animator= new Thread(this);
private final int DELAY= 50;
public TetrisBoard(){
setFocusable(true);
//setBackground(Color.WHITE);
setDoubleBuffered(true);
//this.setBackground(Color.BLACK);
setOpaque(false);
}
@Override
public void addNotify() {
super.addNotify();
animator = new Thread(this);
animator.start();
}//addNotify
@Override
public void paint (Graphics g){
super.paint(g);
g.drawRect (20, 30, 130, 50);
}//paint
@Override
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (true) {
repaint();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
if (sleep < 0)
sleep = 2;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
}//TetrisBoard class
public static void main(String[] args) {
Tetris t = new Tetris();
}
与:
编辑:假设我理解您试图执行的操作,请替换TetrisBoard构造函数中的以下行:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tetris extends JFrame {
public Tetris() {
add(new GamePanel());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setSize(800, 600);
setVisible(true);
setLocationRelativeTo(null);
setTitle("Tetris");
}
public class GamePanel extends JPanel {
public GamePanel(){
TetrisBoard tetraBoard= new TetrisBoard();
GridBagLayout layout= new GridBagLayout();
this.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 1;
c.ipadx = 190;
c.ipady = 390;
c.insets.left= 360;
layout.setConstraints(tetraBoard, c);
this.add(tetraBoard);
setBackground(Color.WHITE);
}
@Override
public void paint(Graphics g){
super.paint(g);
g.setFont(new Font("Birth Std", Font.PLAIN, 12));
g.setColor(Color.LIGHT_GRAY);
g.drawString("200", 36, 63);
g.drawString("200", 36, 88);
g.drawString("200", 36, 114);
}
}//GamePanel class
public class TetrisBoard extends JPanel implements Runnable{
private Thread animator= new Thread(this);
private final int DELAY= 50;
public TetrisBoard(){
setFocusable(true);
//setBackground(Color.WHITE);
setDoubleBuffered(true);
//this.setBackground(Color.BLACK);
setOpaque(false);
}
@Override
public void addNotify() {
super.addNotify();
animator = new Thread(this);
animator.start();
}//addNotify
@Override
public void paint (Graphics g){
super.paint(g);
g.drawRect (20, 30, 130, 50);
}//paint
@Override
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (true) {
repaint();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
if (sleep < 0)
sleep = 2;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
}//TetrisBoard class
public static void main(String[] args) {
Tetris t = new Tetris();
}
与:
例如:
setBackground(new Color(0,0,0,0));
因此,您必须执行两个操作:1) 覆盖JPanel的
paintComponent(图形g)
2) 并将bg颜色设置为透明:
新颜色(0,0,0,0)
例如:
setBackground(new Color(0,0,0,0));
因此,您必须执行两个操作:1) 覆盖JPanel的
paintComponent(图形g)
2) 并将bg color设置为transparent:
新颜色(0,0,0,0)
要更快地获得更好的帮助,请发布一个。在JPanel
中,覆盖paintComponent(Graphics)
而不是paint(Graphics)
我发布了一个简单的代码,我尝试使用paintComponent而不是paint,但我得到了相同的结果。“我发布了一个简单的代码…”我推荐(并将研究)一个SSCCE。而且,JPanel
仍然覆盖paint()
!你能看完一句话吗?我必须同意安德鲁的观点。你的问题是一个行为不端的问题,如果我们能够完全理解这个行为不端的问题,我们可以自己去看,通过拥有我们可以编译、运行、修改和希望纠正的代码,一个足够小的程序,这样我们就不会被太多无关的代码淹没,这样我们就看不到树的森林,但又足够大,可以编译和运行,简言之,是一个sscce。很抱歉前面的代码。现在与原始代码的唯一区别是,我不使用图像,而是绘制一个矩形。我发布的代码有效,但我的原始代码无效。我使用了paintComponent,但它也不起作用。要更快获得更好的帮助,请发布一个。在JPanel
中,覆盖paintComponent(Graphics)
而不是paint(Graphics)
我发布了一个简单的代码,我尝试使用paintComponent而不是paint,但我得到了相同的结果。“我发布了一个简单的代码…”我推荐(并将研究)一个SSCCE。而且,JPanel
仍然覆盖paint()
!你能看完一句话吗?我必须同意安德鲁的观点。你的问题是一个行为不端的问题,如果我们能够完全理解这个行为不端的问题,我们可以自己去看,通过拥有我们可以编译、运行、修改和希望纠正的代码,一个足够小的程序,这样我们就不会被太多不相关的代码淹没,这样我们就看不到森林中的树木,但又足够大,可以编译