重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这是我一个一个代码敲上的,希望对你有用啊!!
成都创新互联公司自2013年起,先为钟山等服务建站,钟山等地企业,进行企业商务咨询服务。为钟山企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
提示:这是在一个有背景的JFrame中加入一个JTable,代码如下:
public class imgs{
pubic imgs(){}
public static void main(String[] args){
JFrame frame= new JFrame("背景透明展示");
ImageIcon icon=new ImageIcon("img\\a.jpg");
jlabel lab=new jlabel(icon); //将图片放入到label中
lab.setbounds(0,0,icon.getIconWidth(),icon.geticonHeight()); //设置放有图片的label的位置
JTable t=new jtable(20,3){ //设置jtable的单元格为透明的
public Component prepareRenderer(TableCellRenderer renderer,int row,int column){
component c=super.prepareRenderer(renderer,row,column);
if(c instanceof JComponent){
((JComponent)c).setOpaque(false);
}
return c;
}
};
t.setOpaque(false); //设置jtable本身为透明的
frame.getContentPane().add(lab,-1); //jframe本身是窗体,不能放置任何组件,用getContentPane()方法得到frame的默认内容面板,将lab放入其中,-1表示放入面板的下层
frame.getCcontentPane().add(t,0); //0表示放在面板的最顶层
Container con=frame.getContentPane();
((JPanel)con).setOpaque(false); //设置面板为透明的
frame.setsize(500,500);
frame.setVisible(true);
基本思想就是这个样子了,我也是刚刚弄出来的,个人觉得和你要求的样式差不多,但愿有用吧,自己手打的,很多大小写都没有切换过来,你要注意看懂了,自己打一遍
}
}
你好,你可以增加以下代码,然后就变透明了:
UIManager.put("TabbedPane.contentOpaque", false);
JTabbedPane tabbedPane = new JTabbedPane();
希望可以帮助到你,记得采纳哦
我简单的做了一下,很简单的,代码如下:
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 12-7-1
* Time: 上午8:39
* To change this template use File | Settings | File Templates.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.awt.AWTUtilities;
public class a extends JPanel{
public static void main(String[] args)
{
JButton t1;
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
JFrame f1=new JFrame();
JMenuBar mb =new JMenuBar();
mb.add(t1=new JButton("X"),BorderLayout.EAST);
t1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f1.setJMenuBar(mb);
f1.add(dataList);
f1.setSize(300,300);
f1.setUndecorated(true);
AWTUtilities.setWindowOpacity(f1, 0.6F);
f1.setLocationRelativeTo(null);
f1.setVisible(true);
}
}
效果如下:
如有不解:群里找我,咖啡编程:
57914814
java swing中将组件的背景设置成透明的示例如下:
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
public class Talpha {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
//使用paintlistener,保证每次均重新绘制。
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
GC gc = e.gc;
//读图像
ImageData imageData = new ImageData("1.jpg");
//这里是建立从左到右的渐进Alpha。
byte[] alphaValues = new byte[imageData.height * imageData.width];
for (int j = 0; j imageData.height; j++) {
for (int i = 0; i imageData.width; i++) {
alphaValues[j * imageData.width + i] = (byte) (255 - 255
* i / imageData.width);
}
}
imageData.alphaData = alphaValues;
Image image = new Image(display, imageData);
//绘制
gc.drawImage(image,0,0);//关键点是这里设置背景颜色
}
});
FillLayout fillLayout = new FillLayout();
fillLayout.type = SWT.VERTICAL;
shell.setLayout(fillLayout);
shell.setSize(200, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
当然可以,Button透明背景设置方法:
方法1:JButton b1=new JButton();
b1.setOpaque(false);
b1.setColor(new Color(2,2,2)); //此颜色值随便设置,只起占位作用,但是必须加上此句,否则不会出现透明背景效果。
b1..setIcon(new ImageIcon("bin/filechooser/image/rec.gif"));
方法2:JButton b2=new JButton();
b2.setContentAreaFilled(false); //只须加上此句
b2.setIcon(new ImageIcon("bin/filechooser/image/rec.gif"));
设置颜色:
把pan - pan6的Background设为Null,这样他们就变为透明的了,以后只需要改变inquestFrame的颜色就可以改变全部的颜色,代码如下:
// 设置整体布局
JPanel pan = new JPanel();
pan.setLayout(new GridLayout(6, 1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
pan.add(pan4);
pan.add(pan5);
pan.add(pan6);
pan.setBackground(null);
pan1.setBackground(null);
pan2.setBackground(null);
pan3.setBackground(null);
pan4.setBackground(null);
pan5.setBackground(null);
pan6.setBackground(null);
r1.setBackground(null);
r2.setBackground(null);
inquestFrame.setBackground(Color.LIGHT_GRAY);
inquestFrame.setContentPane(pan);
inquestFrame.setSize(300, 250);
inquestFrame.setVisible(true);
设置图片需要改的比较多,自己比较那不一样吧:
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Inquest {
JFrame inquestFrame;
JLabel lab1, lab2, lab3, lab4, lab5, lab6;
JTextField text1, text2, text4, text5, text6;
JRadioButton r1, r2;
JButton inqButton;
public Inquest() {
inquestFrame = new JFrame("教育经历管理系统-查询窗口");
MyPanel pan1 = new MyPanel();
lab1 = new JLabel("请输入编码:");
text1 = new JTextField(10);
inqButton = new JButton(" 查询");
inqButton.addActionListener(new ButtonHandler());
pan1.add(lab1);
pan1.add(text1);
pan1.add(inqButton);
MyPanel pan2 = new MyPanel();
lab2 = new JLabel("姓名:");
text2 = new JTextField(10);
pan2.add(lab2);
pan2.add(text2);
MyPanel pan3 = new MyPanel();
lab3 = new JLabel("性别:");
r1 = new JRadioButton("男");
r2 = new JRadioButton("女");
// 把两个单选按钮放到一个群组中,保持二选一
ButtonGroup group = new ButtonGroup();
group.add(r1);
group.add(r2);
pan3.add(lab3);
pan3.add(r1);
pan3.add(r2);
MyPanel pan4 = new MyPanel();
lab4 = new JLabel("单位:");
text4 = new JTextField(10);
pan4.add(lab4);
pan4.add(text4);
MyPanel pan5 = new MyPanel();
lab5 = new JLabel("职位:");
text5 = new JTextField(10);
pan5.add(lab5);
pan5.add(text5);
MyPanel pan6 = new MyPanel();
lab6 = new JLabel("年龄:");
text6 = new JTextField(10);
pan6.add(lab6);
pan6.add(text6);
// 设置整体布局
MyPanel pan = new MyPanel();
pan.setLayout(new GridLayout(6, 1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
pan.add(pan4);
pan.add(pan5);
pan.add(pan6);
pan.setBackground(null);
pan1.setBackground(null);
pan2.setBackground(null);
pan3.setBackground(null);
pan4.setBackground(null);
pan5.setBackground(null);
pan6.setBackground(null);
r1.setBackground(null);
r2.setBackground(null);
MyPanel pan0 = new MyPanel();
pan0.setPreferredSize(pan.getPreferredSize());
pan0.setLayout(null);
pan.setBounds(0, 0, pan.getPreferredSize().width, pan.getPreferredSize().height);
URL url = Inquest.class.getResource("Winter.jpg");
ImageIcon img = new ImageIcon(url);
JLabel lab = new JLabel(img);
lab.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
pan0.add(pan);
pan0.add(lab);
inquestFrame.setContentPane(pan0);
inquestFrame.pack();
inquestFrame.setVisible(true);
}
public static class MyPanel extends JPanel {
public void paint(Graphics g) {
paintChildren(g);
}
}
public void setBack() {
((JPanel) inquestFrame.getContentPane()).setOpaque(false);
// Winter.jpg这个图片的位置要跟当前这个类是同一个包下
URL url = Inquest.class.getResource("Winter.jpg");
ImageIcon img = new ImageIcon(url);
JLabel background = new JLabel(img);
inquestFrame.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 生成SQL语句
String s = "select * from information where id=" + "'" + text1.getText() + "'";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:test";
// DB db = new DB(driver, url, "sa", "");
// ResultSet rs = db.query(s);
// try {
// while (rs.next()) {
// text2.setText(rs.getString(2));
// String sex = rs.getString(3);
// if (sex.trim().equals("男"))
// r1.setSelected(true);
// else
// r2.setSelected(true);
//
// text4.setText(rs.getString(4));
// text5.setText(rs.getString(5));
// text6.setText(rs.getString(6));
// }
// } catch (Exception e2) {
// e2.printStackTrace();
// }
}
}
public static void main(String args[]) {
new Inquest();
}
}