Sunday, December 29, 2013

How to remove a JPanel then add another one in, properly?
































































































































Ok, heres all of the necessary code.
































































































































































































Java Code:







































































































































package cyentw.utility.cutilities;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Launch extends JFrame implements ActionListener{

private JFrame frame;
private JButton button;
private JPanel panel;
private JPanel panel2;
private JPanel panel3 = new Panel3();
private JPanel panel4 = new Panel4();

public static void main(String[] args) {
new Launch();
}
public Launch()
{

frame = new JFrame();
panel = new JPanel();
panel2 = new JPanel();
button = new JButton("button.");

frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setTitle("CUtilities");
frame.setName("CUtilities");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

panel.setSize(500,500);
panel.setLayout(null);
frame.add(panel);

panel2.setSize(350,500);
panel2.setLayout(null);
panel2.setLocation(160,0);
panel2.setBackground(Color.white);
panel.add(panel2);

button.setSize(150,50);
button.setLocation(5, 15);
button.addActionListener(this);
panel.add(button);

panel2.add(panel4);

frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==give)
{
panel2.remove(panel4);
panel2.add(panel3);
panel2.revalidate();
panel2.repaint();
pack();
}
}
}

//CLASSES PANEL3 AND PANEL4 BOTH EXTEND JPANEL, HAVE A LAYOUTMANAGER OF NULL, HAVE LOCATION 0,0 AND SIZE 350,500, HAVE 2 COMPONENTS EACH, AND ARE SET TO VISIBLE.





































































































































Hope this helps :)






























































































































































































No comments:

Post a Comment