\$\begingroup\$
\$\endgroup\$
Three days ago I wrote about a Java Dice Roller I wrote. I've now added a GUI to that program. Here it is:
DiceRollerGUI.java:
package com.egroegnosbig.dicerollergui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DiceRollerGUI {
static JFrame frameOne = new JFrame("Dice Roller");
public static void main(String[] args) {
frameOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DiceGUI GUI = new DiceGUI();
frameOne.add(GUI);
Button b = new Button("Roll");
b.addActionListener(new ButtonAction());
frameOne.add(b);
frameOne.setLayout(new GridLayout(1, 2));
frameOne.setSize(400, 250);
frameOne.setResizable(false);
frameOne.setVisible(true);
}
}
class ButtonAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
DiceRollerGUI.frameOne.setVisible(false);
JFrame frameTwo = new JFrame("Dice Roller");
frameTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameTwo.setSize(400, 250);
frameTwo.setResizable(false);
ResultGUI resultGUI = new ResultGUI();
frameTwo.add(resultGUI);
frameTwo.setVisible(true);
}
}
DiceGUI.java:
package com.egroegnosbig.dicerollergui;
import java.awt.*;
import javax.swing.*;
public class DiceGUI extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.drawString("Dice Roller", 70, 20);
}
}
ResultGUI.java:
package com.egroegnosbig.dicerollergui;
import java.awt.*;
import javax.swing.*;
public class ResultGUI extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
Dice dice = new Dice(6);
int resultInt = dice.roll();
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(resultInt);
String result = sb.toString();
g.drawString("The dice rolled a", 150, 125);
g.drawString(result, 243, 125);
}
}
Dice.java:
package com.egroegnosbig.dicerollergui;
import java.util.Random;
public class Dice {
private final Random rand;
private final int faces;
public Dice(int faces) {
this.rand = new Random();
this.faces = faces;
}
public int roll() {
return rand.nextInt(faces) + 1;
}
}
Working on better class names...
asked Sep 29, 2015 at 17:53
user69731user69731
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
- Rather than swapping out the entire JFrame for another on the button click, you should simply be updating the contents. This is all very overwrought. I think all you really need is a panel with a button and a label. Click the button, and display the results in the label.
- Related to the above, rather than overriding
paint
, you should be using a layout and adding subcomponents.
To get you started:
public class DicePanel extends JPanel {
private final Dice dice;
private JButton rollButton;
private JLabel displayLabel;
public DicePanel(Dice dice) {
this.dice = dice;
rollButton = new JButton("Roll");
displayLabel = new JLabel();
rollButton.addActionListener(e ->
displayLabel.setText("You rolled a: " + dice.roll())
);
// or if you're not using Java 8, you can do the more verbose thing.
// not specifying a layout defaults to a flow layout. Set a layout via:
// setLayout(new BorderLayout()); // or whatever
add(rollButton);
add(displayLabel);
}
}
Your program should just create a Dice
, create a DicePanel
with that, and stick it in a JFrame
and show it. Then play around with layouts to get something you like.
answered Sep 29, 2015 at 18:31
-
\$\begingroup\$ Nice, fixes my horrible button layout as well! \$\endgroup\$user69731– user697312015年09月29日 18:57:40 +00:00Commented Sep 29, 2015 at 18:57
-
\$\begingroup\$ Find the latest version on GitHub. \$\endgroup\$user69731– user697312015年10月01日 06:29:49 +00:00Commented Oct 1, 2015 at 6:29
lang-java