Share on Google+Share on Google+

L.Bala Subrahmanyam
labels,textfields,buttons
1 Answer(s)      5 years and 10 months ago
Posted in : Java Beginners

how to set the labels,textfields,buttons at certain positions such that we can add them without layout managers? my program is to design a login page with image and these labels at exact positions.... thanks

Ads

View Answers

June 27, 2011 at 4:57 PM


1)LoginDemo.java:

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

class LoginDemo extends JFrame{
 JButton SUBMIT;
 JLabel label1,label2;
 final JTextField  text1,text2;
  LoginDemo(){
    setTitle("Login Form");
    setLayout(null);
    label1 = new JLabel();
    label1.setText("Username:");
    text1 = new JTextField(15);

    label2 = new JLabel();
    label2.setText("Password:");
    text2 = new JPasswordField(15);

    SUBMIT=new JButton("SUBMIT");
    label1.setBounds(350,100,100,20);
    text1.setBounds(450,100,200,20);
    label2.setBounds(350,130,100,20);
    text2.setBounds(450,130,200,20);
    SUBMIT.setBounds(450,160,100,20);
    add(label1);
    add(text1);
    add(label2);
    add(text2);
    add(SUBMIT);

   setVisible(true);
   setSize(1024,768);

 SUBMIT.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent ae){
    String value1=text1.getText();
    String value2=text2.getText();
    try{
    Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from login where username='"+value1+"' and password='"+value2+"'");
           int count=0;
           int id=0;
           while(rs.next()){
               id=rs.getInt("id");
               count++;
           }
 if(count>0){
     WelcomePage page=new WelcomePage(id);
     page.setVisible(true);
       }
 else {

     JOptionPane.showMessageDialog(null,"Error!");
     text1.setText("");
     text2.setText("");
     }
    }
    catch(Exception e){}
}
 });
  }
  public static void main(String arg[]){
  new LoginDemo();
}
}

2)WelcomePage.java:

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

class WelcomePage extends JFrame{
WelcomePage(int id){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root");
                        Statement stmt = con.createStatement();
                        ResultSet rs = stmt
                                        .executeQuery("select image from login where id="+id+"");
                        byte[] bytes = null;
                        if (rs.next()) {
                                bytes = rs.getBytes(1);
                        }
                        rs.close();
                        stmt.close();
                        con.close();
                        if (bytes != null) {
                                JFrame f = new JFrame();
                                Image image = f.getToolkit().createImage(bytes);
                                ImageIcon icon=new ImageIcon(image);
                                JLabel label=new JLabel(icon);
                                f.setLayout(null);
                                label.setBounds(10,10,100,100);
                                f.getContentPane().add(label);
                                f.setSize(500,300);
                                f.setVisible(true);
                        }
}
catch(Exception e){
    System.out.println(e);
}

}
}









Related Tutorials/Questions & Answers:
labels,textfields,buttons
labels,textfields,buttons  how to set the labels,textfields,buttons at certain positions such that we can add them without layout managers? my program is to design a login page with image and these labels at exact positions
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads