I am learning java, I have created a dice thrower - when i press the button it throws a dice - but the output is in the console. How can I output in the same frame with the buttons.
// sorry i am not native english speaking person, but i hope you get the idea.
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JTextArea;
public class Main {
public static void main(String s[]){
Random rand = new Random();
JFrame frame = new JFrame("Dice Thrower 2000");
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("Throw dice");
JLabel label1 = new JLabel("You got : ");
JTextArea output = new JTextArea(" a ");
JButton buttond4 = new JButton();
buttond4.setText("d4");
buttond4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
int n = rand.nextInt(4) + 1;
System.out.println("You threw d4 and got " + n);
}
});
JButton buttond6 = new JButton();
buttond6.setText("d6");
buttond6.addActionListener(new ActionListener() {...});
JButton buttond8 = new JButton();
buttond8.setText("d8");
buttond8.addActionListener(new ActionListener() {...});
JButton buttond10 = new JButton();
buttond10.setText("d10");
buttond10.addActionListener(new ActionListener() {...});
JButton buttond12 = new JButton();
buttond12.setText("d12");
buttond12.addActionListener(new ActionListener() {...});
JButton buttond20 = new JButton();
buttond20.setText("d20");
buttond20.addActionListener(new ActionListener() {...});
panel.add(label);
panel.add(buttond4);
panel.add(buttond6);
panel.add(buttond8);
panel.add(buttond10);
panel.add(buttond12);
panel.add(buttond20);
panel.add(label1);
frame.add(output);
frame.add(panel);
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I have provided buttons d4 - extended code, the code for rest buttons is almost identical.
Using System.out.println("You threw d4 and got " + n);
causes the print to the console.
You should use the JTextArea#append
method instead.
Your code would become:
final JTextArea output = new JTextArea(" a ");
JButton buttond4 = new JButton();
buttond4.setText("d4");
buttond4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
int n = rand.nextInt(4) + 1;
output.append("You threw d4 and got " + n + "\n");
}
});
Edit:
You also need to add the output
to the JFrame
. You tried this by using
frame.add(output);
frame.add(panel);
but that won't work. The content pane of a JFrame
by default has a BorderLayout
, and calling add
will add the component in the BorderLayout#CENTER
. That area can only contain one component.
Now you have two options:
LayoutManager
. This is a good overview of the standard available LayoutManager
s.Keep the current LayoutManager
, and add the two components to different areas, e.g.
frame.add(output, BorderLayout.CENTER);
frame.add(panel, BorderLayout.SOUTH);