導航:首頁 > 編程語言 > javajpanel背景

javajpanel背景

發布時間:2022-09-15 05:07:33

① 在java中怎麼樣設置JPanel裡面的背景圖片

java要加背景圖比較麻煩,需要重載paintComponent方法,下面以JPanel為例,其它按此繼承就可
JPanel jContentPane = new getpane(800,600,"images\\0044.jpg");
jContentPane.setLayout(null);
jContentPane.setOpaque(false);//此處要設為false
jContentPane.setAlignmentY(0.5F);
jContentPane.add(jLabel, null);
jContentPane.add(getJTestYear(), null);
this.initComboBox();
jContentPane.add(getJSearchByName(), null);
jContentPane.add(getJSearchByID(), null);
jContentPane.add(getJIDOrName(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJPanel(), null);
this.setContentPane(jContentPane);

class getpane extends JPanel{
private static final long serialVersionUID = 1L;
int width=0,hight=0;
String imgpath = "";
public getpane(int width,int hight,String file){
this.width = width;
this.hight = hight;
imgpath = file;
}
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon(imgpath);
Image img = icon.getImage();
g.drawImage(img, 0, 0, width, hight, this);
}

}

② java中使用Eclipse怎麼把JPanel的背景色設置成透明的

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(null); // 把背景設置為會
buttonPanel.setOpaque(false); // 設置為透明

③ java中jpanel的背景圖片問題

寫一個ImageJPanel類繼承自JPanel,在paintComponent方法中用g.drawImage繪制Image背景圖片就擋不著第一張卡片上的四個按鈕了。

完整的程序如下:(注意把背景圖片的路徑換成你的背景圖片路徑)

importjava.awt.CardLayout;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.GridLayout;
importjava.awt.Image;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JSplitPane;
{
=1L;
JPaneljp1=newJPanel();
JPaneltmpjp1=newJPanel();
JPaneljp2=newJPanel();
JPanelcard_jp1=newJPanel();
JPanelcard_jp2=newJPanel();
JPanelcard_jp3=newJPanel();
JPanelcard_jp4=newJPanel();
JButtonjb1=newJButton("按鈕1");
JButtonjb2=newJButton("按鈕2");
JButtonjb3=newJButton("按鈕3");
JButtonjb4=newJButton("按鈕4");
JSplitPanejsp=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,jp1,jp2);
CardLayoutcl=newCardLayout();
intj=1;
D(){
tmpjp1.setLayout(newGridLayout(4,1,5,5));
tmpjp1.add(jb1);tmpjp1.add(jb2);tmpjp1.add(jb3);tmpjp1.add(jb4);
jp1.add(tmpjp1);
jp2.setLayout(cl);
JPanelp[]=newJPanel[4];
p[0]=newImageJPanel();
jp2.add(p[0],String.valueOf(0));
for(inti=1;i<4;i++){
p[i]=newJPanel();
jp2.add(p[i],String.valueOf(i));
}
JButtonb[]=newJButton[4];
for(inti=1;i<4;i++){
inta=i+1;
b[i]=newJButton("第"+a+"頁,下一頁");
b[i].addActionListener(this);
p[i].add(b[i]);
}
jsp.setResizeWeight(0.3);
add(jsp);
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}

publicstaticvoidmain(String[]args){
newD();
}
@Override
publicvoidactionPerformed(ActionEvente){
cl.show(jp2,String.valueOf(j++));
if(j==4)j=0;
}
classImageJPanelextendsJPanel{
privateImageimage;
JButtoncard_jb1=newJButton("按鈕1");
JButtoncard_jb2=newJButton("按鈕2");
JButtoncard_jb3=newJButton("按鈕3");
JButtonjb=newJButton("第1頁,下一頁");
publicImageJPanel(){
super();
setOpaque(true);
add(card_jb1);
add(card_jb2);
add(card_jb3);
jb.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
cl.show(jp2,String.valueOf(j++));
if(j==4)j=0;
}
});
add(jb);
image=Toolkit.getDefaultToolkit().getImage("C:/tmp/F/3.jpg");//這里換成你的背景圖片地址
}
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
setBackground(Color.WHITE);
if(image!=null){
intheight=image.getHeight(this);
intwidth=image.getWidth(this);
if(height!=-1&&height>getHeight())
height=getHeight();
if(width!=-1&&width>getWidth())
width=getWidth();
intx=(int)(((double)(getWidth()-width))/2.0);
inty=(int)(((double)(getHeight()-height))/2.0);
g.drawImage(image,x,y,width,height,this);
}
}
}
}

④ JAVA 的JPanel 設置背景求解

你的this的布局是不是null啊、如果是null,就看不到
你可以設置borderlayout,就可以看到了,他就在中間

⑤ 在java中怎麼樣設置JPanel裡面的背景圖片

java要加背景圖比較麻煩,需要重載paintComponent方法,下面以JPanel為例,其它按此繼承就可 JPanel jContentPane = new getpane(800,600,"images\\0044.jpg"); jContentPane.setLayout(null); jContentPane.setOpaque(false);//此處要設為false jContentPane.setAlignmentY(0.5F); jContentPane.add(jLabel, null); jContentPane.add(getJTestYear(), null); this.initComboBox(); jContentPane.add(getJSearchByName(), null); jContentPane.add(getJSearchByID(), null); jContentPane.add(getJIDOrName(), null); jContentPane.add(jLabel1, null); jContentPane.add(getJPanel(), null); this.setContentPane(jContentPane); class getpane extends JPanel{ private static final long serialVersionUID = 1L; int width=0,hight=0; String imgpath = ""; public getpane(int width,int hight,String file){ this.width = width; this.hight = hight; imgpath = file;}protected void paintComponent(Graphics g) {

⑥ Java swing JPanel背景顏色邊框顯示問題 實現多個JPanel的無縫連接

你使用的是TitleBorder,是帶邊框的,背景色不會改變邊框的顏色。要麼設置EmptyBorder,要麼改變邊框顏色。

⑦ java中如何在JPanel里設置背景

使用成員函數setBackground(Color bg),下面是java文檔中對該方法的說明。
public void setBackground(Color bg)
Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.
It is up to the look and feel to honor this property, some may choose to ignore it.

⑧ 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();
}
}

⑨ java swing 的jPanel背景顏色能否設置為透明,代碼怎麼寫

設置顏色:
把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();
}

}

閱讀全文

與javajpanel背景相關的資料

熱點內容
壓縮因子定義 瀏覽:968
cd命令進不了c盤怎麼辦 瀏覽:214
葯業公司招程序員嗎 瀏覽:974
毛選pdf 瀏覽:659
linuxexecl函數 瀏覽:727
程序員異地戀結果 瀏覽:374
剖切的命令 瀏覽:229
干什麼可以賺錢開我的世界伺服器 瀏覽:290
php備案號 瀏覽:990
php視頻水印 瀏覽:167
怎麼追程序員的女生 瀏覽:487
空調外壓縮機電容 瀏覽:79
怎麼將安卓變成win 瀏覽:459
手機文件管理在哪兒新建文件夾 瀏覽:724
加密ts視頻怎麼合並 瀏覽:775
php如何寫app介面 瀏覽:804
宇宙的琴弦pdf 瀏覽:396
js項目提成計算器程序員 瀏覽:944
pdf光子 瀏覽:834
自拍軟體文件夾名稱大全 瀏覽:328