将图像添加到我的面板,使其显示为背景但不显示

时间:2013-07-11 19:36:03

标签: java image swing jpanel embedded-resource

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
import java.util.*;

public class molwetsi extends JFrame implements ActionListener
{

  static JFrame frame;
  static JPanel panel;
  JLabel blabel, lhead, lRegistrationNumber, lpatientName, lRegistrationDate, 
  lpatientAddress, lCity, lContactNo, lStatus, lAge, lReligion, lNextOfKin, lGender,
  lMaritalStatus;
  JButton ADD, UPDATE, EDIT, SEARCH, EXIT;
  JComboBox Marital_status,Gender;
  JTextField Registration_Number, Registration_Date, Patient_Name,Patient_Address, City,
  Contact_No, Religion, Next_Of_Kin, Age, Status;
  private PreparedStatement ps;
  Connection conn;
  ImageIcon icon;
  JLabel image;

  public molwetsi()
  {
    //creating logainer
    Container loga = getContentPane();
    loga.setLayout(null);
    loga.setBackground(Color.white);
    /*create frame and panel*/
    panel=new JPanel();
    panel.setLayout(null);
    panel.setBounds(0,0,1000,1000);
    panel.setBackground(Color.cyan);
    panel.setOpaque(false);
    panel.setBorder(BorderFactory.createEtchedBorder());
    loga.add(panel);
    setTitle("Qangwa Hospital");
    /*image*/
    ImageIcon icon = new ImageIcon("C:\\Users\\Tangee\\Desktop\\redcross.gif");
    image = new JLabel(icon);
    image.setToolTipText("SGS Security");
    panel.add(image);

     /*create labels*/
    blabel = new JLabel(" ");
    lhead = new JLabel ("PATIENT ENTRY FORM");
    lhead.setFont(new java.awt.Font("Times New Roman", 1, 20)); 
    lhead.setBounds(10,0,600,25);
    lhead.setForeground(Color.black);
    panel.add(lhead);

    lRegistrationNumber = new JLabel ("  Registration Number");
    lRegistrationNumber.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lRegistrationNumber.setBounds(10,100,600,25);
    lRegistrationNumber.setForeground(Color.black);
    panel.add(lRegistrationNumber);
    // text field for regname
    Registration_Number= new JTextField();
    Registration_Number.setBounds(180,100,200,25);
    panel.add(Registration_Number);

    lpatientName = new JLabel(" Patient Name");
    lpatientName.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lpatientName.setBounds(10,140,400,25);
    lpatientName.setForeground(Color.black);
    panel.add(lpatientName);
    // text feild for patient name
    Patient_Name= new JTextField();
    Patient_Name.setBounds(180,140,200,25);
    panel.add(Patient_Name);

    lRegistrationDate = new JLabel(" Registration Date"); 
    lRegistrationDate.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lRegistrationDate.setBounds(500,100,400,25);
    lRegistrationDate.setForeground(Color.black);
    panel.add(lRegistrationDate);
    // text field for registration date
    Registration_Date= new JTextField();
    Registration_Date.setBounds(640,100,200,25);
    panel.add(Registration_Date);

    lpatientAddress = new JLabel (" Patient Address");
    lpatientAddress.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lpatientAddress.setBounds(10,180,400,25);
    lpatientAddress.setForeground(Color.black);
    panel.add(lpatientAddress);
    //text field for Patient address
    Patient_Address= new JTextField();
    Patient_Address.setBounds(180,180,200,25);
    panel.add(Patient_Address);

    lCity = new JLabel (" City");
    lCity.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lCity.setBounds(500,140,400,25);
    lCity.setForeground(Color.black);
    panel.add(lCity);

    //text field for City
    City = new JTextField();
    City.setBounds(640,140,200,25);
    panel.add(City);
    lContactNo = new JLabel (" Contact No");
    lContactNo.setFont(new java.awt.Font("Times New Roman", 1, 14));
    lContactNo.setBounds(500,180,400,25);
    lContactNo.setForeground(Color.black);
    panel.add(lContactNo);
    //text field for Contacts
    Contact_No = new JTextField();
    Contact_No.setBounds(640,180,200,25);
    panel.add(Contact_No);

    lStatus = new JLabel (" Status");
    lStatus.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lStatus.setBounds(10,220,400,25);
    lStatus.setForeground(Color.black);
    panel.add(lStatus);
    //text field for Status
    Status = new JTextField("how is the patient");
    Status.setBounds(180,220,200,25);
    panel.add(Status);

    lAge = new JLabel (" Age");
    lAge.setFont(new java.awt.Font("Times New Roman", 1, 14));
    lAge.setBounds(500,220,400,25);
    lAge.setForeground(Color.black);
    panel.add(lAge);
    //text field for Age
    Age = new JTextField("how many years is the patient");
    Age.setBounds(640,220,200,25);
    panel.add(Age);
    lReligion = new JLabel (" Religion");
    lReligion.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lReligion.setBounds(10,260,400,25);
    lReligion.setForeground(Color.black);
    panel.add(lReligion);
    //text field for Religion
    Religion = new JTextField();
    Religion.setBounds(180,260,200,25);
    panel.add(Religion);
    lNextOfKin = new JLabel (" Next Of Kin");
    lNextOfKin .setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lNextOfKin .setBounds(500,260,400,25);
    lNextOfKin .setForeground(Color.black);
    panel.add(lNextOfKin );
    //text field for Religion
    Next_Of_Kin = new JTextField();
    Next_Of_Kin.setBounds(640,260,200,25);
    panel.add(Next_Of_Kin);

    //create Gender label
    lGender= new JLabel("Gender"); 
    lGender.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lGender.setBounds(10,300,400,25);
    lGender.setForeground(Color.black);
    panel.add(lGender);
    // Create a Gender combobox
    Gender = new JComboBox();
    Gender.setBounds( 180,300,200,25);
    panel.add(Gender);
    //create Marital_Status label
    lMaritalStatus= new JLabel(" Marital_status "); 
    lMaritalStatus.setFont(new java.awt.Font("Times New Roman", 1, 14)); 
    lMaritalStatus.setBounds(500,300,400,25);
    lMaritalStatus.setForeground(Color.black);
    panel.add(lMaritalStatus);
    // Create Marital_Status  combobox
    Marital_status = new JComboBox();
    Marital_status.setBounds( 640,300,200,25);
    panel.add(Marital_status);
    // create ADD Button
    ADD=new JButton("ADD");
    ADD.setBounds(80,420,100,45);
    ADD.addActionListener(this);
    panel.add(ADD);
    // create UPDATE BUTTON
    UPDATE = new JButton("UPDATE");
    UPDATE.setBounds(210,420,100,45);
    UPDATE.addActionListener(this);
    panel.add(UPDATE);
    // create EDIT Button
    EDIT = new JButton("EDIT");
    EDIT.setBounds(340,420,100,45);
    EDIT.addActionListener(this);
    panel.add(EDIT);
    // create SEARCH Button
    SEARCH = new JButton("SEARCH");
    SEARCH.setBounds(470,420,100,45);
    SEARCH.addActionListener(this);
    panel.add(SEARCH);
    // create EXIT Button
    EXIT = new JButton("EXIT");
    EXIT.setBounds(600,420,100,45);
    EXIT.addActionListener(this);
    panel.add(EXIT);

  }
  public void close()
  {
    WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
  }

  /*calling method*/
  public static void main(String args [])
  {
    molwetsi q = new molwetsi();
    q.setSize(1000,600);
    q.setVisible(true);
    // q.setResizable(false);
    q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try {
      Connection conn=null;

      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      conn=DriverManager.getConnection("jdbc:odbc:DatabaseName","user", "password");
      JOptionPane.showMessageDialog(null, "JDBC driver loaded.");
    }
    catch(Exception e)
    {
      System.out.println(e);
    }
  }
  public void actionPerformed(ActionEvent e)
  {
    if
        (e.getSource()==ADD)
    {
      int result = JOptionPane.showConfirmDialog((Component) e.getSource(), "Are u sure you 
      want to proceed?");
      if (result == JOptionPane.YES_OPTION) 
      {
        try
        {
          conn=DriverManager.getConnection("jdbc:odbc:hospital","sa", "niit@123");
          ps = conn.prepareStatement("INSERT INTO Patient(Registration_Number,Patient_Name,
          Registration_Date,Patient_Address,City,Contact_No,Status,Age,Religion,Next_Of_Kin
          ) Values(?,?,?,?,?,?,?,?,?,?)");
          ps.setString(1, Registration_Number.getText());
          ps.setString(2, Patient_Name.getText());
          ps.setString(3, Registration_Date.getText());
          ps.setString(4, Patient_Address.getText());
          ps.setString(5, City.getText());
          ps.setString(6, Contact_No.getText());
          ps.setString(7, Status.getText());
          ps.setString(8, Age.getText());
          ps.setString(9, Religion.getText());
          ps.setString(10, Next_Of_Kin.getText());

          ps.executeUpdate();
          JOptionPane.showMessageDialog(null, "Record added");
          //System.out.println("Record added");
        }
        catch(SQLException ex)
        {   
          JOptionPane.showMessageDialog(null, "your record exceeds the muximum limit.");
          //System.err.println("your record exceeds the muximum limit");  
        } // catch

      }
      else if (result == JOptionPane.NO_OPTION)
      {
        Registration_Number.setText(Registration_Number.getText());
        Patient_Name.setText(Patient_Name.getText());
        Registration_Date.setText(Registration_Date.getText());
        Patient_Address.setText(Patient_Address.getText());
        City.setText(City.getText());
        Contact_No.setText(Contact_No.getText());
        Religion.setText(Religion.getText());
        Next_Of_Kin.setText(Next_Of_Kin.getText());
        Age.setText(Age.getText());
        Status.setText(Status.getText());
      }
    }
    else if(e.getSource()==UPDATE)
    {
      Updateform b = new Updateform();
      b.setSize(600,400);
      b.setVisible(true);
      b.setResizable(false);
      //b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.dispose();
    }
    if(e.getSource()==EXIT)
    {
      System.exit(0);
    }
    else{}
  }
}

1 个答案:

答案 0 :(得分:0)

这里没有设置背景图像的代码。任

  • 使用ImageIO.read加载图片,覆盖paintComponent()并使用Graphics2D.drawRenderedImage(image)绘制图像,然后调用super.paintComponent()或
  • 使用JLayeredPane,使用LayoutManager将图标放在底层,使其大小与面板大小相同,然后将其余组件放在其上方的图层上