有那位懂得的?
问题补充
我想要的是java SWT 画的面板中Text和Button 透明
不是要Awt Swing 的
Text:
父容器setBackgroundMode(SWT.INHERIT_FORCE);
Button:
1.构造一个空Image
2.利用org.eclipse.swt.widgets.Control.print(org.eclipse.swt.graphics.GC)将父容器上的内容绘制上去.
3.计算自己在父容器的区域,将Image那个区域的Image调用org.eclipse.swt.graphics.GC.drawImage(org.eclipse.swt.graphics.Image, int, int, int, int, int, int, int, int)绘制上去.
4.如果想实现蒙板效果,在3后再设置gc.setAlpha(200);gc.fillRectangle(0,0,w,h);即可.
package com.test;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.Rectangle;
public class Test2 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
/**
* This is the default constructor
*/
public Test2() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBackground(new Color(102, 255, 102));
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(98, 29, 124, 35));
jButton.setText("aaaaaa");
jButton.setBackground(new Color(102, 0, 204));
jButton.setFont(new Font("Dialog", Font.BOLD, 24));
// jButton.setForeground(new Color(255, 51, 0));
jButton.setOpaque(false);
jButton.setBorder(BorderFactory.createEmptyBorder());
}
return jButton;
}
}
你运行这个看看怎么样?
这个是用图片的
package sample;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private JPanel jPanel = null;
/**
* This is the default constructor
*/
public Frame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(513, 309);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJPanel(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(34, 27, 150, 44));
jButton.setText("愛我老婆");
jButton.setBorder(null);
//下面这句设置背景颜色必须要否则无法实现背景透明
jButton.setBackground(Color.BLACK);
jButton.setOpaque(false);
jButton.setForeground(Color.red);
jButton.setFont(new Font("",Font.BOLD,24));
}
return jButton;
}
class getpane extends JPanel {
private static final long serialVersionUID = 1L;
int width = 0, hight = 0;
String imgpath = "";
public getpane(int width, int hight, String imgpath) {
this.width = width;
this.hight = hight;
this.imgpath = imgpath;
this.setOpaque(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon = new ImageIcon(imgpath);
Image img = icon.getImage();
g.drawImage(img, 0, 0, width, hight, this);
}
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
String imgpath = "src/game/image/good02.jpg";
jPanel = new getpane(this.getWidth(), this.getHeight(), imgpath);
// jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(13, 8, 472, 251));
jPanel.add(getJButton(), null);
}
return jPanel;
}
}
[quote="yqklw521"]package com.test;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.Rectangle;
public class Test2 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
/**
* This is the default constructor
*/
public Test2() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBackground(new Color(102, 255, 102));
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(98, 29, 124, 35));
jButton.setText("aaaaaa");
jButton.setBackground(new Color(102, 0, 204));
jButton.setFont(new Font("Dialog", Font.BOLD, 24));
// jButton.setForeground(new Color(255, 51, 0));
jButton.setOpaque(false);
jButton.setBorder(BorderFactory.createEmptyBorder());
}
return jButton;
}
}
你运行这个看看怎么样?[/quote]
连题目都不看清。
你傻啦,你把那控件改成awt的控件不就是一樣的嗎?好心沒好報,笨到家了。
单控件透明,还是控件的parent设置为透明?
单控件透明的话需要自行定制
不是不想给demo,这个东西要完善的实现确实很复杂,而且低效.我建议你可以用Label代替button,通过设置父容器setBackgroundMode(SWT.INHERIT_DEFAULT);就可透明显示label.然后你给label增加一个PaintListern,自己使用GC绘制一个正方框表示button的轮廓.或者你做一个只有边框,中间透明的png图片.设置给Label,也可以.