Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将图像添加到“我的面板”,使其显示为背景,但不显示_Java_Image_Swing_Jpanel_Embedded Resource - Fatal编程技术网

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

Java 将图像添加到“我的面板”,使其显示为背景,但不显示,java,image,swing,jpanel,embedded-resource,Java,Image,Swing,Jpanel,Embedded Resource,这里没有设置背景图像的代码。或者 使用ImageIO加载图像。读取,覆盖paintComponent(),并使用Graphics2D.drawRenderedImage(图像)绘制图像,然后调用super.paintComponent(),或 使用JLayeredPane,使用LayoutManager将图标放在底层,使其大小与面板大小一致,然后将其余组件放在其上面的层上 请重新格式化您的代码。1)为了更快获得更好的帮助,请发布一个(而不是>500 LOC!)。2) 请对代码、输入/输出和结构

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

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

请重新格式化您的代码。1)为了更快获得更好的帮助,请发布一个(而不是>500 LOC!)。2) 请对代码、输入/输出和结构化文档(如HTML或XML)使用代码格式。要执行此操作,请选择示例并单击消息发布/编辑表单上方的
{}
按钮。
ImageIcon icon=new-ImageIcon(“C:\\Users\\Tangee\\Desktop\\redcross.gif”)到部署时,这些资源很可能会成为一个资源。在这种情况下,必须通过
URL
而不是
File
访问资源。有关如何形成
URL
.1)
setLayout(null)的方法,请参见标签的javagui可能必须在许多平台上工作,在不同的屏幕分辨率上&使用不同的plaf。因此,它们不利于部件的精确放置。要为健壮的GUI组织组件,请使用布局管理器或它们的组合,以及空白的布局填充和边框。2) 请学习类、方法和属性名称的通用(特别是用于名称的大小写)并一致使用它们。在简短、完整的过程中只关注问题的图像方面。使用
JLabel
,直接放置在
框架的内容窗格上。将
JLabel
的图标设置为背景图像,将布局管理器设置为标签并向其添加组件。。。
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{}
  }
}