1. 怎么设置javax.Swing.Jplane的背景
Hi, guy ,this is my code long time agoclass WindowDesktop1 extends JDesktopPane { private static final long serialVersionUID = -3458049910691811494L; Image image = new ImageIcon("image\\main.png").getImage(); public WindowDesktop1() { super(); } public void paintComponent(Graphics g) { g.drawImage(image, 0, 0, getWidth(), getHeight(), 0, 0, image .getWidth(null), image.getHeight(null), null); }}// 在窗体里建立虚拟桌面并实例化WindowDesktop1 jDesktopPane1 = new WindowDesktop1();getContentPane().add(jDesktopPane1, BorderLayout.CENTER);jDesktopPane1.setPreferredSize(new java.awt.Dimension(652, 476));
2. java中panel中设置背景图片
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewJFrame.java
*
* Created on 2010-4-25, 16:12:38
*/
package test;
import java.awt.*;
import javax.swing.*;
/**
*
* @author Administrator
*/
public class NewJFrame extends javax.swing.JFrame {
JScrollPane scrollPane;
ImageIcon icon;
Image image;
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
icon = new ImageIcon("login1.jpg");
jPanel1.setPreferredSize( new Dimension(614, 363) );
scrollPane = new JScrollPane( jPanel1 );
getContentPane().add( jPanel1 );
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel()
{
protected void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
jPanel2 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jPanel1.setOpaque(false);
jPanel2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 308, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 140, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(149, 149, 149)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(154, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(161, 161, 161)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(62, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// //GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
new NewJFrame().setSize(613, 365);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration//GEN-END:variables
}
3. java JFrame里面怎么添加背景
我这里刚好有个例子
ImageIcon img = new ImageIcon("f:/library.jpg");//这是背景图片 JLabel imgLabel = new JLabel(img);//将背景图放在标签里。 this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。 imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());//设置背景标签的位置
4. 在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) {
5. JAVA程序框如何设置背景
设置background颜色,或 在paint事件绘图片
~~~~~~~~
6. Java中怎么设置面板的背景为白色啊??在线等......急急急
用
setBackground(Color.WHITE)
方法
7. 在java中如何设定面板的背景颜色
我们先打开需要设置背景颜色的word文档,打开之后我们可以看到在工具栏有一个“格式”选项,单击该选项,会看到该选项还会下分子选项。
在下分的选项里,我们将鼠标移到“背景”又会下拉选项,这时我们就可以选择我们喜欢的颜色作为背景颜色了,假如你喜欢红色的话,那么单击红色即可。
单击“红色”按钮之后就会出现如图所示啦,这时我们就可以看到word文档的背景变成红色的了。
选择红色之后,我们可以看到红色太刺眼了,那么想选择其他颜色的话该怎么办呢?那么我们继续按照上面的两个步骤来做,只不过在下拉的选项里我们单击“填充效果”按钮。
单击“填充效果”按钮之后会弹出如图对话框,这时我们单击“纹理”按钮。
单击“纹理”按钮之后会弹出如图对话框,这时我们就可以给word文档填充自己喜欢的纹理背景了。
如果你不喜欢纹理背景的话,那么可以单击“图案”按钮选择自己喜欢的图案来做word背景颜色。
如果“图案”里面的图形也不能满足你的话,那么你可以继续单击后方的“图片”按钮。单击之后会出现如图所示,这时我们单击下方的“选择图片”按钮。
单击之后会弹出如图对话框,这时我们可以从自己的电脑里找到自己喜欢的图片来作为word的背景颜色,找到之后单击“插入”按钮即可。
单击“插入”按钮之后会出现如图所示,此时我们单击“确定”按钮。
单击“确定”按钮之后就会出现如下图所示啦,这时我们就可以看出来,这次的背景颜色比第一次的要好看很多。为word添加背景颜色你会了吗?
8. java如何在使用了GridBagLayout的JDesktopPane中设置背景图片? 如果设置
package windows;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.font.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import util.ConnectionDB;
public class Location extends JFrame implements ActionListener{
//用户名的文本框
JTextField JTF = new JTextField(20);
//密码的文本行
JPasswordField JPF = new JPasswordField(25);
Location(){
//设置窗体标题
this.setTitle("用户登录");
//设置窗体大小
this.setSize(200, 200);
//设置背景色
this.setBackground(java.awt.Color.lightGray);
//窗体关闭时结束程序
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//获取内容窗格
Container jf_con = this.getContentPane();
//设置布局管理器
jf_con.setLayout(new BorderLayout());
//获得图片对象
ImageIcon backIcon=new ImageIcon("background.jpg");
//把图片绘制到背景面板上
JLabel backLabel=new JLabel(backIcon);
// 创建背景标签
backLabel.setSize(backIcon.getIconWidth(), backIcon.getIconHeight());
backLabel.setLocation(0, 0);
// 设置标签定位
backLabel.setLayout(new GridLayout(4,1));
//建立登录和注册按钮
JButton JB[] = new JButton[2];
JB[0] = new JButton("登录");
JB[1] = new JButton("注册新账号");
for(int i=0;i<2;i++)
{
JB[i].setForeground(Color.red);
Font font =new Font("黑体",0,16);
JB[i].setFont(font);
JB[i].addActionListener(this);
}
//建立用户名,注册和密码标签
JLabel JL[] = new JLabel[2];
JL[0] = new JLabel("用户名");
JL[1] = new JLabel("密码");
//建立面板
JPanel JP[] = new JPanel[4];
//用户名面板
JP[0] = new JPanel();
JP[0].setLayout(new GridLayout(2,1));
JP[0].add(JL[0]);
JP[0].add(JTF);
//密码面板
JP[1] = new JPanel();
JP[1].setLayout(new GridLayout(2,1));
JP[1].add(JL[1]);
JP[1].add(JPF);
//登录按钮面板
JP[2] = new JPanel(new FlowLayout());
JP[2].add(JB[0]);
//注册按钮面板
JP[3] = new JPanel(new FlowLayout());
JP[3].add(JB[1]);
//把面板的背景设置为透明
for(int i=0;i<4;i++)
JP[i].setOpaque(false);
//把所有面板放置到窗体上
backLabel.add(JP[0]);
backLabel.add(JP[1]);
backLabel.add(JP[2]);
backLabel.add(JP[3]);
jf_con.add(backLabel);
//把窗口移到屏幕中间
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
int x=(dim.width-200)/2;
int y=(dim.height-100)/2;
this.setLocation(x, y);
//去除窗体的最大化键
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args){
new Location();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton j=(JButton)e.getSource();
String x=null;
x=j.getText();
if(x=="登录"){
this.setVisible(false);
Connection con=ConnectionDB.getConnection();
String sql="select L_Pass,L_Degree from Location where L_Location=?";
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
ResultSet rs=null;
try {
ps.setString(1, JTF.getText());
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
rs=ps.executeQuery();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String pass = null;
String degree = null;
try {
while(rs.next()){
pass=rs.getString("L_Pass");
degree=rs.getString("L_Degree");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String s=new String(JPF.getPassword());
if(!s.equals(pass)){
JOptionPane.showMessageDialog(null, "用户名或密码错误","警告",JOptionPane.ERROR_MESSAGE);
this.setVisible(true);
JPF.setText(null);
}
else{
if("0".equals(degree))
System.out.println("Client");
else if("1".equals(degree))
new Administrator();
else if("2".equals(degree))
System.out.println("Others");
}
}
else if(x=="注册新账号"){
this.setVisible(false);
new Registration();
}
}
}