Results 1 to 2 of 2
- 02-17-2013, 05:36 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 1
- Rep Power
- 0
Subframe, clear and exit button problems
Greetings everyone, i made a program about adding customer details into a notepad. However, i do not know how to create subframe from the main class n i do not know how to make the 'clear' and 'exit' button to work.
just ignore the 'search', 'list' and 'delete' button from the main class.
This is the main class code:
/////////////////////////////
import java.awt.*;
import javax.swing.*;
public class main_menu
{
private JFrame frame;
private JPanel p1, p2;
private JButton badd, bsearch, blist, bdelete, bexit;
private JLabel nama;
public static void main(String[]args)
{
main_menu gui=new main_menu();
gui.maukali();
}
public void maukali()
{
frame=new JFrame("Online Book Store System");
frame.setLayout(new BorderLayout(9,9)); //frame.setLayout(new FlowLayout());
p1=new JPanel();
p1.setLayout(new GridLayout(11,1));
nama=new JLabel("Welcome my Online Book Store System");
badd=new JButton("Add");
bsearch=new JButton("Search");
blist=new JButton("List");
bdelete=new JButton("Delete");
bexit=new JButton("Exit");
p1.add(nama);
p1.add(new JLabel("Press 'Add' if you want to add a book to the store"));
p1.add(badd);
p1.add(new JLabel("Press 'Search' if you want to search a book in the store"));
p1.add(bsearch);
p1.add(new JLabel("Press 'List' if you want to see all books in the store"));
p1.add(blist);
p1.add(new JLabel("Press 'Delete' if you want to delete a book"));
p1.add(bdelete);
p1.add(new JLabel("Press 'Exit' if you want to exit the program"));
p1.add(bexit);
//p2=new JPanel();
//p2.setLayout(new GridLayout(3,5));
//p2.add(blist);
//p2.add(bdelete);
//p2.add(bexit);
frame.add(p1, BorderLayout.CENTER);
//frame.add(p2, BorderLayout.CENTER);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
}
}
/////////////////////////////
This is subframe class code:
/////////////////////////////
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class main_menu
{
JFrame frame;
JLabel lid, lname, lprice, lqty, lstatus, des;
JButton badd, bclear, bback;
JTextField txtid, txtname, txtprice, txtqty;
JPanel p1, p2, despanel;
public main_add()
{
//path="Books.mdb";
frame=new JFrame("Add a book");
frame.setLayout(new BorderLayout(9,9));
p1=new JPanel();
p1.setLayout(new GridLayout(5,2));
lid=new JLabel("Book ID:");
lname=new JLabel("Book Title:");
lprice=new JLabel("Price:");
lqty=new JLabel("Quantity:");
lstatus=new JLabel("Status:");
txtid=new JTextField(25);
txtname=new JTextField(25);
txtprice=new JTextField(25);
txtqty=new JTextField(25);
badd=new JButton("Add");
bclear=new JButton("Clear");
bback=new JButton("Back");
p1.add(lid);
p1.add(txtid);
p1.add(lname);
p1.add(txtname);
p1.add(lprice);
p1.add(txtprice);
p1.add(lqty);
p1.add(txtqty);
p1.add(badd);
p1.add(bclear);
p2=new JPanel();
p2.setLayout(new GridLayout(2,5));
p2.add(bback);
p2.add(lstatus);
despanel=new JPanel();
despanel.setLayout(new FlowLayout());
des=new JLabel("Choose a book you want to add");
frame.add(des, BorderLayout.NORTH);
frame.add(p1, BorderLayout.CENTER);
frame.add(p2, BorderLayout.SOUTH);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
badd.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent event)
{
try
{
Add();
}
catch(Exception e)
{
lstatus.setText("Status: Error - " +e);
}
}
}
);
}
public void Add() throws Exception
{
int id=0;
String title;
double qty;
double price;
File sfile;
PrintWriter output;
sfile=new File("Books.txt");
if (!sfile.exists())
{
sfile.createNewFile();
lstatus.setText("Status: New file \"" +sfile.getName()+ "\" has been created to the \n"
+"current directory\n");
output=new PrintWriter(sfile);
id=1;
}
else
{
Scanner finput=new Scanner(sfile);
finput.useDelimiter("[,\\n]");
while(finput.hasNext())
{
id=finput.nextInt();
title=finput.next();
price=Double.parseDouble(finput.next());
qty=Double.parseDouble(finput.next());
//qty=finput.nextInt();
//gender=(finput.next()).charAt(0);
//amt=Double.parseDouble(finput.next());
}
id=id+1;
txtid.setText(Integer.toString(id));
boolean append=true;
FileWriter fwriter=new FileWriter(sfile, append);
output=new PrintWriter(fwriter);
}
id=Integer.parseInt(txtid.getText());
title=txtname.getText();
qty=Double.parseDouble(txtprice.getText());
//qty=Integer.parseInt(txtqty.getText());
price=Double.parseDouble(txtprice.getText());
output.println(id+","+title+","+qty+","+price);
output.close();
lstatus.setText("Status: One Records Added");
}
public static void main(String[]args)
{
menu_add add=new menu_add();
}
}
/////////////////////////////
any help will be appreciatedLast edited by dhiy0; 02-17-2013 at 05:39 PM. Reason: forgot something
-
Re: Subframe, clear and exit button problems
Please edit your post above and wrap the code in code tags:
BB Code List - Java Programming Forum - Learn Java Programming
As for opening a second window, I would not open a second JFrame as that would mean your application would have two main windows, an unusual and usually undesirable situation. I'd recommend instead that you either use a dialog window such as a JOptionPane or a JDialog, or else swap views in the current main window using a JDialog. The Java Swing tutorials can help you learn how to do this as well as help you with your other questions as well, and Google can help you find these tutorials.
Similar Threads
-
A problem about pushing a exit button to exit my java swing program
By blackdiz in forum New To JavaReplies: 7Last Post: 06-27-2012, 05:30 PM -
Solving Clear Button
By Ryan10 in forum New To JavaReplies: 73Last Post: 04-13-2011, 05:04 AM -
Clear/Reset Button Problem
By Ryan10 in forum New To JavaReplies: 10Last Post: 04-12-2011, 03:04 PM -
Clear Radio Button
By Reborn in forum New To JavaReplies: 6Last Post: 07-25-2010, 05:21 PM -
Creating Clear and Exit Buttons
By flambookey in forum New To JavaReplies: 2Last Post: 03-24-2010, 05:08 PM
Bookmarks