⑴ java SWING如何让一个按钮半透明
Java Swing视频教程:
⑵ java 中JButton设置为透明的
JButtonb=newJButton("test");
b.setContentAreaFilled(false);
con.add(b);
con.setBackground(Color.red);
⑶ JAVA 怎么才能把按钮变成透明
setOpaque()方法设置透明连字也看不到;
//建议你用JLabel,你可以这样写,
JLabel b= new JLabel("123");
b.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
//........
}
})
⑷ 在java中怎么设置滚动面板为透明
设置颜色:
把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();
}
}
⑸ java 怎么让JLabel和JPanel透明
import javax.swing.*;
import java.awt.*;
public class BackGround extends JFrame {
Container content;
Toolkit toolkit;
JLabel bkLabel = new JLabel(); // 背景图片标签
JLabel horseLabel = new JLabel();
JButton bButton;
int screenWidth, screenHeight, WIDTH, HEIGHT;
ImageIcon backImage, bImage;
BackGround() {
content = getContentPane();
toolkit = Toolkit.getDefaultToolkit();
screenWidth = toolkit.getScreenSize().width;
screenHeight = toolkit.getScreenSize().height;
bImage = new ImageIcon("pic/logo.jpg");
//我的笨方法
JLabel image_lab = new JLabel(new ImageIcon("D:\\pic\\logo.jpg"));
//编译的时候会帮你把文件拷贝到classes目录下面去
//同时要达到LZ的效果很简单,两个Panel。直接覆盖的话,如果覆盖面板小于底层面板,当鼠标移动到底层面板区域。则显示了,所以建议加上 //setEnabled(false);这样做LZ你不觉得复杂了吗?你直接在我的image_lab 上加载组件不行吗?或者你先都把组件隐藏了,等用户触发事件再显//示也可以啊。
//
backImage = new ImageIcon("pic/background.gif");
bkLabel.setIcon(backImage);
bkLabel.setOpaque(true);
WIDTH = backImage.getIconWidth();
HEIGHT = backImage.getIconHeight();
content.add(bkLabel, -1); // 添加背景图片
bButton = new JButton(bImage);
JPanel panel2 = new JPanel();
panel2.setOpaque(false); // 设置panel2背景透明
panel2.setPreferredSize(new Dimension(40, 40));
panel2.add(bButton);
content.add(panel2);
validate();
setBounds(300, 200, WIDTH, HEIGHT);
setLocation((screenWidth - WIDTH) / 2, (screenHeight - HEIGHT) / 2);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
new BackGround();
}
}