Please help me, I want someone to write Main class for java code. import java.aw
ID: 669927 • Letter: P
Question
Please help me, I want someone to write Main class for java code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Association extends JApplet implements ActionListener{
private static final long serialVersionUID = 1L;
JButton add;
JRadioButton one,two,four,eight;
JLabel result;
public void init(){
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
JRadioButton("1");
one.addActionListener(this);
contentPane.add(one);
two = new JRadioButton("2");
two.addActionListener(this);
contentPane.add(two);
four = new JRadioButton("4");
four.addActionListener(this);
contentPane.add(four);
eight = new JRadioButton("8");
eight.addActionListener(this);
contentPane.add(eight);
add = new JButton("Add");
add.addActionListener(this);
contentPane.add(add);
result = new JLabel("");
contentPane.add(result);
}
public void actionPerformed(ActionEvent ae) {
String str=ae.getActionCommand();
int sum = 0;
if (str.compareTo("Add") == 0){
if (one.isSelected() == true) sum += 1;
if (two.isSelected() == true) sum += 2;
if (four.isSelected() == true) sum += 4;
if (eight.isSelected() == true) sum += 8;
result.setText(sum+"");
}
}
}
Explanation / Answer
import java.awt.BorderLayout; import java.awt.Panel; 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; public class Main{ public static void main(String[] args){ JFrame mainFrame = new JFrame("Experiment"); mainFrame.setSize(500,500); mainFrame.setVisible(true); Panel panel = new Panel(); mainFrame.getContentPane().add(panel); JLabel output = new JLabel("This is where the result from the num variable in the numGenerator class would go"); panel.add(output); JButton numGenerator = new JButton("Generate Number"); panel.add(numGenerator); numGenerator.addActionListener(new numGenerator()); } static class numGenerator implements ActionListener{ public void ActionPerfomed (ActionEvent e){ int num; //This is the variable I want to be passed to the //Main class so it can be displayed in the "output" Jlabel. Random dice = new Random(); num = dice.nextInt(3); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.