bluej code does not switch to a Jframe third time - java

when i execute it the first jframe open then there are many jbuttons, I have only given a work or you can say the actionlistner e.It opens the second jframe that is about the same as the first jframe.But when I press the jbutton I have given the work it does not open any other jframe and it is also not finding the void A.
Here is my code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class GUI_window {
JFrame MF;
JFrame MFA;
JFrame ASF;
JButton ASB = new JButton("THE AREA...");
JTextField JTFASI = new JTextField("");
JLabel JLASR = new JLabel("Please type length of 1 side ");
JButton AS = new JButton("AREA OF A SQUARE");
JButton AR = new JButton("AREA OF A RECTANGLE");
JButton AT = new JButton("AREA OF A TRIANGLE");
JButton LR = new JButton("LENGTH OF A RECTANGLE WHEN YOU KNOW THE BREADTH AND THE AREA");
JButton BR = new JButton("BREATH OF A RECTANGLE WHEN YOU KNOW THE LENGTH AND THE AREA");
JButton OS = new JButton("LENGTH OF 1 SIDE OF A SQUARE WHEN YOU KNOW AND THE AREA");
JButton LT = new JButton("LENGTH OF 1 SIDE OF A TRIANGLE WHEN YOU KNOW LENGTH OF 2 SIDES AND THE AREA");
JButton B = new JButton("BACK");
JLabel JLA = new JLabel("Please tell what you want to know in the topic 'AREA'");
JLabel JLl = new JLabel("Please choose want to know what you ");
JButton F = new JButton("FUNDAMENTAL OPRATIONS");
JButton TA = new JButton("TABLES");
JButton T = new JButton("TEMPERATURE");
JButton E = new JButton("EQUATIONS");
JButton PAR = new JButton("POWER AND ROOTS");
JButton A = new JButton("AREA");
JButton P = new JButton("PERIMETER");
JButton C = new JButton("CIRCUMFERENCE");
JButton EXIT = new JButton("EXIT");
public GUI_window() {
MmF();
EXIT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
A.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new A();
MF.setVisible(false);
}
});
P.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
T.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
TA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
F.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
C.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
E.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
PAR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
}
void MmF() {
MF = new JFrame("MAIN MENU");
JPanel contentPane = (JPanel) MF.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new GridLayout(4, 4));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
JTextField JTF = new JTextField("");
contentPane.add(JLl, BorderLayout.NORTH);
contentPane.add(F);
contentPane.add(TA);
contentPane.add(T);
contentPane.add(E);
contentPane.add(PAR);
contentPane.add(P);
contentPane.add(A);
contentPane.add(C);
contentPane.add(EXIT);
MF.pack();
MF.setVisible(true);
}
void A() {
AmF();
B.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GUI_window();
MFA.dispose();
}
});
AS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ASa();
}
});
AT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
AR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
OS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
LR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
LT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
BR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
void AmF() {
MFA = new JFrame("AREA");
JPanel contentPane = (JPanel) MFA.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new GridLayout(4, 4));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
contentPane.add(JLA, BorderLayout.NORTH);
contentPane.add(AS);
contentPane.add(AR);
contentPane.add(AT);
contentPane.add(LR);
contentPane.add(BR);
contentPane.add(OS);
contentPane.add(LT);
contentPane.add(B);
MFA.pack();
MFA.setVisible(true);
}
void ASS() {
AmFas();
ASB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTFASI.setText(Maths.fforAS());
}
});
}
void AmFas() {
ASF = new JFrame("AREA");
JPanel contentPane = (JPanel) ASF.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new GridLayout(4, 4));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
contentPane.add(JLASR, BorderLayout.NORTH);
contentPane.add(JTFASI, BorderLayout.NORTH);
contentPane.add(ASB);
ASF.pack();
ASF.setVisible(true);
}
}

Related

layered pane bug where panels go haywire in debug

I am working on a tictactoe game using swing library in java; I made the UI using JLayeredpane and added panes containing buttons to represent a 3x3 matrix. the UI works alright but when I go in debug mode of IntelliJ the UI is all messed up. I am a newbie but I tried my best to find what causes the problem but could not find one. the same thing happens when I try to update points but in run time. I simply can't find what causes it.
error
what its supposed look
heres the code for the UIframe:
public class mainFrame {
JFrame frame= new JFrame("TicTackDIE");
JLayeredPane Container= new JLayeredPane();
static ImageIcon X = new ImageIcon("src/Webp.net-resizeimageX.png");
static ImageIcon O= new ImageIcon("src/Webp.net-resizeimageO.png");
static ImageIcon W= new ImageIcon("src/Webp.net-resizeimageBox.png");
static ImageIcon P=new ImageIcon();
static ImageIcon C=new ImageIcon();
static JLabel Points= new JLabel("YOU CPU");
int x=Ppoint;
int y=Cpoint;
JLabel YouPoints=new JLabel(String.valueOf(x));
JLabel CpuPoints=new JLabel(String.valueOf(y));
JButton container=new JButton();
JPanel AP1=new JPanel();
JPanel AP2=new JPanel();
JPanel AP3=new JPanel();
JPanel BP1=new JPanel();
JPanel BP2=new JPanel();
JPanel BP3=new JPanel();
JPanel CP1=new JPanel();
JPanel CP2=new JPanel();
JPanel CP3=new JPanel();
static JButton A1=new JButton();
static JButton A2=new JButton();
static JButton A3=new JButton();
static JButton B1=new JButton();
static JButton B2=new JButton();
static JButton B3=new JButton();
static JButton C1=new JButton();
static JButton C2=new JButton();
static JButton C3=new JButton();
static int Ppoint=0;
static int Cpoint=0;
static boolean A11=true;
static boolean A12=true;
static boolean A13=true;
static boolean B11=true;
static boolean B12=true;
static boolean B13=true;
static boolean C11=true;
static boolean C12=true;
static boolean C13=true;
mainFrame()
{
new BOX();
frame.setBounds(200,200,400,400);
Image icon_image=new ImageIcon("src/FAVPNG_red-skull-bone_kPu7iYJJ.png").getImage();
frame.setIconImage(icon_image);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
frame.add(Container);
Points.setBounds(122,15,200,19);
Points.setFont(new Font("San-Serif",Font.BOLD,17));
Container.add(Points,2);
YouPoints.setBounds(129,42,30,19);
YouPoints.setFont(new Font("San-Serif",Font.BOLD,15));
Container.add(YouPoints,3);
CpuPoints.setBounds(230,42,30,18);
CpuPoints.setFont(new Font("San-Serif",Font.BOLD,15));
Container.add(CpuPoints,4);
container.setIcon(new ImageIcon("src/TicTackToe.png"));
container.setBounds(0,0,390,380);
container.setFocusPainted(false);
container.setBorderPainted(false);
container.setContentAreaFilled(false);
Container.add(container,1);
//////
AP1.setBounds(84,82,67,67);
AP1.setLayout(null);
A1.setBounds(0,0,67,67);
A1.setIcon(W);
A1.setBackground(Color.white);
A1.setFocusPainted(false);
A1.setBorderPainted(false);
A1.setContentAreaFilled(false);
A1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(1,BOX.field[0][0],true);
new CPUmove(A11);
A11=false;
}
});
AP1.add(A1);
Container.add(AP1,0);
AP2.setBounds(160,82,67,67);
AP2.setLayout(null);
A2.setBounds(0,0,67,67);
A2.setIcon(W);
A2.setFocusPainted(false);
A2.setBorderPainted(false);
A2.setContentAreaFilled(false);
A2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(2,BOX.field[0][1],true);
new CPUmove(A12);
A12=false;
}
});
AP2.add(A2);
Container.add(AP2,0);
AP3.setBounds(236,82,67,67);
AP3.setLayout(null);
A3.setBounds(0,0,67,67);
A3.setIcon(W);
A3.setFocusPainted(false);
A3.setBorderPainted(false);
A3.setContentAreaFilled(false);
A3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(3,BOX.field[0][2],true);
new CPUmove(A13);
A13=false;
}
});
AP3.add(A3);
Container.add(AP3,0);
BP1.setBounds(84,158,67,67);
BP1.setLayout(null);
B1.setBounds(0,0,67,67);
B1.setIcon(W);
B1.setFocusPainted(false);
B1.setBorderPainted(false);
B1.setContentAreaFilled(false);
B1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(4,BOX.field[1][0],true);
new CPUmove(B11);
B11=false;
}
});
BP1.add(B1);
Container.add(BP1,0);
BP2.setBounds(160,158,67,67);
BP2.setLayout(null);
B2.setBounds(0,0,67,67);
B2.setIcon(W);
B2.setFocusPainted(false);
B2.setBorderPainted(false);
B2.setContentAreaFilled(false);
B2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(5,BOX.field[1][1],true);
new CPUmove(B12);
B12=false;
}
});
BP2.add(B2);
Container.add(BP2,0);
BP3.setBounds(236,158,67,67);
BP3.setLayout(null);
B3.setBounds(0,0,67,67);
B3.setIcon(W);
B3.setFocusPainted(false);
B3.setBorderPainted(false);
B3.setContentAreaFilled(false);
B3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(6,BOX.field[1][2],true);
new CPUmove(B13);
B13=false;
}
});
BP3.add(B3);
Container.add(BP3,0);
CP1.setBounds(84,233,67,67);
CP1.setLayout(null);
C1.setBounds(0,0,67,67);
C1.setIcon(W);
C1.setFocusPainted(false);
C1.setBorderPainted(false);
C1.setContentAreaFilled(false);
C1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(7,BOX.field[2][0],true);
new CPUmove(C11);
C11=false;
}
});
CP1.add(C1);
Container.add(CP1,0);
CP2.setBounds(160,233,67,67);
CP2.setLayout(null);
C2.setBounds(0,0,67,67);
C2.setIcon(W);
C2.setFocusPainted(false);
C2.setBorderPainted(false);
C2.setContentAreaFilled(false);
C2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(8,BOX.field[2][1],true);
new CPUmove(C12);
C12=false;
}
});
CP2.add(C2);
container.add(CP2,0);
CP3.setBounds(236,233,67,67);
CP3.setLayout(null);
C3.setBounds(0,0,67,67);
C3.setIcon(W);
C3.setFocusPainted(false);
C3.setBorderPainted(false);
C3.setContentAreaFilled(false);
C3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX(9,BOX.field[2][2],true);
new CPUmove(C13);
C13=false;
}
});
CP3.add(C3);
Container.add(CP3,0);
////
JButton clear = new JButton("clear");
JPanel Clear=new JPanel();
Clear.setLayout(null);
Clear.setBounds(155,320,70,17);
clear.setBorderPainted(false);
clear.setContentAreaFilled(false);
clear.setBounds(0,0,70,17);
clear.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new BOX();
}
});
Clear.add(clear);
Container.add(Clear,0);
}
public static void main(String[] args) {
new mainFrame();
}
}
I truly am stuck upon why this happens.
the same thing happens when i try to refresh the points on click of buttons.

how to set input text onscreen swing keyboard to system caret?

i want to write any text or key from swing onscreen keyboard to any caret location like browser notepad office word. i get some example layout, but dont know how to work to system caret. please teach me how it work.
this example code
public class keyboard extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
keyboard frame = new keyboard();
frame.setVisible(true);
frame.setAlwaysOnTop(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public keyboard() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(3, 3));
for(int i = 1; i <= 9; i++) {
createButton(Integer.toString(i));
}
pack();
}
private void createButton(String label)
{
JButton btn = new JButton(label);
btn.setFocusPainted(false);
btn.setPreferredSize(new Dimension(100, 100));
contentPane.add(btn);
}
public void actionPerformed(ActionEvent e)
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
}
});
}
}

Switching from one Jtable to another Jtable every x seconds

I have a simple gui, that is suppose to shows table(JTable). How to make it switch to table1(JTable) after x amount of seconds, and then back to table after x amount of seconds and have that run in a loop. I know setting the other table is nothing more than putting this line in:
scrollPane.setViewportView(table1);
but how to switch from one to the other and back periodically. Guess I have to use the Timer but need some help with this one.thanks
Here is the simple example code:
public class t extends JFrame {
private JPanel contentPane;
private JTable table;
private JTable table1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
t frame = new t();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public t() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable(10,10);
table1 = new JTable(10,5);
scrollPane.setViewportView(table);
}
}
THE FINAL RESULT:
public class t extends JFrame {
private JPanel contentPane;
private JTable table;
private JTable table1;
private Timer timer;
private Timer timer1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
t frame = new t();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public t() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable(10,10);
table1 = new JTable(10,5);
scrollPane.setViewportView(table);
ActionListener action = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
timer.stop();
scrollPane.setViewportView(table1);
timer1.start();
}
};
ActionListener action1 = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
timer1.stop();
scrollPane.setViewportView(table);
timer.start();
}
};
timer = new Timer(3000, action);
timer.start();
timer1 = new Timer(3000, action1);
}
}
here is the working example of what i wanted.thanks everyone for help

Generating non-repeating random methods in GUI

I've been spending so long looking at my computer monitor because I really don't know what to do to prevent the frames on my program on appearing simultaneously when I click the Start button.
Here's my main class:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;
public class PopQuizDemo {
public static void main (String args[]){
PopQuizDemo();
}
public static void PopQuizDemo(){
final SimpleFrame frame = new SimpleFrame();
final JPanel main = new JPanel();
main.setSize(400,75);
main.setLayout(new GridLayout(3,1));
frame.add(main);
JLabel l1 = new JLabel("Welcome to POP Quiz!");
main.add(l1);
JLabel l2 = new JLabel("Enter your name:");
main.add(l2);
final JTextField name = new JTextField ();
main.add(name);
final JPanel panel = new JPanel();
panel.setSize(400,50);
panel.setLocation(0,225);
frame.add(panel);
JButton start = new JButton ("Start");
panel.add(start);
frame.setVisible(true);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
randomize();
}
});
}
public static void randomize(){
Questions q = new Questions();
int a=0;
Random randnum = new Random (System.currentTimeMillis());
java.util.HashSet<Integer> myset = new java.util.HashSet<>();
for (int count = 1; count <= 3; count++){
while (true) {
a = randnum.nextInt (3);
if(!myset.contains(a)) { myset.add(new Integer(a)); break;}
}
if(a==0){
q.one();
}
else if(a==1){
q.two();
}
else if(a==2){
q.three();
}
else{
break;
}
}
}
}
And here is the class Question where I get the methods one(), two(), and three():
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Questions {
public static void one(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("In computers, what is the smallest and basic unit of");
JLabel qu2 = new JLabel();
qu2.setText("information storage?");
p1.add(qu1);
p1.add(qu2);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Bit");
p2.add(a);
JButton b = new JButton("b. Byte");
p2.add(b);
JButton c = new JButton("c. Data");
p2.add(c);
JButton d = new JButton("d. Newton");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
public static void two(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("Machine language is also known as __________.");
p1.add(qu1);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Low level language");
p2.add(a);
JButton b = new JButton("b. Assembly language");
p2.add(b);
JButton c = new JButton("c. High level language");
p2.add(c);
JButton d = new JButton("d. Source code");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
public static void three(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("What is the shortcut key of printing a document for");
JLabel qu2 = new JLabel();
qu2.setText("computers using Windows?");
p1.add(qu1);
p1.add(qu2);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Ctrl + P");
p2.add(a);
JButton b = new JButton("b. Shift + P");
p2.add(b);
JButton c = new JButton("c. Shift + PP");
p2.add(c);
JButton d = new JButton("d. Alt + P");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
}
The only problem here is that the when I call the method randomize() in the action listener in the Start button, it shows all the frames. Yes, they are not repeating but it shows simultaneously. I don't know where the problem is. Is it with the method randomize, the looping, the questions? Can someone help me? Please? Big thanks.
PS:
This is the class SimpleFrame
import javax.swing.JFrame;
public class SimpleFrame extends JFrame{
public SimpleFrame(){
setSize(400,300);
setTitle("Pop Quiz!");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setResizable(false);
}
}
You can use model dialog to show one after another using a for loop as show below:
public static void main(String[] args) {
JFrame m = new JFrame("Hello");
m.setSize(200,200);
m.setVisible(true);
for(int i=0;i<3;i++) {
JDialog dlg = new JDialog(m,"Dialog",true);
dlg.setSize(100,100);
dlg.show();
}
}

frame 2 inside frame 1

I have 2 classes; Students and RegisterStudents, and hence 2 different main_panel(Class 1) and panel_1 (Class 2). What I am trying to do is, when a button on the Students Interface is pressed, the whole panel_1 should appear within main_panel. I have set both to same size already. is that possible?
The code i got so far is:
JButton btnNewButton = new JButton("Register Student");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
Students main_panel = new Students();
RegisterStudent panel_1 = new RegisterStudent();
main_panel.add(panel_1);
}
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);
This isnt doing anything though? its compiling, but panel_1 is not actually appearing inside the main_panel. Has anyone got any suggestions?
JButton btnNewButton = new JButton("Register Student");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
Students main_panel = new Students();
RegisterStudent panel_1 = new RegisterStudent();
main_panel.add(panel_1);
panel.add(main_panel); // ADD THIS LINE
}
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);
You were initializing the new main_panel, and new panel_1, and adding panel_1 to main_panel but then you weren't doing anything with the new main_panel.
Also, I highly suggest naming your variables otherwise - these names are very non-intuitive.
For such things I would suggest you to use CardLayout
When you add something to the container, you must call revalidate() and repaint() methods to realize the changes made to it at RunTime. Like in your case you adding main_panel.add(panel_1);now after this you must perform
main_panel.revalidate();
main_panel.repaint();
frame.getRootPane().revalidate(); // for Upto JDK 1.6.
frame.revalidate(); // for JDK 1.7+
frame.repaint();
so that changes can be seen. A small code snippet to help you understand what I mean.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultiplePanels extends JFrame
{
private JPanel registrationPanel, loginPanel, searchPanel;
private JButton registerButton, loginButton, searchButton;
private ActionListener action;
public MultiplePanels()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
registrationPanel = new JPanel();
registrationPanel.setBackground(Color.WHITE);
loginPanel = new JPanel();
loginPanel.setBackground(Color.YELLOW);
searchPanel = new JPanel();
searchPanel.setBackground(Color.BLUE);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(0, 1));
buttonPanel.setBackground(Color.DARK_GRAY);
registerButton = new JButton("REGISTER");
loginButton = new JButton("LOGIN");
searchButton = new JButton("SEARCH");
buttonPanel.add(registerButton);
buttonPanel.add(loginButton);
buttonPanel.add(searchButton);
action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JButton button = (JButton) ae.getSource();
if (button == registerButton)
{
if (!(loginPanel.isShowing()) && !(searchPanel.isShowing()))
{
add(registrationPanel, BorderLayout.CENTER);
}
else
{
if (loginPanel.isShowing())
{
remove(loginPanel);
add(registrationPanel, BorderLayout.CENTER);
}
else if (searchPanel.isShowing())
{
remove(searchPanel);
add(registrationPanel, BorderLayout.CENTER);
}
}
}
else if (button == loginButton)
{
if (!(registrationPanel.isShowing()) && !(searchPanel.isShowing()))
{
add(loginPanel, BorderLayout.CENTER);
}
else
{
if (registrationPanel.isShowing())
{
remove(registrationPanel);
add(loginPanel, BorderLayout.CENTER);
}
else if (searchPanel.isShowing())
{
remove(searchPanel);
add(loginPanel, BorderLayout.CENTER);
}
}
}
else if (button == searchButton)
{
if (!(loginPanel.isShowing()) && !(registrationPanel.isShowing()))
{
add(searchPanel, BorderLayout.CENTER);
}
else
{
if (loginPanel.isShowing())
{
remove(loginPanel);
add(searchPanel, BorderLayout.CENTER);
}
else if (registrationPanel.isShowing())
{
remove(registrationPanel);
add(searchPanel, BorderLayout.CENTER);
}
}
}
// This is what we are doing here to realize the changes
// made to the GUI.
revalidate();
repaint();
}
};
registerButton.addActionListener(action);
loginButton.addActionListener(action);
searchButton.addActionListener(action);
add(buttonPanel, BorderLayout.LINE_START);
setSize(300, 300);
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new MultiplePanels();
}
});
}
}

Categories