Cách ẩn hoặc xóa đi toàn bộ những component trong Frame

Mình tạo một giao diện có 2 Button , 1 cái để khi kích vào nó hiện ra một giao diện Nhập Bill , cái kia kích vào nó hiện thị là giao diện những sản phẩm.Mình muốn tất cả hiện thị trên một frame thôi

Mình tính removeAll giao diện cũ rồi mình add trong mỗi button một giao diện mới , nhưng removeAll nó xóa hết rồi những vẫn hiển thị hình ảnh trên giao diện.

package bill;

import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class SalesManageGUI extends JFrame implements ActionListener
{
	
	private JLabel lbSB1,lbSB2, lbCus, lbDate, lbPay1,lbPay2,lbNote1,lbNote2,lbItem,lbNumber,lbTitle,lbTb1,lbTb2;
	private JTextField txtSB1,txtSB2,txtCus,txtDate,txtPay1,txtPay2,txtItem,txtNumber;
	private TextArea txtNote1,txtNote2;
	private JButton btnAdd,btnRemove, btnSort,btnSave,btnEdit,btnReset,btnFind,btnItem,btnBill;
	private JPanel pAll;
	JTable tMain1,tMain2;
	
	
	private JLabel lbTitle1,lbID,lbName,lbDis,lbNOI,lbNOP,lbPrice;
	private JTextField txtID,txtName,txtDis,txtNOI,txtNOP,txtPrice;
	private JPanel p1,p2,p3,p4,p5;
	private JTable tMain ;
	private JButton btnAdd2,btnEdit2,btnDelete2,btnInsert2,btnSave2 ;
	private String[] column = {"ID Item","Name Item","Distributor","Price","Number Of Inventory","Note"} ;
	private Object[][] data = new Object[50][50];
	private Item[] item = new Item[100] ;
	private Vector vTittle = new Vector() ;
	private Vector vMain = new Vector();
	private Vector vAdd ;
	private DefaultTableModel dtm = new DefaultTableModel() ;
	private int numberOfOrder ;
	int t = 0 ;
	
	
	public SalesManageGUI() {
		super("Sale Manage") ;
		super.setSize(1400,800);
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super.setVisible(true);
		super.setLayout( null);	
		
		
		btnItem = new JButton("Status Item") ;
		btnItem.setBounds(200,50,150,50);
		
		btnBill = new JButton("Bill Manage") ;
		btnBill.setBounds(50,50,150,50);
		
		super.add(btnItem) ;
		super.add(btnBill) ;

		
		
		btnBill.addActionListener( new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				// TODO Auto-generated method stub
	//			superClear();
				setFrame();
				Font fontLb=new Font("Time new Roman",Font.PLAIN,16);
				
				Font fontTb=new Font("Time new Roman",Font.BOLD, 22);
				lbTb1=new JLabel("Bill Table");
				lbTb1.setFont(fontTb);
				lbTb1.setBounds(870,150,300,30);
				
				lbTb2=new JLabel("Detail Bill Table");
				lbTb2.setFont(fontTb);
				lbTb2.setBounds(840,450,300,30);
				
				
				lbTitle=new JLabel("Sale Manage");
				Font font=new Font("Time new Roman",Font.BOLD,30);
				lbTitle.setFont(font);
				lbTitle.setBounds(650,20,400,35);
				
				lbSB1=new JLabel("Bill Serial");
				lbSB1.setFont(fontLb);
				lbSB1.setBounds(50,200,100,20);
				txtSB1=new JTextField();
				txtSB1.setBounds(150,200,150,25);
				
				lbCus=new JLabel("Customer");
				lbCus.setFont(fontLb);
				lbCus.setBounds(50,240,100,20);
				txtCus=new JTextField();
				txtCus.setBounds(150,240,150,25);
				
				lbDate=new JLabel("Date");
				lbDate.setFont(fontLb);
				lbDate.setBounds(50,280,50,20);
				txtDate=new JTextField();
				txtDate.setBounds(150,280,150,25);
				
				lbPay1=new JLabel("Payment");
				lbPay1.setFont(fontLb);
				lbPay1.setBounds(50,320,100,20);
				txtPay1=new JTextField();
				txtPay1.setBounds(150,320,150,25);
				
				lbNote1=new JLabel("Note");
				lbNote1.setFont(fontLb);
				lbNote1.setBounds(385,200,50,20);
				txtNote1=new TextArea();
				txtNote1.setBounds(320,240,170,105);
				
				
				String[] column1 = {"Ordinal Numbers","Bill ID","Customer","Date","Payment","Note"} ;
				Object[][] data1 = {
				} ;
				tMain1 = new JTable(data1,column1) ;
				JScrollPane table1 = new JScrollPane(tMain1) ;
				table1.setBounds(500,200,800,200);
				
				//txtPay,txtIdI,txtItem,txtNumber
				lbSB2=new JLabel("Item Serial");
				lbSB2.setFont(fontLb);
				lbSB2.setBounds(50,500,100,20);
				txtSB2=new JTextField();
				txtSB2.setBounds(150,500,150,25);
				
				lbItem=new JLabel("Item Name");
				lbItem.setFont(fontLb);
				lbItem.setBounds(50,540,100,20);
				txtItem=new JTextField();
				txtItem.setBounds(150,540, 150, 25);
				
				lbNumber=new JLabel("Number");
				lbNumber.setFont(fontLb);
				lbNumber.setBounds(50,580,100,20);
				txtNumber=new JTextField();
				txtNumber.setBounds(150,580,150,25);
				
				lbPay2=new JLabel("Payment");
				lbPay2.setFont(fontLb);
				lbPay2.setBounds(50,620,100,20);
				txtPay2=new JTextField();
				txtPay2.setBounds(150,620,150,25);
				
				lbNote2=new JLabel("Note");
				lbNote2.setFont(fontLb);
				lbNote2.setBounds(385,500,50,20);
				txtNote2=new TextArea();
				txtNote2.setBounds(320,540,170,105);
				
				
				//button
				
				btnAdd = new JButton("Add") ;
				btnAdd.setBounds(50,360,100,20);
				
				
				String[] column2 = {"Ordinal Numbers","Bill ID","Item","Number","Payment","Note"} ;
				Object[][] data2 = {
				} ;
				tMain2 = new JTable(data2,column2) ;
				JScrollPane table2 = new JScrollPane(tMain2) ;
				table2.setBounds(500,500,800,200);
				
				superAdd(btnItem);
				superAdd(btnAdd) ;
				superAdd(lbTitle);
				superAdd(lbSB1);
				superAdd(txtSB1);
				superAdd(lbCus);
				superAdd(txtCus);
				superAdd(lbDate);
				superAdd(txtDate);
				superAdd(lbPay1);
				superAdd(txtPay1);
				superAdd(lbNote1);
				superAdd(txtNote1);
				superAdd(lbSB2);
				superAdd(txtSB2);
				superAdd(lbItem);
				superAdd(txtItem);
				superAdd(lbNumber);
				superAdd(txtNumber);
				superAdd(lbPay2);
				superAdd(txtPay2);
				superAdd(lbNote2);
				superAdd(txtNote2);
				
				
				
				superAdd(lbTb1);
				superAdd(lbTb2);
				superAdd(table1);
				superAdd(table2);
//				t++ ;
			}
		});
		
		
		btnItem.addActionListener( new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
	//			superClear();
				setFrame();
				Font f=new Font("Times New Roman",Font.BOLD,30);
				lbTitle=new JLabel("ITEM");
				lbTitle.setBounds(650,10,100,30);//x,y,chieu rong,chieu cao
				lbTitle.setFont(f);
				
				
				lbID=new JLabel("ID Item ");
				lbID.setBounds(120,500,80,30);
				txtID = new JTextField(5);
				txtID.setBounds(200,500,150,30);
				
				
				lbName = new JLabel("Name Item ") ;
				lbName.setBounds(120,550,80,30);
				txtName = new JTextField(5);
				txtName.setBounds(200,550,150,30);
				
				
				lbPrice = new JLabel("Price ") ;
				lbPrice.setBounds(120,600,80,30);
				txtPrice = new JTextField(5);
				txtPrice.setBounds(200,600,150,30);
				
				lbDis = new JLabel("Distributor") ;
				lbDis.setBounds(400,500,80,30);
				txtDis = new JTextField(5);
				txtDis.setBounds(530,500,150,30);
				
				lbNOI = new JLabel("Number of inventory") ;
				lbNOI.setBounds(400,550,120,30);
				txtNOI = new JTextField(5);
				txtNOI.setBounds(530,550,150,30);
				
				
				//set Button
				btnAdd2 = new JButton("Add") ;
				btnAdd2.setBounds(120,700,100,40);
				
				btnEdit2 = new JButton("Edit");
				btnEdit2.setBounds(270,700,100,40);
				
				btnDelete2 = new JButton("Delete");
				btnDelete2.setBounds(420,700,100,40);
				
				btnInsert2 = new JButton("Insert");
				btnInsert2.setBounds(570,700,100,40);
				
				btnSave2 = new JButton("Save") ;
				btnSave2.setBounds(720,700,100,40);
				
				//setText for Table 
				 
				 tMain = new JTable(data,column) ;
				 JScrollPane table = new JScrollPane(tMain) ;
				 table.setBounds(100,60,1200,400);
				 
				 
				
				//b3:them cac components vao Jframe
				superAdd(table);
				superAdd(lbTitle);
				superAdd(lbName);
				superAdd(txtName);
				superAdd(lbID);
				superAdd(txtID);
				superAdd(lbPrice);
				superAdd(txtPrice);
				superAdd(lbDis);
				superAdd(txtDis);
				superAdd(lbNOI);
				superAdd(txtNOI);
				
				
				superAdd(btnAdd2);
				superAdd(btnSave2) ;
				superAdd(btnEdit2);
				superAdd(btnDelete2);
				superAdd(btnInsert2);
				
				title() ;
				
				
				btnAdd.addActionListener( new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						
						// TODO Auto-generated method stub
						item[numberOfOrder] = new Item(txtID.getText(),txtName.getText(),txtDis.getText(),Integer.parseInt(txtPrice.getText()),Integer.parseInt(txtNOI.getText()),0) ;
						addItem(item[numberOfOrder]);
						dtm.setDataVector(vMain,vTittle);
						tMain.setModel(dtm);
					}
				}) ;
	//			t++ ;
			}
		});
		t++ ;
		super.validate();
	}
	
	
	
	public void addItem(Item i) {
		numberOfOrder++ ;
		vAdd = new Vector() ;
		vAdd.add(i.getId())	;
		vAdd.add(i.getNameOfItem()) ;
		vAdd.add(i.getPrice()) ;
		vAdd.add(i.getDistribor()) ;
		vAdd.add(i.getNumberOfInventory()) ;
		
		
		vMain.add(vAdd) ;
	}
	
	
	
	public void superAdd(Component comp) {
		super.add(comp) ;
	}
	public void superClear() {
		super.removeAll(); 
		super.hide(); 
	}
	public void setFrame() {
		super.removeAll();
		super.hide();
		super.setTitle("Sales Manage");
		super.setSize(1400,800);
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super.setVisible(true);
		super.setLayout( null);	
		
		
		btnItem = new JButton("Status Item") ;
		btnItem.setBounds(200,50,150,50);
		
		btnBill = new JButton("Bill Manage") ;
		btnBill.setBounds(50,50,150,50);
		
		super.add(btnItem) ;
		super.add(btnBill) ;
		super.validate();
		
	}
	
	
	
	public void title() {
		vTittle.add("Serial Item") ;
		vTittle.add("Name Item") ;
		vTittle.add("Price Item") ;
		vTittle.add("Price") ;
		vTittle.add("Serial Item") ;
		vTittle.add("Serial Item") ;
	}
	
	

	
	public static void main(String[] args) {
		SalesManageGUI s= new SalesManageGUI();
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}

package bill;

import java.util.Scanner;

public class Item {
	
	protected String nameOfItem,Id,distribor ;
	protected int price ;
	protected int numberOfPurchase,numberOfInventory ;
	
	public Item() {
	}
	public Item(String id,String name,String distributor,int price,int purchase,int inventory) {
		setId(id);
		setNameOfItem(name);
		setDistribor(distributor);
		setPrice(price);
		setNumberOfInventory(inventory);
		setNumberOfPurchase(purchase);
	}
	
	

	public String getNameOfItem() {
		return nameOfItem;
	}
	public void setNameOfItem(String nameOfItem) {
		this.nameOfItem = nameOfItem;
	}



	public String getId() {
		return Id;
	}
	public void setId(String id) {
		Id = id;
	}



	public String getDistribor() {
		return distribor;
	}
	public void setDistribor(String distribor) {
		this.distribor = distribor;
	}



	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}



	public int getNumberOfPurchase() {
		return numberOfPurchase;
	}
	public void setNumberOfPurchase(int numberOfPurchase) {
		this.numberOfPurchase = numberOfPurchase;
	}



	public int getNumberOfInventory() {
		return numberOfInventory;
	}
	public void setNumberOfInventory(int numberOfInventory) {
		this.numberOfInventory = numberOfInventory;
	}
	
	
	
	public void input() {
		Scanner kb = new Scanner(System.in) ;
		System.out.print("Enter Id : ");
		this.setId(kb.next());
		System.out.print("Enter name of item  : ");
		this.setNameOfItem(kb.nextLine());
		kb.nextLine() ;
		System.out.print("Enter Distributor : ");
		this.setDistribor(kb.nextLine());
		System.out.println("Enter Price : ");
		this.setPrice(kb.nextInt());
		System.out.print("Enter number of inventory : ");
		this.setNumberOfInventory(kb.nextInt());
		System.out.print("Enter number of purchase : ");
		this.setNumberOfPurchase(kb.nextInt());
	}
	
	
	
	public void output() {
		System.out.println("ID : "+ this.Id);
		System.out.println("Name of Item : "+this.nameOfItem);
		System.out.println("Distributor : "+this.distribor);
		System.out.println("Number Of Inventory : "+this.numberOfInventory);
		System.out.println("Number Of Purchase : "+this.numberOfPurchase);
	}
	
	
	public double total() {
		return this.price*this.numberOfPurchase ;
	}
	

}

Cho 3 phần, mỗi phần vào 1 JPanel, cần hiện/ẩn cái nào thì setVisible() cho cái đó, việc gì phải loại bỏ hết, rồi tạo lại cho nó lung lên chứ.

2 Likes

Mình sửa lại rồi , bạn xem giúp mình thử

package bill;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class SalesManageGUI extends JFrame implements ActionListener
{
	
	
	private JButton btnItem,btnBill;
	private JPanel pAll;
	
	private JLabel lbTitle1,lbID,lbName,lbDis,lbNOI,lbNOP,lbPrice;
	private JTextField txtID,txtName,txtDis,txtNOI,txtNOP,txtPrice;
	private JTable tMain ;
	private JButton btnAdd3,btnEdit3,btnDelete3,btnInsert2,btnSave3 ;
	private String[] column = {"ID Item","Name Item","Distributor","Price","Number Of Inventory","Note"} ;
	private Object[][] data = new Object[50][50];
	private Item[] item = new Item[100] ;
	private Vector vTittle = new Vector() ;
	private Vector vMain = new Vector();
	private Vector vAdd ;
	private DefaultTableModel dtm = new DefaultTableModel() ;
	private int numberOfOrder ;
	private JPanel pMain = (JPanel) super.getContentPane(); 
	private JPanel pMain2 = (JPanel) super.getContentPane();
	
	private JLabel lbSB1,lbSB2, lbCus, lbDate, lbPay1,lbPay2,lbNote1,lbNote2,lbItem,lbNumber,lbTitle,lbTb1,lbTb2,lbFind,lbPnb,lbAdd;
	private JTextField txtSB1,txtSB2,txtCus,txtDate,txtPay1,txtPay2,txtItem,txtNumber,txtSum,txtNote1,txtNote2,txtFind,txtPnb,txtAdd;
	private JButton btnAdd1,btnEdit1,btnReset1,btnRemove1, btnSort,btnSum,btnSave,btnFind,btnAdd2,btnEdit2,btnReset2,btnRemove2;

	private JPanel pButton1,pButton2,JTable1,JTable2;
	JTable tMain1,tMain2;
	int t = 0 ;
	
	
	public SalesManageGUI() {
		super("Sale Manage") ;
		super.setSize(1400,800);
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super.setVisible(true);
		super.setLayout( null );	
		
		
		btnItem = new JButton("Status Item") ;
		btnItem.setBounds(200,50,150,50);
		
		
		btnBill = new JButton("Bill Manage") ;
		btnBill.setBounds(50,50,150,50);
		
		super.add(btnItem) ;
		super.add(btnBill) ;

//		pMain.setVisible(false);
//		pMain2.setVisible(false);
		
		btnItem.addActionListener(this);
		btnBill.addActionListener(this);
		super.validate();
/*				new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				// TODO Auto-generated method stub
				t++ ;
				
				pMain2.setVisible(false);
				pMain.setLayout(null);
				Dimension size ; 
				

				Font fontLb=new Font("Time new Roman",Font.PLAIN,16);
				
				Font fontTb=new Font("Time new Roman",Font.BOLD, 22);
				lbTb1=new JLabel("Bill Table");
				lbTb1.setFont(fontTb);
				pMain.add(lbTb1) ;
				size = lbTb1.getPreferredSize() ;
				lbTb1.setBounds(870,130,size.width,size.height);
				
				lbTb2=new JLabel("Detail Bill Table");
				lbTb2.setFont(fontTb);
				pMain.add(lbTb2) ;
				lbTb2.setBounds(840,450,300,30);
				
				
				lbTitle=new JLabel("Sale Manage");
				Font font=new Font("Time new Roman",Font.BOLD,30);
				lbTitle.setFont(font);
				pMain.add(lbTitle) ;
				
				lbTitle.setBounds(650,20,400,35);
				
				lbSB1=new JLabel("Bill Serial");
				lbSB1.setFont(fontLb);
				pMain.add(lbSB1) ;
				lbSB1.setBounds(50,200,100,20);
				
				txtSB1=new JTextField();
				pMain.add(txtSB1) ;
				txtSB1.setBounds(180,200,150,25);
				
				lbCus=new JLabel("Customer");
				lbCus.setFont(fontLb);
				pMain.add(lbCus) ;
				lbCus.setBounds(50,240,100,20);
				
				txtCus=new JTextField();
				pMain.add(txtCus) ;
				txtCus.setBounds(180,240,150,25);
				
				lbPnb=new JLabel("Phone Number");
				lbPnb.setFont(fontLb);
				pMain.add(lbPnb) ;
				lbPnb.setBounds(50,280,130,20);
				
				txtPnb=new JTextField();
				pMain.add(txtPnb) ;
				txtPnb.setBounds(180,280,150,25);
				
				lbAdd=new JLabel("Address");
				lbAdd.setFont(fontLb);
				pMain.add(lbAdd) ;
				lbAdd.setBounds(50,320,100,20);
				
				txtAdd=new JTextField();
				pMain.add(txtAdd) ;
				txtAdd.setBounds(180,320,150,25);

				
				lbDate=new JLabel("Date");
				lbDate.setFont(fontLb);
				pMain.add(lbDate) ;
				lbDate.setBounds(380,200,50,20);
				
				txtDate=new JTextField();
				pMain.add(txtDate) ;
				txtDate.setBounds(480,200,150,25);
				
				lbPay1=new JLabel("Payment");
				lbPay1.setFont(fontLb);
				pMain.add(lbPay1) ;
				lbPay1.setBounds(380, 240, 100, 20);
				
				txtPay1=new JTextField();
				pMain.add(txtPay1) ;
				txtPay1.setBounds(480, 240, 150, 25);
				
				lbNote1=new JLabel("Note");
				lbNote1.setFont(fontLb);
				pMain.add(lbNote1) ;
				lbNote1.setBounds(380, 280, 100, 20);
				
				txtNote1=new JTextField();
				pMain.add(txtNote1) ;
				txtNote1.setBounds(480,280,150,65);
				
				pButton1 = new JPanel();
				btnAdd1=new JButton("Add");
				btnEdit1=new JButton("Edit");
				btnReset1=new JButton("Reset");
				btnRemove1=new JButton("Remove");
				pButton1.add(btnAdd1);
				pButton1.add(btnEdit1);
				pButton1.add(btnReset1);
				pButton1.add(btnRemove1);
				pMain.add(pButton1) ;
				pButton1.setBounds(200, 398, 400, 40);
				
				String[] column1 = {"Ordinal Number","Bill Serial","Customer","Phone Number","Address","Date","Payment","Note"} ;
				Object[][] data1 = {
				} ;
				tMain1 = new JTable(data1,column1) ;
				JScrollPane table1 = new JScrollPane(tMain1) ;
				pMain.add(table1) ;
				table1.setBounds(700,200,800,200);
				
				btnSort=new JButton("Sort");
				pMain.add(btnSort) ;
				btnSort.setBounds(1199,176, 100, 25);

				btnSum=new JButton("Sum");
				pMain.add(btnSum) ;
				btnSum.setBounds(1197,398,100,25);
				
				txtSum=new JTextField();
				pMain.add(txtSum) ;
				txtSum.setBounds(1296,398,100,26);
				
				lbFind=new JLabel("Enter Bill Serial");
				pMain.add(lbFind) ;
				lbFind.setBounds(705,400,100,25);
				
				txtFind=new JTextField();
				pMain.add(txtFind) ;
				txtFind.setBounds(800,398,102,25);
				
				btnFind=new JButton("Find");
				pMain.add(btnFind) ;
				btnFind.setBounds(900,398,101,24);
				
				//txtPay,txtIdI,txtItem,txtNumber
				lbSB2=new JLabel("Bill Serial");
				lbSB2.setFont(fontLb);
				pMain.add(lbSB2) ;
				lbSB2.setBounds(50,500,100,20);
				
				txtSB2=new JTextField();
				pMain.add(txtSB2) ;
				txtSB2.setBounds(150,500,150,25);
				
				lbItem=new JLabel("Item Name");
				lbItem.setFont(fontLb);
				pMain.add(lbItem) ;
				lbItem.setBounds(50,540,100,20);
				
				txtItem=new JTextField();
				pMain.add(txtItem) ;
				txtItem.setBounds(150,540, 150, 25);
				
				lbNumber=new JLabel("Number");
				lbNumber.setFont(fontLb);
				pMain.add(lbNumber) ;
				lbNumber.setBounds(50,580,100,20);
				
				txtNumber=new JTextField();
				pMain.add(txtNumber) ;
				txtNumber.setBounds(150,580,150,25);
				
				lbPay2=new JLabel("Payment");
				lbPay2.setFont(fontLb);
				pMain.add(lbPay2) ;
				lbPay2.setBounds(50,620,100,20);
				
				txtPay2=new JTextField();
				pMain.add(txtPay2) ;
				txtPay2.setBounds(150,620,150,25);
				
				lbNote2=new JLabel("Note");
				lbNote2.setFont(fontLb);
				pMain.add(lbNote2) ;
				lbNote2.setBounds(380,500,50,20);
				
				txtNote2=new JTextField();
				pMain.add(txtNote2) ;
				txtNote2.setBounds(480,500,170,105);
				
				
				pButton2 = new JPanel();
				btnAdd2=new JButton("Add");
				btnEdit2=new JButton("Edit");
				btnReset2=new JButton("Reset");
				btnRemove2=new JButton("Remove");
				pButton2.add(btnAdd2);
				pButton2.add(btnEdit2);
				pButton2.add(btnReset2);
				pButton2.add(btnRemove2);
				pMain.add(pButton2) ;
				pButton2.setBounds(200, 680, 400, 40);
				
				String[] column2 = {"Ordinal Numbers","Bill Serial","Item","Number","Payment","Note"} ;
				Object[][] data2 = {
				} ;
				tMain2 = new JTable(data2,column2) ;
				JScrollPane table2 = new JScrollPane(tMain2) ;
				pMain.add(table2) ;
				table2.setBounds(700,500,800,200);
		
				
				superAdd(pMain) ;
				
				
				

			}
		});
		
		
		btnItem.addActionListener( new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				pMain.setVisible(false);
				Font f=new Font("Times New Roman",Font.BOLD,30);
				lbTitle=new JLabel("ITEM");
				lbTitle.setFont(f);
				pMain2.add(lbTitle) ;
				lbTitle.setBounds(650,10,100,30);//x,y,chieu rong,chieu cao
				
				
				lbID=new JLabel("ID Item ");
				pMain2.add(lbID) ;
				lbID.setBounds(120,500,80,30);
				
				txtID = new JTextField(5);
				pMain2.add(txtID) ;
				txtID.setBounds(200,500,150,30);
				
				
				lbName = new JLabel("Name Item ") ;
				pMain2.add(lbName) ;
				lbName.setBounds(120,550,80,30);
				
				txtName = new JTextField(5);
				pMain2.add(txtName) ;
				txtName.setBounds(200,550,150,30);
				
				
				lbPrice = new JLabel("Price ") ;
				lbPrice.setBounds(120,600,80,30);
				pMain2.add(lbPrice) ;
				
				txtPrice = new JTextField(5);
				pMain2.add(txtPrice) ;
				txtPrice.setBounds(200,600,150,30);
				
				lbDis = new JLabel("Distributor") ;
				lbDis.setBounds(400,500,80,30);
				pMain2.add(lbDis) ;
				
				txtDis = new JTextField(5);
				pMain2.add(txtDis) ;
				txtDis.setBounds(530,500,150,30);
				
				lbNOI = new JLabel("Number of inventory") ;
				pMain2.add(lbNOI) ;
				lbNOI.setBounds(400,550,120,30);
				
				txtNOI = new JTextField(5);
				pMain2.add(txtNOI) ;
				txtNOI.setBounds(530,550,150,30);
				
				
				//set Button
				btnAdd2 = new JButton("Add") ;
				pMain2.add(btnAdd2) ;
				btnAdd2.setBounds(120,700,100,40);
				
				btnEdit2 = new JButton("Edit");
				pMain2.add(btnEdit2) ;
				btnEdit2.setBounds(270,700,100,40);
				
				btnDelete3 = new JButton("Delete");
				btnDelete3.setBounds(420,700,100,40);
				
//				btnInsert2 = new JButton("Insert");
//				btnInsert2.setBounds(570,700,100,40);
				
				btnSave3 = new JButton("Save") ;
				pMain2.add(btnSave3) ;
				btnSave3.setBounds(720,700,100,40);
				
				//setText for Table 
				 
				 tMain = new JTable(data,column) ;
				 JScrollPane table = new JScrollPane(tMain) ;
				 pMain2.add(table) ;
				 table.setBounds(100,60,1200,400);
				 
				 
				
				//b3:them cac components vao Jframe
				
				superAdd(pMain2);
				
				title() ;
				
				
//				btnAdd.addActionListener( new ActionListener() {
//					@Override
//					public void actionPerformed(ActionEvent e) {
//						
//						// TODO Auto-generated method stub
//						item[numberOfOrder] = new Item(txtID.getText(),txtName.getText(),txtDis.getText(),Integer.parseInt(txtPrice.getText()),Integer.parseInt(txtNOI.getText()),0) ;
//						addItem(item[numberOfOrder]);
//						dtm.setDataVector(vMain,vTittle);
//						tMain.setModel(dtm);
//					}
//				}) ;
			}
		});
		*/
		
//		super.validate();
	}
	
	
	
	public void addItem(Item i) {
		numberOfOrder++ ;
		vAdd = new Vector() ;
		vAdd.add(i.getId())	;
		vAdd.add(i.getNameOfItem()) ;
		vAdd.add(i.getPrice()) ;
		vAdd.add(i.getDistribor()) ;
		vAdd.add(i.getNumberOfInventory()) ;
		
		
		vMain.add(vAdd) ;
	}
	
	
	
	public void superAdd(Component comp) {
		super.add(comp) ;
	}


	
	public void setFrame(JPanel j) {
		super.setTitle("Sales Manage");
		super.setSize(1400,800);
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super.setVisible(true);
		super.setLayout( null);	
		
		
		btnItem = new JButton("Status Item") ;
		btnItem.setBounds(200,50,150,50);
		
		btnBill = new JButton("Bill Manage") ;
		btnBill.setBounds(50,50,150,50);
		
		super.add(btnItem) ;
		super.add(btnBill) ;
		super.validate();
		
	}
	
	
	
	public void title() {
		vTittle.add("Serial Item") ;
		vTittle.add("Name Item") ;
		vTittle.add("Price Item") ;
		vTittle.add("Price") ;
		vTittle.add("Serial Item") ;
		vTittle.add("Serial Item") ;
	}
	
	

	
	public static void main(String[] args) {
		SalesManageGUI s= new SalesManageGUI();
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == btnItem) {
			pMain.setVisible(false);
			pMain2.setVisible(true);
			Font f=new Font("Times New Roman",Font.BOLD,30);
			lbTitle=new JLabel("ITEM");
			lbTitle.setFont(f);
			pMain2.add(lbTitle) ;
			lbTitle.setBounds(650,10,100,30);//x,y,chieu rong,chieu cao
			
			
			lbID=new JLabel("ID Item ");
			pMain2.add(lbID) ;
			lbID.setBounds(120,500,80,30);
			
			txtID = new JTextField(5);
			pMain2.add(txtID) ;
			txtID.setBounds(200,500,150,30);
			
			
			lbName = new JLabel("Name Item ") ;
			pMain2.add(lbName) ;
			lbName.setBounds(120,550,80,30);
			
			txtName = new JTextField(5);
			pMain2.add(txtName) ;
			txtName.setBounds(200,550,150,30);
			
			
			lbPrice = new JLabel("Price ") ;
			lbPrice.setBounds(120,600,80,30);
			pMain2.add(lbPrice) ;
			
			txtPrice = new JTextField(5);
			pMain2.add(txtPrice) ;
			txtPrice.setBounds(200,600,150,30);
			
			lbDis = new JLabel("Distributor") ;
			lbDis.setBounds(400,500,80,30);
			pMain2.add(lbDis) ;
			
			txtDis = new JTextField(5);
			pMain2.add(txtDis) ;
			txtDis.setBounds(530,500,150,30);
			
			lbNOI = new JLabel("Number of inventory") ;
			pMain2.add(lbNOI) ;
			lbNOI.setBounds(400,550,120,30);
			
			txtNOI = new JTextField(5);
			pMain2.add(txtNOI) ;
			txtNOI.setBounds(530,550,150,30);
			
			
			//set Button
			btnAdd2 = new JButton("Add") ;
			pMain2.add(btnAdd2) ;
			btnAdd2.setBounds(120,700,100,40);
			
			btnEdit2 = new JButton("Edit");
			pMain2.add(btnEdit2) ;
			btnEdit2.setBounds(270,700,100,40);
			
			btnDelete3 = new JButton("Delete");
			btnDelete3.setBounds(420,700,100,40);
			
//			btnInsert2 = new JButton("Insert");
//			btnInsert2.setBounds(570,700,100,40);
			
			btnSave3 = new JButton("Save") ;
			pMain2.add(btnSave3) ;
			btnSave3.setBounds(720,700,100,40);
			
			//setText for Table 
			 
			 tMain = new JTable(data,column) ;
			 JScrollPane table = new JScrollPane(tMain) ;
			 pMain2.add(table) ;
			 table.setBounds(100,60,1200,400);
			 
			 
			
			//b3:them cac components vao Jframe
			
			super.add(pMain2);
			
			title() ;
			
		}
		else if(e.getSource() == btnBill) {
			
//			pMain2.setVisible(false);
			pMain.setVisible(true);
			pMain2.hide() ;
			
			pMain.setLayout(null);
			Dimension size ; 
			

			Font fontLb=new Font("Time new Roman",Font.PLAIN,16);
			
			Font fontTb=new Font("Time new Roman",Font.BOLD, 22);
			lbTb1=new JLabel("Bill Table");
			lbTb1.setFont(fontTb);
			pMain.add(lbTb1) ;
			size = lbTb1.getPreferredSize() ;
			lbTb1.setBounds(870,130,size.width,size.height);
			
			lbTb2=new JLabel("Detail Bill Table");
			lbTb2.setFont(fontTb);
			pMain.add(lbTb2) ;
			lbTb2.setBounds(840,450,300,30);
			
			
			lbTitle=new JLabel("Sale Manage");
			Font font=new Font("Time new Roman",Font.BOLD,30);
			lbTitle.setFont(font);
			pMain.add(lbTitle) ;
			
			lbTitle.setBounds(650,20,400,35);
			
			lbSB1=new JLabel("Bill Serial");
			lbSB1.setFont(fontLb);
			pMain.add(lbSB1) ;
			lbSB1.setBounds(50,200,100,20);
			
			txtSB1=new JTextField();
			pMain.add(txtSB1) ;
			txtSB1.setBounds(180,200,150,25);
			
			lbCus=new JLabel("Customer");
			lbCus.setFont(fontLb);
			pMain.add(lbCus) ;
			lbCus.setBounds(50,240,100,20);
			
			txtCus=new JTextField();
			pMain.add(txtCus) ;
			txtCus.setBounds(180,240,150,25);
			
			lbPnb=new JLabel("Phone Number");
			lbPnb.setFont(fontLb);
			pMain.add(lbPnb) ;
			lbPnb.setBounds(50,280,130,20);
			
			txtPnb=new JTextField();
			pMain.add(txtPnb) ;
			txtPnb.setBounds(180,280,150,25);
			
			lbAdd=new JLabel("Address");
			lbAdd.setFont(fontLb);
			pMain.add(lbAdd) ;
			lbAdd.setBounds(50,320,100,20);
			
			txtAdd=new JTextField();
			pMain.add(txtAdd) ;
			txtAdd.setBounds(180,320,150,25);

			
			lbDate=new JLabel("Date");
			lbDate.setFont(fontLb);
			pMain.add(lbDate) ;
			lbDate.setBounds(380,200,50,20);
			
			txtDate=new JTextField();
			pMain.add(txtDate) ;
			txtDate.setBounds(480,200,150,25);
			
			lbPay1=new JLabel("Payment");
			lbPay1.setFont(fontLb);
			pMain.add(lbPay1) ;
			lbPay1.setBounds(380, 240, 100, 20);
			
			txtPay1=new JTextField();
			pMain.add(txtPay1) ;
			txtPay1.setBounds(480, 240, 150, 25);
			
			lbNote1=new JLabel("Note");
			lbNote1.setFont(fontLb);
			pMain.add(lbNote1) ;
			lbNote1.setBounds(380, 280, 100, 20);
			
			txtNote1=new JTextField();
			pMain.add(txtNote1) ;
			txtNote1.setBounds(480,280,150,65);
			
			pButton1 = new JPanel();
			btnAdd1=new JButton("Add");
			btnEdit1=new JButton("Edit");
			btnReset1=new JButton("Reset");
			btnRemove1=new JButton("Remove");
			pButton1.add(btnAdd1);
			pButton1.add(btnEdit1);
			pButton1.add(btnReset1);
			pButton1.add(btnRemove1);
			pMain.add(pButton1) ;
			pButton1.setBounds(200, 398, 400, 40);
			
			String[] column1 = {"Ordinal Number","Bill Serial","Customer","Phone Number","Address","Date","Payment","Note"} ;
			Object[][] data1 = {
			} ;
			tMain1 = new JTable(data1,column1) ;
			JScrollPane table1 = new JScrollPane(tMain1) ;
			pMain.add(table1) ;
			table1.setBounds(700,200,800,200);
			
			btnSort=new JButton("Sort");
			pMain.add(btnSort) ;
			btnSort.setBounds(1199,176, 100, 25);

			btnSum=new JButton("Sum");
			pMain.add(btnSum) ;
			btnSum.setBounds(1197,398,100,25);
			
			txtSum=new JTextField();
			pMain.add(txtSum) ;
			txtSum.setBounds(1296,398,100,26);
			
			lbFind=new JLabel("Enter Bill Serial");
			pMain.add(lbFind) ;
			lbFind.setBounds(705,400,100,25);
			
			txtFind=new JTextField();
			pMain.add(txtFind) ;
			txtFind.setBounds(800,398,102,25);
			
			btnFind=new JButton("Find");
			pMain.add(btnFind) ;
			btnFind.setBounds(900,398,101,24);
			
			//txtPay,txtIdI,txtItem,txtNumber
			lbSB2=new JLabel("Bill Serial");
			lbSB2.setFont(fontLb);
			pMain.add(lbSB2) ;
			lbSB2.setBounds(50,500,100,20);
			
			txtSB2=new JTextField();
			pMain.add(txtSB2) ;
			txtSB2.setBounds(150,500,150,25);
			
			lbItem=new JLabel("Item Name");
			lbItem.setFont(fontLb);
			pMain.add(lbItem) ;
			lbItem.setBounds(50,540,100,20);
			
			txtItem=new JTextField();
			pMain.add(txtItem) ;
			txtItem.setBounds(150,540, 150, 25);
			
			lbNumber=new JLabel("Number");
			lbNumber.setFont(fontLb);
			pMain.add(lbNumber) ;
			lbNumber.setBounds(50,580,100,20);
			
			txtNumber=new JTextField();
			pMain.add(txtNumber) ;
			txtNumber.setBounds(150,580,150,25);
			
			lbPay2=new JLabel("Payment");
			lbPay2.setFont(fontLb);
			pMain.add(lbPay2) ;
			lbPay2.setBounds(50,620,100,20);
			
			txtPay2=new JTextField();
			pMain.add(txtPay2) ;
			txtPay2.setBounds(150,620,150,25);
			
			lbNote2=new JLabel("Note");
			lbNote2.setFont(fontLb);
			pMain.add(lbNote2) ;
			lbNote2.setBounds(380,500,50,20);
			
			txtNote2=new JTextField();
			pMain.add(txtNote2) ;
			txtNote2.setBounds(480,500,170,105);
			
			
			pButton2 = new JPanel();
			btnAdd2=new JButton("Add");
			btnEdit2=new JButton("Edit");
			btnReset2=new JButton("Reset");
			btnRemove2=new JButton("Remove");
			pButton2.add(btnAdd2);
			pButton2.add(btnEdit2);
			pButton2.add(btnReset2);
			pButton2.add(btnRemove2);
			pMain.add(pButton2) ;
			pButton2.setBounds(200, 680, 400, 40);
			
			String[] column2 = {"Ordinal Numbers","Bill Serial","Item","Number","Payment","Note"} ;
			Object[][] data2 = {
			} ;
			tMain2 = new JTable(data2,column2) ;
			JScrollPane table2 = new JScrollPane(tMain2) ;
			pMain.add(table2) ;
			table2.setBounds(700,500,800,200);
	
			
			super.add(pMain) ;
		}
	}

}

Ơ, đùa à, 2 giá trị này là 1 thôi mà.
Bạn phải tạo các JPanel mới chứ.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?