⑴ java怎麼樣設置窗口透明而控制項不透明
要完全點不到窗口只能點到組件可以設置窗口的形狀。
⑵ java中窗體的標題欄如何設為透明
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import com.birosoft.liquid.LiquidLookAndFeel;
public class TestEvent extends JComponent
implements ComponentListener,WindowFocusListener {
private JFrame frame;
private boolean start = false;
private Image background;
private Point p;
// 獲得當前屏幕快照
public void updateBackground() {
try {
Robot rbt = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
background = rbt.createScreenCapture(new Rectangle(0, 0, (int) dim
.getWidth(), (int) dim.getHeight()));
} catch (Exception ex) {
// p(ex.toString());
// 此方法沒有申明過 ,因為無法得知上下文 。因為不影響執行效果 ,先注釋掉它 ex.printStackTrace();
}
}
// 將窗口掉離出屏幕以獲得純粹的背景圖象
public void refresh() {
if (start == true) {
this.updateBackground();
frame.setLocation(p);
if (p.x < 0 || p.y < 0)
frame.setLocation(0, 0);
this.repaint();
}
}
public void componentHidden(ComponentEvent e) {
System.out.println("Hidden");
}
// 窗口移動時
public void componentMoved(ComponentEvent e) {
System.out.println("moved");
this.repaint();
}
// 窗口改變大小時
public void componentResized(ComponentEvent e) {
System.out.println("resized");
this.repaint();
}
public void componentShown(ComponentEvent e) {
System.out.println("shown");
}
// 窗口得到焦點後,用refresh()方法更新界面
public void windowGainedFocus(WindowEvent e) {
System.out.println("gainedFocus");
refresh();
start = false;
}
// 窗口失去焦點後,將其移出屏幕
public void windowLostFocus(WindowEvent e) {
System.out.println("lostFocus");
if (frame.isShowing() == true) {
System.out.println("visible");
} else {
System.out.println("invisible");
}
start = true;
p = frame.getLocation();
frame.setLocation(-2000, -2000);
}
public TestEvent(JFrame frame) {
super();
this.frame = frame;
updateBackground();
this.setSize(200, 120);
this.setVisible(true);
frame.addComponentListener(this);
frame.addWindowFocusListener(this);
}
// 繪制外觀,注意,其中 pos,offset 是為了將特定部分的圖象貼到窗口上
public void paintComponent(Graphics g) {
Point pos = this.getLocationOnScreen();
Point offset = new Point(-pos.x, -pos.y);
g.drawImage(background, offset.x, offset.y, null);
}
/**
* @param args
*/
public static void main(String[] args) {
try {
// UIManager.setLookAndFeel("org.fife.plaf.Office2003.Office2003LookAndFeel");
// UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");
// UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");
UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
LiquidLookAndFeel.setLiquidDecorations(true);
// LiquidLookAndFeel.setLiquidDecorations(true, "mac");
// UIManager.setLookAndFeel(new SubstanceLookAndFeel());
// UIManager.setLookAndFeel(new SmoothLookAndFeel());
// UIManager.setLookAndFeel(new QuaquaLookAndFeel());
// UIManager.put("swing.boldMetal", false);
if (System.getProperty("substancelaf.useDecorations") == null) {
JFrame.(true);
// JDialog.(true);
}
System.setProperty("sun.awt.noerasebackground", "true");
// SubstanceLookAndFeel.setCurrentTheme(new
// SubstanceLightAquaTheme());
// UIManager.setLookAndFeel("org.fife.plaf.VisualStudio2005.VisualStudio2005LookAndFeel");
} catch (Exception e) {
System.err.println("Oops! Something went wrong!");
}
JFrame frame = new JFrame("Transparent Window");
TestEvent t = new TestEvent(frame);
t.setLayout(new BorderLayout());
JButton button = new JButton("This is a button");
t.add("North", button);
JLabel label = new JLabel("This is a label");
t.add("South", label);
frame.getContentPane().add("Center", t);
frame.pack();
frame.setSize(150, 100);
frame.show();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// t.start=true;
}
}
⑶ java JFrame製作透明窗口的問題
不明白你要的窗口透明到底是什麼意思。不知道可不可以嘗試添加不同的JPanel,一個專門用來做你所說的透明效果以及paint裡面的操作,然後另一層JPanel來負責放置你得組件。建議多換思路多嘗試。
⑷ java實現窗口全屏透明化,並在屏幕中顯示漢字
// 將一個Frame最大化
if (f.isUndecorated()) {// 無邊框窗口
f.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());
}else{
f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
⑸ java怎麼給窗體邊框四周添加陰影和半透明
java swing是提供了設置陰影和透明度的方法的,示例如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.awt.AWTUtilities;
導入 AWTUtilities可能會提示:訪問限制:由於對必需的庫 C:\Program Files\Java\jre6\lib\rt.jar 具有一定限制,因此無法訪問類型 AWTUtilities,請到eclipse進行設置:窗口》首選項》java》編譯器》錯誤警告 選擇 代碼樣式》訪問外層不可訪問成員》 把錯誤修改成功 警告
public class Ceshi extends JFrame
{
public Ceshi()
{
JLabel label=new JLabel("這是一個標簽!");
label.setOpaque(false);
label.setHorizontalAlignment(JLabel.CENTER);
add(label);
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Opacity最大值為1.0f,也就是什麼也不透明,取值不能大於1.0f
AWTUtilities.setWindowOpacity(this,0.5f);
setVisible(true);
}
public static void main(String[] args)
{
new Ceshi();
}
}
⑹ java透明窗口問題求教
這純粹是Timer類使用錯誤:
第一個錯誤說明你的source的問題,Timer是沒有stop方法的,停止Timer是用cancel()方法;
第二個錯誤時你new Timer的時候參數不對,Timer的構造方法有Timer(),Timer(boolean isDaemon),Timer(String name),Timer(String name,boolean isDaemon),Timer類也沒有start方法。
你這計時器的用法有問題。
⑺ 在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窗口透明化
這個問題應該是你沒有導入com.sun.awt.awt.awt.AWTUtilities這個包,因為以com或者sun開頭的包不是javase基礎類庫中的包,所以你要是在使用它們的時候需要單獨設置它們,如果你使用的是myeclipse的話,你可以這樣進行配置
右鍵項目名稱----->Propreties------>Libraries------>展開JRE(單擊JRE System...前面的+號)------>雙擊Access Rule...------>單擊add------>把Forbidden變為Accessible 同時Rule Pattern中添加 com/**----->確定完畢了~~~~
⑼ java 窗口內背景透明是用哪個列做的
panel之類的內部窗體的話setOpaque(false) 設置一下這個就會透明了...不過要是jframe之類的窗體..那就得自己去截取背景圖來作為窗體的背景了
⑽ java JFrame窗口透明組件不透明該怎麼實現
JPanel.setOpaque(false); //false透明,true不透明