Diskuze: JavaGUI nespustí se
V předchozím kvízu, Online test znalostí Java, jsme si ověřili nabyté zkušenosti z kurzu.
Petr Štechmüller:8.7.2017 19:55
Ahoj,
v dnešní době by se mělo GUI v Jave dělat pomocí JavaFX.
Tvůj kód jsem u sebe přeložil a spustil a okno se mi normálně
zobrazilo.
Můžeš zkusit ještě přidat tento řádek:
frame.pack();
Tady je odkaz na celý návod, jak tvořit GUI ve swingu.
Trochu jsem pozměnil kód
package test.DG.javaGUI;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JavaGUI
{
public static void main(String[] args)
{
int bonus = 0;
JFrame frame = new JFrame("JavaGUI");
JButton but0 = new JButton();
JButton but1 = new JButton();
CustomAct act1 = new CustomAct();
CustomBonus act2 = new CustomBonus(bonus);
but0.setAction(act1);
but0.setText("Klikni!");
but1.setAction(act2);
but1.setText("za 10$ \n+ 1 za kliknutí");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setLayout(new BorderLayout ());
frame.setResizable(false);
frame.add(but0, BorderLayout.SOUTH);
frame.add(but1, BorderLayout.EAST);
frame.add(act1.getLab(), BorderLayout.CENTER);
}
}
package test.DG.javaGUI;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JLabel;
public class CustomAct extends AbstractAction
{
private static final long serialVersionUID = 0;
JLabel lab = new JLabel("", JLabel.CENTER);
private int counter = 0;
private int bonus = 0;
private CustomBonus CB = new CustomBonus(bonus);
public CustomAct()
{
}
public void actionPerformed(ActionEvent e)
{
lab.setText(counter + "$");
lab.setForeground(Color.green);
counter++;
counter = counter + CB.getBonus();
}
public int getCounter()
{
return counter;
}
public JLabel getLab()
{
return lab;
}
}
package test.DG.javaGUI;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
public class CustomBonus extends AbstractAction
{
private static final long serialVersionUID = 0;
private int bonus = 0;
private int cena = 20;
private CustomAct CA = new CustomAct();
public CustomBonus(int bonus)
{
this.bonus = bonus;
}
public void actionPerformed(ActionEvent e)
{
if(CA.getCounter() < cena)
{
bonus++;
cena = cena + (cena / 10);
}
}
public int getBonus()
{
return bonus;
}
}
Ale když to zkompiluji a spustím jako jar soubor tak to hodí tuhle chybu
Exception in thread "main" java.lang.StackOverflowError
at java.util.Hashtable.get(Hashtable.java:363)
at javax.swing.UIDefaults.getFromHashtable(UIDefaults.java:173)
at javax.swing.UIDefaults.get(UIDefaults.java:161)
at javax.swing.MultiUIDefaults.get(MultiUIDefaults.java:58)
at javax.swing.UIDefaults.getColor(UIDefaults.java:417)
at javax.swing.UIManager.getColor(UIManager.java:701)
at javax.swing.LookAndFeel.installColors(LookAndFeel.java:175)
at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:211)
at javax.swing.plaf.basic.BasicLabelUI.installDefaults(BasicLabelUI.java:339)
at javax.swing.plaf.basic.BasicLabelUI.installUI(BasicLabelUI.java:324)
at javax.swing.JComponent.setUI(JComponent.java:666)
at javax.swing.JLabel.setUI(JLabel.java:261)
at javax.swing.JLabel.updateUI(JLabel.java:275)
at javax.swing.JLabel.<init>(JLabel.java:164)
at javax.swing.JLabel.<init>(JLabel.java:183)
at test.DG.javaGUI.CustomAct.<init>(CustomAct.java:13)
at test.DG.javaGUI.CustomBonus.<init>(CustomBonus.java:13)
at test.DG.javaGUI.CustomAct.<init>(CustomAct.java:16)
at test.DG.javaGUI.CustomBonus.<init>(CustomBonus.java:13)
at test.DG.javaGUI.CustomAct.<init>(CustomAct.java:16)
at test.DG.javaGUI.CustomBonus.<init>(CustomBonus.java:13)
at test.DG.javaGUI.CustomAct.<init>(CustomAct.java:16)
..........
Petr Štechmüller:9.7.2017 13:05
Vždyť tam máš rekurzivní vytváření instancí tříd CustomBonus a CustomAct, tak se nemůžeš divit, že to spadlo na StackOverflowError.
Já jsem asi slepej ale tu rekurzy tam nevidím.
Marian Benčat:9.7.2017 21:44
private CustomAct CA = new CustomAct();
...
private CustomBonus CB = new CustomBonus(bonus);
ano si.
Tak se to už spustí ale nefunguje tlačítko na přidání bodu za kliknutí
package test.DG.javaGUI;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JavaGUI
{
public static void main(String[] args)
{
JFrame frame = new JFrame("JavaGUI");
JButton but0 = new JButton();
JButton but1 = new JButton();
CustomAct act1 = new CustomAct();
CustomBonus act2 = new CustomBonus();
but0.setAction(act1);
but0.setText("Klikni!");
but1.setAction(act2);
but1.setText("za " + act2.getCena() + "$ \n+ 1 za kliknutí");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setLayout(new BorderLayout ());
frame.setResizable(false);
frame.add(but0, BorderLayout.SOUTH);
frame.add(but1, BorderLayout.EAST);
frame.add(act1.getLab(), BorderLayout.CENTER);
}
}
package test.DG.javaGUI;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JLabel;
public class CustomAct extends AbstractAction
{
private static final long serialVersionUID = 0;
JLabel lab = new JLabel("", JLabel.CENTER);
private int counter = 0;
public CustomAct()
{
}
public void actionPerformed(ActionEvent e)
{
CustomBonus CB = new CustomBonus();
lab.setText(counter + "$");
lab.setForeground(Color.green);
counter++;
counter = counter + CB.getBonus();
}
public int getCounter()
{
return counter;
}
public JLabel getLab()
{
return lab;
}
}
package test.DG.javaGUI;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
public class CustomBonus extends AbstractAction
{
private static final long serialVersionUID = 0;
private int bonus = 0;
private int cena = 10;
public CustomBonus()
{
}
public void actionPerformed(ActionEvent e)
{
CustomAct CA = new CustomAct();
if(CA.getCounter() < cena)
{
bonus++;
cena = cena + (cena / 10);
}
}
public int getBonus()
{
return bonus;
}
public int getCena()
{
return cena;
}
}
+5 Zkušeností
Zobrazeno 7 zpráv z 7.