Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Janet
Gui
4 Answer(s)      4 years and 3 months ago
Posted in : Java Beginners


How can i set a background color in my code and also put the Gender panel in the middle or center of the Personal details and The Buttons the adding an image on the label Inpatients maintainance module and also increasing the size of the JTextArea for address so that the JScrollPane can be visible before entering details



import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.regex.*;

class InpatientDetails extends JFrame{
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,lPhoto,lN;
JTextField text1,text2,text3,text4,text5,text6,text7,text8,text9;
JTextArea area;
JScrollPane pane;
JButton b1,b2,b3,b4,b5,b6;
JRadioButton r1,r2;
JPanel p1,p2,p3,p4,p;
ButtonGroup bg;

public InpatientDetails(){
super("Nakasero Hospital Managment System [ Inpatient Maintance Module]");
setSize(650,500);
l1=new JLabel("Patient ID");
l2=new JLabel("First Name");
l3=new JLabel("Last Name");
l4=new JLabel("Phone Number");
l5=new JLabel("Place Of Residence");
l6=new JLabel("Date Of Birth");
l7=new JLabel("Marital Status");
l8=new JLabel("Next Of Kin");
l9=new JLabel("Age");
l10=new JLabel("Address");
l11=new JLabel("Gender");
lPhoto=new JLabel();
lPhoto.setIcon(new ImageIcon("image2.png"));
lN=new JLabel("Inpatient Maintenance Module");
lN.setFont(new Font("Lucida Handwriting",Font.BOLD,20));
lN.setForeground(Color.RED);
JPanel p3=new JPanel();
p3.add(lPhoto);
p3.add(lN);
text1=new JTextField(20);
text2=new JTextField(20);
text3=new JTextField(20);
text4=new JTextField(20);
text5=new JTextField(20);
text6=new JTextField(20);
text7=new JTextField(20);
text8=new JTextField(20);
text9=new JTextField(20);

area=new JTextArea();
pane=new JScrollPane(area);

b1=new JButton("Add");
b2=new JButton("Update");
b3=new JButton("First");
b4=new JButton("Last");
b5=new JButton("Next");
b6=new JButton("Previous");

r1=new JRadioButton("Male",false);
r2=new JRadioButton("Female",false);
bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
p=new JPanel();
p1=new JPanel(new GridLayout(11,2));
p2=new JPanel();

p4=new JPanel();
Border border1 = BorderFactory.createTitledBorder("Personal Details");
p1.setBorder(border1);
p1.add(l1);
p1.add(text1);
p1.add(l2);
p1.add(text2);
p1.add(l3);
p1.add(text3);
p1.add(l4);
p1.add(text4);
p1.add(l5);
p1.add(text5);

p1.add(l6);
p1.add(text6);
p1.add(l7);
p1.add(text7);
p1.add(l8);
p1.add(text8);
p1.add(l9);
p1.add(text9);
p1.add(l10);

p1.add(l11);
p1.add(pane);



Border border2 = BorderFactory.createTitledBorder("Gender");
p2.setBorder(border2);
p2.add(l11);
p2.add(r1);
p2.add(r2);
p4.add(b1);
p4.add(b2);
p4.add(b3);
p4.add(b4);
p4.add(b5);
p4.add(b6);
p.add(p3);
p.add(p1);
p.add(p2);
p.add(p4);
add(p);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


text1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text1.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Enter only Numeric Value!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
text2.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text2.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid First Name!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
text3.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text3.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid Last Name!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
text4.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text4.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Enter only Numeric Value!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

text5.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text5.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
text6.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text6.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Enter only Numeric Value!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
text7.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text7.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid Entry!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
text8.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text8.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid Entry!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
text9.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = text9.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Enter only Numeric Value!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

area.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = area.getText();
Pattern p = Pattern.compile("[&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid Address!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
}
public static void main(String[] args) {
new InpatientDetails();
}
}

Advertisement
View Answers

September 27, 2010 at 1:18 PM


Hi Friend,

Try the following code:

import javax.swing.*;
import java.awt.*;
import java.util.regex.*;
import java.awt.event.*;

class InpatientDetails extends JFrame{
JLabel lPhoto,lN,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10;
JTextField txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9;
JTextArea area;
SpringLayout lay;
JButton b1,b2,b3,b4,b5,b6;
JRadioButton r1,r2;
ButtonGroup bg;
JScrollPane pane;
public InpatientDetails(){
super("Nakasero Hospital Managment System [ Inpatient Maintance Module]");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
setSize(650,520);
Container cp = getContentPane();
cp.setBackground(Color.pink);

lPhoto=new JLabel();
lPhoto.setIcon(new ImageIcon("image2.png"));

lN=new JLabel("Inpatient Maintenance Module");
lN.setFont(new Font("Lucida Handwriting",Font.PLAIN,16));
lN.setForeground(Color.RED);

l1=new JLabel("Patient ID:");
txt1=new JTextField(20);

l2=new JLabel("First Name:");
txt2=new JTextField(20);

l3=new JLabel("Last Name:");
txt3=new JTextField(20);

l4=new JLabel("Phone Number:");
txt4=new JTextField(20);

l5=new JLabel("Place Of Residence:");
txt5=new JTextField(20);

l6=new JLabel("Date Of Birth:");
txt6=new JTextField(20);

l7=new JLabel("Marital Status:");
txt7=new JTextField(20);

l8=new JLabel("Next Of Kin:");
txt8=new JTextField(20);

l9=new JLabel("Address:");
area=new JTextArea(4,20);
pane=new JScrollPane(area);

l10=new JLabel("Gender:");

b1=new JButton("Add");
b2=new JButton("Update");
b3=new JButton("First");
b4=new JButton("Last");
b5=new JButton("Next");
b6=new JButton("Previous");

r1=new JRadioButton("Male",false);
r2=new JRadioButton("Female",false);
bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);

txt1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = txt1.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Enter only Numeric Value!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
txt2.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = txt2.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid First Name!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
txt3.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = txt3.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid Last Name!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});


September 27, 2010 at 1:18 PM


continue..

txt4.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = txt4.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Enter only Numeric Value!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

txt5.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = txt5.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
txt6.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = txt6.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Enter only Numeric Value!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
txt7.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = txt7.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid Entry!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
txt8.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = txt8.getText();
Pattern p = Pattern.compile("[0-9,&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid Entry!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
area.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = area.getText();
Pattern p = Pattern.compile("[&%$@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Invalid Address!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

add(lPhoto);
add(lN);
add(l1);
add(txt1);
add(l2);
add(txt2);
add(l3);
add(txt3);
add(l4);
add(txt4);
add(l5);
add(txt5);
add(l6);
add(txt6);
add(l7);
add(txt7);
add(l8);
add(txt8);
add(l9);
add(pane);
add(l10);
add(r1);
add(r2);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);

lay=new SpringLayout();


September 27, 2010 at 1:20 PM


continue..

lay=new SpringLayout();

lay.putConstraint(SpringLayout.WEST,lPhoto,200,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lPhoto,10,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lN,230,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lN,10,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l1,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l1,70,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt1,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt1,70,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l2,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l2,100,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt2,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt2,100,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l3,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l3,130,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt3,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt3,130,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l4,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l4,160,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt4,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt4,160,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l5,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l5,190,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt5,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt5,190,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l6,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l6,220,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt6,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt6,220,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l7,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l7,250,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt7,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt7,250,SpringLayout.NORTH,this);


September 27, 2010 at 1:21 PM


continue..

lay.putConstraint(SpringLayout.WEST,l8,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l8,280,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt8,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt8,280,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l9,130,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l9,310,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,pane,280,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,pane,310,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,l10,210,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,l10,400,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,r1,310,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,r1,400,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,r2,360,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,r2,400,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,b1,90,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,b1,450,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,b2,160,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,b2,450,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,b3,250,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,b3,450,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,b4,320,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,b4,450,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,b5,390,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,b5,450,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,b6,460,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,b6,450,SpringLayout.NORTH,this);
setLayout(lay);

}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new InpatientDetails().setVisible(true);
}
});
}
}

Thanks


Related Tutorials/Questions & Answers:
GUI
GUI  How to GUI in Net-beans ... ??   Please visit the following link: http://www.roseindia.net/java/java-tips/background/30java_tools/netbeans.shtml
GUI framework
GUI framework  what do u mean by GUI framework
Advertisements
GUI component
GUI component  How can a GUI component handle its own events
GUI problem
GUI problem  Create a class called CDProgram and write a GUI program to compute the amount of a certificate of deposit on maturity. The sample data follows: Amount deposited: 80000.00 Years: 15 Interest Rate: 7.75 Hint
gui question
gui question  design a gui application for me and write its code in which the user enters a no. in a textfield and onn clicking the button the sum of the digits of the no. should be displayed. hint: suppose the user enters 12
java gui
java gui   friends... good day.. i have doubt in java gui. ? i created 1 java gui application. That has two text fields jtext1,jtext2. case: user entered value in first textfield(jtext1) and pressed the enter key . the cursor
Convert the code to GUI
GUI code  GUI code
Java GUI
Java GUI  1) Using Java GUI, create a rectangular box that changes color each time a user click a change color button. 2) Modify Question 1 to include a new button named insert image, that allow user to insert a bitmap image
Convert the code to GUI
GUI Example  GUI Example code to learn
Convert the code to GUI
GUI Application example  GUI Application example
gui question
gui question  design a gui application and write code to accept a string from the user in a textfeild and print using option pane whether it is a palindrome or not. hint: abba is a palindrome   import java.awt.*; import
GUI problem
GUI problem  How do I make a Jbutton which is shaped like a circle. This button needs to be clicked in order to change color.   import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class
Convert the code to GUI
Java GUI Class Example  Java GUI Class Example
Convert the code to GUI
GUI Java JSP application  GUI Java JSP application
Convert the code to GUI
GUI Application Development   GUI Application Development
Convert the code to GUI
Java and GUI application Example  Java and GUI application Example
Convert the code to GUI
Write a GUI Application  best way to write a GUI based application
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
Convert the code to GUI
How to create GUI application in Java   How to create GUI application in Java
Convert the code to GUI
Java Code to GUI   can any one convert My code to GUI code
Netbeans GUI Ribbon
Netbeans GUI Ribbon  how to create ribbon task in java GUI using netbeans
Convert the code to GUI
Convert the code   How to convert a code to GUI look alike
Convert the code to GUI
Is it possible to convert a code into GUI  Is it possible to convert a code into GUI
Java GUI - Java Beginners
Java GUI  HOW TO ADD ICONS IN JAVA GUI PROGRAMMES
Magic Matrix in GUI
Magic Matrix in GUI  I want program in java GUI contain magic matrix for numbers
Regarding GUI Applications
Regarding GUI Applications  How to create a save and open jmenu item in java desktop application
how to refresh my GUI page
how to refresh my GUI page  how to refresh a GUI in java
Flex SDK GUI
Flex SDK GUI  Hi....... please give me ans of this question.. What classes do you typically extend in the Flex SDK for creating GUI controls and why? Thanks
GUI application program
GUI application program  Write a GUI application program that meets the following requirements: Create an array with 100 randomly chosen integers. Create a textfield to enter an array index and another textfield to display
Java GUI - Java3D
Java GUI  1) Using Java GUI, create a rectangular box that changes color each time a user click a change color button. 2) Modify Question 1 to include a new button named insert image, that allow user to insert a bitmap image
java gui-with jscroll pane
java gui-with jscroll pane  Dear friends.. I have a doubt in my gui application. I developed 1 application. In this application is 1 Jscrollpane of height 600 and width 400. Normally it is showing 200 height and 400 width
 

 

 

DMCA.com