how to exit upper frame by click on ok and how to add back button when two page in single frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Prhr extends JFrame implements ActionListener,ItemListener
{
JLabel l1;
JLabel l2;
JTextField ar;
JTextField t;
JButton b1;
JButton b2;
JButton b3,b4;
JComboBox c1,c2;
JLabel l3;
Connection con;
Statement st;
JLabel l4;
JLabel l5;
JLabel l6;
JLabel l7,l8,l9,l10,l11,l12,l13;
Font f,f2,f1;
String s3,s4;
public Prhr()
{
c1=new JComboBox();
c1.addItem("....");
//*****************************************
try{
System.out.println("in");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/amit","root","1234");
System.out.println(con);
st=con.createStatement();
System.out.println(st);
ResultSet rs= st.executeQuery("select * from CHR");
while(rs.next())
{
c1.addItem(rs.getString("prnm"));
}
}catch(Exception e1)
{
e1.printStackTrace();
}
//****************************************
//JPanel p=new JPanel();
//j.add(p);
f2=new Font("Tahoma", 1, 20);
setFont(f2);
setTitle("CREATE PROCESS HIERARCHY");
f1=new Font("Tahoma", 1, 18);
//setFont(f1);
l1=new JLabel("Process Name");
l2=new JLabel("Description");
l3=new JLabel("Provide Process Detail");
l8=new JLabel("Parent Process");
l9=new JLabel("Processs Type");
ar=new JTextField();
t=new JTextField();
c2=new JComboBox();
b1=new JButton("Preview & Save");
b2=new JButton("Clear");
b3=new JButton("Save as Draft");
setLayout(null);
f=new Font("Tahoma", 1, 20);
//c1.addItem("....");
c2.addItem("Process");
//c1.addItem("1c1");
//c1.addItem("2c1");
c2.addItem("Activity");
//c2.addItem("2c2");
add(l3);
l3.setBounds(170, 70, 250, 20);
add(l1);
l3.setFont(f1);
l1.setBounds(100,130,100,20);
add(t);
t.setBounds(210, 130, 200, 20);
add(l2);
l2.setBounds(100, 175, 100, 20);
add(l8);
l8.setBounds(100, 240, 100, 20);
add(l9);
l9.setBounds(100, 285, 100, 20);
add(ar);
ar.setBounds(210, 175, 200, 40);
add(c1);
c1.setBounds(210, 240, 200, 20);
add(c2);
c2.setBounds(210, 285, 200, 20);
add(b1);
b1.setBounds(50, 365, 129, 20);
add(b2);
b2.setBounds(220, 365, 70, 20);
add(b3);
b3.setBounds(340, 365, 120, 20);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c1.addItemListener(this);
c2.addItemListener(this);
t.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!((c >= 65) && (c <= 90)||(c>=97)&&(c<=122) ||
/*(c == KeyEvent.VK_BACK_SPACE) ||*/(c==32)||
(c == KeyEvent.VK_DELETE))) {
// getToolkit().beep();
e.consume();
}
}
});
ar.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!((c >= 65) && (c <= 90)||(c>=97)&&(c<=122) ||
/*(c == KeyEvent.VK_BACK_SPACE) ||*/(c==32)
)) {
// getToolkit().beep();
e.consume();
}
}
});
//b4.addActionListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
s3=(String)c1.getSelectedItem();
System.out.println(s3);
s4=(String)c2.getSelectedItem();
System.out.println(s4);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource()==b1)
{
System.out.println("in b1");
final String s1=t.getText();
final String s2=ar.getText();
int q=s1.length();
int w=s2.length();
if(q<=20||q>0&&w<=200||w>0)
{
System.out.println(s3);
System.out.println(s4);
JFrame j=new JFrame();
j.setVisible(true);
//j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(500,500);
JPanel p=new JPanel();
j.add(p);
p.setLayout(null);
l4=new JLabel("Process Name");
l5=new JLabel(s1);
l6=new JLabel("Description");
l7=new JLabel(s2);
l10=new JLabel("Parent Process");
l11=new JLabel(s3);
l12=new JLabel("Process Type");
l13=new JLabel(s4);
b4=new JButton("OK");
p.add(l4);
l4.setBounds(100,130,100,20);
p.add(l5);
l5.setBounds(210, 130, 200, 20);
p.add(l6);
l6.setBounds(100, 175, 100, 20);
p.add(l7);
l7.setBounds(210, 175, 200, 40);
p.add(l10);
l10.setBounds(100, 240, 100, 20);
p.add(l11);
l11.setBounds(210, 240, 200, 20);
p.add(l13);
l13.setBounds(210, 285, 200, 20);
p.add(l12);
l12.setBounds(100, 285, 100, 20);
p.add(b4);
b4.setBounds(220, 365, 70, 20);
//**************88
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent d) {
if(d.getSource()==b4)
{
//System.exit(1);
dispose();
try
{
dispose();
System.out.println("in");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/amit","root","1234");
System.out.println(con);
st=con.createStatement();
System.out.println(st);
int i=st.executeUpdate("insert into CHR values('"+s1+"','"+s2+"','"+s3+"','"+s4+"')");
if(i>1)
{
con.close();
st.close();
}
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("inserted");
ResultSet rs= st.executeQuery("select * from CHR");
while(rs.next())
{
c1.addItem(rs.getString("prnm"));
}
}catch(Exception e)
{
e.printStackTrace();
}
//j.dispose();
}//b4 if close
}
});
}
else
{
String message = "\"Pls enter Name within 20 Chatarter\"\n"
+ "OR\n"
+ "Pls enter Description within 200 character";;
JOptionPane.showMessageDialog(new JFrame(), message, "Dialog",
JOptionPane.ERROR_MESSAGE);
}
}
else if (a.getSource()==b2)
{
System.out.println("in b2");
t.setText("");
ar.setText("");
c1.setSelectedItem("....");
c2.setSelectedItem("Process");
}
else if(a.getSource()开发者_运维百科==b3)
{
System.out.println("in b3");
}
}
public static void main(String args[])
{
Prhr pr=new Prhr();
pr.setVisible(true);
pr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pr.setSize(500,500);
}
}
I see in your actionPerformed method you are creating a new JFrame. I assume that is the second frame you are talking about. You might want to use a JDialog, but hard to say since I've no idea what your app does.
But to answer your question, to close a JFrame call:
j.setVisible(false);
精彩评论