4
Why every time I use new
in my class, for example:
if(e.getSource() == levelButton) {
new PainelNivel().setVisible(true);
}
Java executes this command, i.e., shows the class Panel and opens the class from which this command was executed. Now, why does this happen?
NOTE: I’ve done this same procedure in the "constructor Jframe’s" of Netbeans and it worked. Why now is not giving?
package main;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Window extends JFrame implements ActionListener {
// Variáveis
JButton generateButton = new JButton("Gerar outro número");
JButton testButton = new JButton("Verificar");
JButton levelButton = new JButton("Nível");
private final JLabel label = new JLabel("Digite um número de 1 a 1000.");
JTextField campotext = new JTextField(27);
private final Object[] ops = new Object[3];
private int i, limit, num, palp, c;
// Construtor
public Window() {
super("Adivinha Número 1.0");
setSize(420, 120);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(label);
add(campotext);
add(testButton);
add(generateButton);
add(levelButton);
testButton.addActionListener(this);
generateButton.addActionListener(this);
levelButton.addActionListener(this);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
Generate();
}
// Gerador
private void Generate() {
Random gen = new Random();
num = gen.nextInt(1001);
}
// Teste
private void Test(int palp) {
if(palp < num) {
JOptionPane.showMessageDialog(this, "Errou, o número é maior");
} else if(palp > num) {
JOptionPane.showMessageDialog(this,"Errou, o número é menor");
} else {
JOptionPane.showMessageDialog(this,"Parabéns, voce acertou o numero!");
campotext.setEditable(false);
}
}
public int SetLimit(int l) {
this.limit = l;
return l;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == testButton) {
palp = Integer.parseInt(campotext.getText());
Test(palp);
if(i == limit) {
ops[0] = "Fechar";
ops[1] = "Gerar Outro Número";
ops[2] = "Mudar Nível";
c = JOptionPane.showOptionDialog(this, "Você chegou ao limite de tentativas!", "Game Over", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, ops, ops[0]);
if(c == JOptionPane.YES_OPTION) {
System.exit(0);
} else if(c == JOptionPane.NO_OPTION) {
Generate();
campotext.setText("");
i = 0;
} else if(c == JOptionPane.CANCEL_OPTION) {
new PainelNivel().setVisible(true);
}
} else {
i++;
System.out.println("Contador = " + i);
}
}
if(e.getSource() == generateButton) {
Generate();
campotext.setEditable(true);
campotext.setText("");
i = 0;
System.out.println("Contador = " + i);
}
if(e.getSource() == levelButton) {
new PainelNivel().setVisible(true);
}
}
public static void main(String[] args) {
new Window();
}
}
Class code Panel:
package main;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class PainelNivel extends JDialog implements ActionListener {
private Window w = new Window();
private final JLabel inst = new JLabel("Selecione o nível de dificuldade:");
private final JButton f = new JButton("Fácil (50 Tentativas)");
private final JButton m = new JButton("Médio (30 Tentativas)");
private final JButton d = new JButton("Difícil (10 Tentativas)");
public PainelNivel() {
setTitle("Seletor De Nível");
setSize(270, 160);
setResizable(false);
setLocationRelativeTo(w);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLayout(new FlowLayout());
add(inst);
add(f);
add(m);
add(d);
f.addActionListener(this);
m.addActionListener(this);
d.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == f) {
w.SetLimit(50);
dispose();
} else if(e.getSource() == m) {
w.SetLimit(30);
dispose();
} else if (e.getSource() == d) {
w.SetLimit(10);
dispose();
}
}
}
printStackTrace > Panel:
java.lang.Throwable: Printing stack trace:
at com.sun.corba.se.impl.util.Utility.printStackTrace(Utility.java:933)
at main.Window.<init>(Window.java:48)
at main.PainelNivel.<init>(PainelNivel.java:13)
at main.Window.actionPerformed(Window.java:116)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.lang.Throwable: Printing stack trace:
at com.sun.corba.se.impl.util.Utility.printStackTrace(Utility.java:933)
at main.PainelNivel.<init>(PainelNivel.java:36)
at main.Window.actionPerformed(Window.java:116)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Well, Painelnivel is a Jdialog, and as far as I know Jdialog is a subclass of Component, right? To clarify I will post the code of the Panel Checklevel in the answer, ok?
– Dongabi
Yes @Dongabi. So please update your question with the bug trace stack and more information about
PainelNivel
. Thank you.– Bruno César
Ready! - @Bruno_césar
– Dongabi
It’s kind of hard to understand what you’re doing young. You have a class
Window
, that treats some events and in some event creates an instance ofPainelNivel
, right? InPainelNivel
you create aWindow
and says that thePainelNivel
is related to theWindow
? o_O, man, crazy, it’s hard to understand the flow of your application. Post the classWindow
to see if I can better understand– Bruno César
Ready - @Bruno_césar
– Dongabi
@Dongabi edited the answer considering some points, see if it works with you too. Good studies :)
– Bruno César
Po vey, thanks a lot! It worked out! It’s just that I’m starting now, you know?... More worth brother! : D
– Dongabi
Come on, man, just need some value.
– Bruno César