关于jframe中paint方法 重绘机制
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import org.omg.CORBA.ORB;
import com.sun.corba.se.impl.activation.CommandHandler;
public class test extends JFrame implements WindowListener, CommandHandler {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JButton jButton4 = null;
private JButton jButton5 = null;
private JLabel jLabel1 = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JTextField jTextField2 = null;
private JProgressBar jProgressBar = null;
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
public String getCommandName() {
// TODO Auto-generated method stub
return null;
}
public void printCommandHelp(PrintStream out, boolean helpType) {
// TODO Auto-generated method stub
}
public boolean processCommand(String[] cmd, ORB orb, PrintStream out) {
// TODO Auto-generated method stub
return false;
}
/**
* This is the default constructor
*/
public test() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(496, 208);
this.setContentPane(getJContentPane());
this.setTitle("文件加密");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(64, 70, 54, 24));
jLabel.setText(" 密 码 ");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(63, 38, 55, 24));
jLabel1.setText("文件路径");
jLabel1.setOpaque(true);
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
jContentPane.add(getJButton4(), null);
jContentPane.add(getJButton5(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJTextField2(), null);
jContentPane.add(getJProgressBar(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(26, 9, 60, 17));
jButton.setText("编码");
}
return jButton;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(96, 9, 60, 17));
jButton2.setText("解码");
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(new Rectangle(166, 9, 88, 17));
jButton3.setText("批量编码");
}
return jButton3;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButton4() {
if (jButton4 == null) {
jButton4 = new JButton();
jButton4.setBounds(new Rectangle(263, 9, 88, 17));
jButton4.setText("批量解码");
}
return jButton4;
}
/**
* This method initializes jButton5
*
* @return javax.swing.JButton
*/
private JButton getJButton5() {
if (jButton5 == null) {
jButton5 = new JButton();
jButton5.setBounds(new Rectangle(360, 9, 94, 17));
jButton5.setText("选择文件");
}
return jButton5;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(133, 38, 303, 24));
}
return jTextField;
}
/**
* This method initializes jTextField2
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField2() {
if (jTextField2 == null) {
jTextField2 = new JTextField();
jTextField2.setBounds(new Rectangle(133, 70, 303, 24));
}
return jTextField2;
}
/**
* This method initializes jProgressBar
*
* @return javax.swing.JProgressBar
*/
private JProgressBar getJProgressBar() {
if (jProgressBar == null) {
jProgressBar = new JProgressBar();
jProgressBar.setBounds(new Rectangle(123, 146, 286, 20));
jProgressBar.setString("内部显示");
jProgressBar.setStringPainted(true);
jProgressBar.setVisible(false);
}
return jProgressBar;
}
public void paint(Graphics g){
super.paint(g);
Toolkit tool=this.getToolkit();
Image innerImage=tool.getImage("picjiqimao.jpg");
g.drawImage(innerImage,0,0,660,700,0,0,1700,1300,Color.RED,this);
}
public static void main(String[] args) {
test t=new test();
t.setVisible(true);
}
}
代码如上!
我想为界面添加背景! 但为何背景添加后其它组件(按钮,文本筐)则无法显示
请问如何解决,还有paintComponents在什么时候会自动调用?