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.
Related
I want to ask what function or another have to write so that every time I pressed the Start button (function:addbutton), the other button to exit (function:addButton2) to change its title to how many times I press the start?
class DroppingFrame extends JFrame {
public DroppingFrame() {
int clicked=0;
String b="a";
setSize(1400, 700);
setTitle("Dropping");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Drop ball", clicked, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//addButton.setText(String.valueOf(++clicked));
Ball b = new Ball(canvas);
// if(b== new Ball(canvas)){
// clicked++;
// }
b.start();
}
});
addButton2(p, b, clicked, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}
public void addButton(Container c, String title, int i, ActionListener a) {
//i++;
//title = Integer.toString(i);
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
public void addButton2(Container c, String title, int i, ActionListener a ) {
i++;
title = Integer.toString(i);
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
private JPanel canvas;
}
My preference would be:
Make clicked a class field - that way you can access it and mutate it inside the event handler
Create a class field for a button
Refactor the addButton method so there is only one of them - this makes your code tidier:
Change the method return type to return the button created, then you decide if you store it or not from the caller. This just smells nicer.
The code looks like:
class DroppingFrame extends JFrame {
final JPanel canvas = new JPanel();
JButton button2;
int clicked = 0;
public DroppingFrame() {
setSize(1400, 700);
setTitle("Dropping");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Drop ball", clicked, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Ball b = new Ball(canvas);
b.start();
button2.setText(String.valueOf(++clicked));
}
});
button2 = addButton(p, String.valueOf(clicked), clicked, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}
public JButton addButton(Container c, String title, int i, ActionListener a) {
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
return b;
}
}
It requires the reference of the second button so that start button can update its text.
Keep it simple:
Use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); instead of adding WindowListener to close the window.
Use JFrame#dispose to close the JFrame programmatically.
Favor Composition over Inheritance It means if you are not overriding any logic/implementation of the existing class then don't extend it.
There is no meaning of creating separate method for adding each component. Either make the method generic or simply remove it.
Sample code:
public class DroppingFrame {
private int clicked = 0;
public DroppingFrame() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JPanel p = new JPanel();
final JButton btn2 = new JButton(String.valueOf(clicked));
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
frame.dispose();
}
});
JButton btn1 = new JButton("Drop ball");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btn2.setText(String.valueOf(++clicked));
}
});
p.add(btn1);
p.add(btn2);
contentPane.add(p, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
It's not correct.. addButton always creates a new button and its reference is lost.. you should make it class-scoped.
So make a class variable:
JButton button = new JButton();
Change your function:
public void addButton(Container c, String title, int i, ActionListener a) {
button.setText(title);
c.add(button);
button.addActionListener(a);
}
And also change your actionPerformed override:
public void actionPerformed(ActionEvent evt) {
button.setText(String.valueOf(++clicked));
....
});
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);
}
}
I found this code online and modified it and it stopped working. I'm thinking it has something to do with when I added the Jpanel but what I am doing works best with a JPanel. How do I make it that the events in the action performed if statements work?
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener {
static JPanel panel = new JPanel(new GridLayout(5, 5, 1, 1));
public GUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setSize(100, 100);
//setLocation(100, 100);
//Button 1
JButton button1 = new JButton("1");
button1.addActionListener(this);
panel.add(button1);
//Button 2
JButton button2 = new JButton("2");
button2.addActionListener(this);
panel.add(button2);
//Button 3
JButton button3 = new JButton("3");
button3.addActionListener(this);
panel.add(button3);
//Button 2
JButton button4 = new JButton("4");
button4.addActionListener(this);
panel.add(button4);
//Button 2
JButton button5 = new JButton("5");
button5.addActionListener(this);
panel.add(button5);
//Button 2
JButton button6 = new JButton("6");
button6.addActionListener(this);
panel.add(button6);
//Button 2
JButton button7 = new JButton("7");
button7.addActionListener(this);
panel.add(button7);
//Button 2
JButton button8 = new JButton("8");
button8.addActionListener(this);
panel.add(button8);
//Button 2
JButton button9 = new JButton("9");
button9.addActionListener(this);
panel.add(button9);
panel.setVisible(true);
}
public static void main(String[] args) {
new GUI();
JFrame f = new JFrame("Calc");
f.setContentPane(panel);
f.setSize(1000, 1000);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
ArrayList numbers = new ArrayList();
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("button1")) {
myMethod();
numbers.add(1);
System.out.println("1");
}
if (command.equals("button1")) {
numbers.add(2);
System.out.println("2");
}
}
public void myMethod() {
JOptionPane.showMessageDialog(this, "Hello, World!!!!!");
System.out.println("Hey");
}
}
You need to change the part actionPerformed as:
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("1")) {
myMethod();
numbers.add(1);
System.out.println("1");
}
if (command.equals("2")) {
numbers.add(2);
System.out.println("2");
}
}
Here, when a button is clicked, your e.getActionCommand() will give the constructor string. i.e. "1", "2" ,"3" and so on
reference
I put comments in code, but the first thing you should do is to read official tutorials. How to use Buttons
public class GUI /*extends JFrame implements ActionListener*/ {
//don't extend JFrame is you don't have too neither implement ActionListener in top-container classes that breaks single responsability principle
private JPanel panel = new JPanel(new GridLayout(5, 5, 1, 1)); // why static??
public JPanel getPanel(){
return panel;
}
public GUI() {
//i use anonymous classes for this, then you don't have to use if-else
//Button 1
JButton button1 = new JButton("1");
button1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
myMethod();
numbers.add(1);
System.out.println("1");
}
});
panel.add(button1);
//Button 2
JButton button2 = new JButton("2");
button2.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
//put here logic for button2
}
});
panel.add(button2);
//and goo on with other buttons
//panel.setVisible(true); you don't need to call this!!
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI gui = new GUI();
frame.add(gui.getPanel());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private List<Integer> numbers = new ArrayList<>();//use generics!
public void myMethod() {
JOptionPane.showMessageDialog(this, "Hello, World!!!!!");
System.out.println("Hey");
}
}
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();
}
}
I have a JFrame which looks like a typical chat box. To connect and disconnect from the server I implemented a JPopupMenu and added a MouseListener to the main window.
However the popup menu does not appear.
Various JComponents in the main window:
JTextArea within a JScrollPane
JTextField to enter the message
Send button
JScrollPane and an ArrayList to display the users
The popup is supposed to appear no matter where you right click. Be it on the JTextArea or the field to enter your message.
To which all components do I add listeners and what listener do I add ?
Code
Variables:
private static A_Chat_Client chatClient;
public static String userName = "Anonymous";
//------------------------------------------------------------------------------
public static JFrame mainFrame = new JFrame();
public static JTextArea textArea = new JTextArea(30,30);
public static JScrollPane pane = new JScrollPane(textArea);
public static JTextField message = new JTextField(10);
public static JButton send = new JButton("Send");
public static JPopupMenu popup = new JPopupMenu();
public static JMenuItem connect = new JMenuItem("Connect");
public static JMenuItem disconnect = new JMenuItem("Disconnect");
public static JMenuItem help = new JMenuItem("Help");
public static JList usersOnline = new JList();
public static JScrollPane userPane = new JScrollPane(usersOnline);
Main method
public static void main(String[] args) {
buildMainWindow();
initialize();
addListeners();
popup.show(mainFrame, 0, 0); //forcefully popup
}
addListeners()
public static void addListeners(){
mainFrame.addMouseListener(new MouseHandler());
send.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
actionSend();
}
});
connect.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
actionConnect();
}
});
disconnect.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
actionDisconnect();
}
});
help.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
actionHelp();
}
});
}
class MouseHandler
private static class MouseHandler extends MouseAdapter{
#Override
public void mouseClicked(MouseEvent e){
if(e.isPopupTrigger()){
popup.show(mainFrame, e.getX(), e.getY());
}
}
}
You could attach an AWTEventListener to the main event queue using Toolkit#addAWTEventListener
You would then need to check the type of event your are receiving and check to see if it's popup event.
public class TestGlobalPopup {
public static void main(String[] args) {
new TestGlobalPopup();
}
public TestGlobalPopup() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
if (event instanceof MouseEvent) {
MouseEvent me = (MouseEvent) event;
if (me.isPopupTrigger()) {
Component component = me.getComponent();
JPopupMenu popup = new JPopupMenu();
popup.add(new JLabel("Clicked on " + component.getClass().getName()));
popup.show(component, me.getX(), me.getY());
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(new JLabel("Label: "), gbc);
gbc.gridx++;
add(new JTextField(10), gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
add(new JScrollPane(new JTextArea(5, 20)), gbc);
}
}
}