Multiple RadioButtons Simulator - jframe

I'm trying to make a sort of simple soccer simulator. This is the code I created after watching tutorials and i know its pretty bad. All i want to do is add a value to the team, like 1 for the best team and 10 for the worst, and when i click simulate a pop up would show up telling me which team would win given the teams value. But i cant figure out how to do it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class sim extends JPanel {
public sim() {
// JFrame constructor
super(true);
JRadioButton chelsea, arsenal, chelsea2, arsenal2;
this.setLayout(new GridLayout(3,0));
ButtonGroup group = new ButtonGroup();
ButtonGroup group2 = new ButtonGroup();
// takes image and saves it the the variable
Icon a = new ImageIcon(getClass().getResource("a.PNG"));
Icon c = new ImageIcon(getClass().getResource("c.JPG"));
chelsea = new JRadioButton("Chelsea",c);
chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal = new JRadioButton("Arsenal",a);
arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);
group.add(chelsea);
group.add(arsenal);
JLabel label = new JLabel("");
TitledBorder titled = new TitledBorder("Team 1");
label.setBorder(titled);
chelsea.setBorder(titled);
arsenal.setBorder(titled);
JButton button = new JButton("Simulate");
button.setHorizontalAlignment(JButton.CENTER);
add(button, BorderLayout.CENTER);
chelsea2 = new JRadioButton("Chelsea",c);
chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal2 = new JRadioButton("Arsenal",a);
arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);
group2.add(chelsea2);
group2.add(arsenal2);
JLabel label2 = new JLabel("");
TitledBorder titled2 = new TitledBorder("Team 2");
label2.setBorder(titled2);
chelsea2.setBorder(titled);
arsenal2.setBorder(titled);
add(label);
add(chelsea);
add(arsenal);
add(button);
add(chelsea2);
add(arsenal2);
HandlerClass handler = new HandlerClass();
chelsea.addActionListener(handler);
}
private class HandlerClass implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null, String.format("%s", e.getActionCommand()));
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Final");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920, 1080);
frame.setContentPane(new sim());
frame.setVisible(true);
}
}

Put the ActionListener into the sim class.
public void actionPerformed(ActionEvent e)
{
JButton clicked = (JButton)e.getSource();
if(clicked == button)
{
JOptionPane.showMessageDialog(null, "this team will win");
}
}
You need to decrees the amount of code to just amount needed you dont need to show us all the raidio

Related

Java Programming Using NetBeans: adding ActionListener to JButton

I have a specific problem I'm stuck on. My job is to have this pop up:
Here's the program I have so far:
import javax.swing.*;
import java.awt.event.*;
public class Assignment5 implements ActionListener
{
JFrame frame;
JPanel panel;
JLabel label1, label2, label3, label4;
JButton button;
JTextField text1, text2, text3;
public Assignment5()
{
frame = new JFrame();
frame.setVisible(true);
frame.setSize(400,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label1 = new JLabel("No. of Years: ");
text1 = new JTextField(20);
label2 = new JLabel("Rate of Interest: ");
text2 = new JTextField(20);
label3 = new JLabel("Principal Amount: ");
text3 = new JTextField(20);
button = new JButton("Calculate Simple Interest");
panel = new JPanel();
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(text1);
panel.add(text2);
panel.add(text3);
panel.add(button);
frame.add(panel);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int years = Integer.parseInt(text1.getText());
double rate = Double.parseDouble(text2.getText());
double amount = Double.parseDouble(text3.getText());
double simple = years*rate*amount;
label4 = new JLabel("Your Payable Amount Is: "+simple);
panel.add(label4);
}
public static void main(String[] args)
{
Assignment5 obj = new Assignment5();
}
}
Everything works so far, but my final label containing the actual result won't show up. I've tried a couple different things, each of which doesn't work, so I'm hoping you guys will be able to tell me a better way of doing this. Thanks.
Do you have any red errors showing up in any line of your code? I am not sure if it is the output code that is wrong. Try this:
label4.setText("Your payable amount is "+simple);
Try this:
JOptionPane.showMessageDialog(null,"Your payable amount is “+simple);
That will pop your message.

SmartGWT - can't click into items into windows modal

I have one problem using Window with setIsModal(true).
I have this code:
#Override
public void onModuleLoad() {
Button tester = new Button("Tester");
tester.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
#Override
public void onClick(ClickEvent event) {
final com.smartgwt.client.widgets.Window win = new com.smartgwt.client.widgets.Window();
win.setTitle("Ventana Tester");
win.setWidth(900);
win.setHeight(600);
win.setIsModal(true);
win.setShowModalMask(true);
win.centerInPage();
win.setMinimized(false);
win.addCloseClickHandler(new CloseClickHandler() {
#Override
public void onCloseClick(CloseClientEvent event) {
win.destroy();
}
});
PlanBoard pb = new PlanBoard();
win.addItem(pb);
win.show();
}
});
vlPrincipal.addMember(tester);
RootPanel.get("main").add(vlPrincipal);
}
and this is PlanBoard class:
public class PlanBoard extends VLayout{
private CaptionPanel contentDetallePlan = new CaptionPanel("DETALLES DEL PLAN");
private CaptionPanel contentAtributosPlan = new CaptionPanel("ATRIBUTOS DE PLAN");
private CaptionPanel contentSeccionesPlan = new CaptionPanel("SECCIONES");
public PlanBoard(){
contentDetallePlan.setStyleName("estiloCaptionPanel");
contentAtributosPlan.setStyleName("estiloCaptionPanel");
addMember(contentDetallePlan);
addMember(contentAtributosPlan);
addMember(contentSeccionesPlan);
preparaDetallePlan();
preparaAtributosPlan();
}
private void preparaDetallePlan(){
VLayout contenedorSeccion = new VLayout();
FlexTable table1 = new FlexTable();
FlexTable table2 = new FlexTable();
FlexTable table3 = new FlexTable();
Label np = new Label("Nombre de Plan:");
Label npText = new Label("Plan B");
Label tc = new Label("Tipo de Carta:");
DynamicForm tcForm = new DynamicForm();
ComboBoxItem tcBox = new ComboBoxItem();
tcBox.setWidth(250);
tcBox.setShowTitle(false);
tcForm.setItems(tcBox);
Label pr = new Label("Periodo:");
DynamicForm prForm = new DynamicForm();
ComboBoxItem prBox = new ComboBoxItem();
prBox.setWidth(150);
prBox.setShowTitle(false);
prForm.setItems(prBox);
Label dp = new Label("Descripcion:");
DynamicForm dpForm = new DynamicForm();
TextAreaItem dpText = new TextAreaItem();
dpText.setShowTitle(false);
dpText.setWidth(600);
dpForm.setItems(dpText);
table1.setWidget(0, 0, np);
table1.setWidget(0, 1, npText);
table2.setWidget(0, 0, tc);
table2.setWidget(0, 1, tcForm);
table2.setWidget(0, 2, pr);
table2.setWidget(0, 3, prForm);
table3.setWidget(0, 1, dp);
table3.setWidget(1, 1, dpForm);
contenedorSeccion.addMember(table1);
contenedorSeccion.addMember(table2);
contenedorSeccion.addMember(table3);
contentDetallePlan.add(contenedorSeccion);
}
private void preparaAtributosPlan(){
VLayout contenedorSeccion = new VLayout();
FlexTable table1 = new FlexTable();
Label fe = new Label("Firma Electornica:");
CheckboxItem feCheck = new CheckboxItem();
DateItem feFechaIni = new DateItem();
DateItem feFechaFin = new DateItem();
feFechaIni.setUseTextField(true);
feCheck.setShowTitle(false);
DynamicForm feForm = new DynamicForm();
feForm.setItems(feCheck,feFechaIni,feFechaFin);
table1.setWidget(0, 0, fe);
table1.setWidget(0, 1, feForm);
contenedorSeccion.addMember(table1);
contentAtributosPlan.add(contenedorSeccion);
}
The problem is when I try to click on CheckBoxItem or DateItem, I can't edit them, but when I don't use setIsModal(true), it works fine.
I don't know how to set the Window to modal(true), and have those items working on that window.
Here is your code cleaned (a little bit) and improved so that what you want to achieve (which is the modal window with selectable/actionable controls):
PlanBoard.java
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.layout.VLayout;
public class PlanBoard extends VLayout {
public PlanBoard(){
preparaDetallePlan();
preparaAtributosPlan();
preparaSecciones();
}
private void preparaDetallePlan(){
StaticTextItem np = new StaticTextItem("id2", "Nombre de Plan:");
StaticTextItem npText = new StaticTextItem("id2", "Plan B");
ComboBoxItem tcBox = new ComboBoxItem();
tcBox.setTitle("Tipo de Carta");
tcBox.setWidth(250);
ComboBoxItem prBox = new ComboBoxItem();
tcBox.setTitle("Periodo");
prBox.setWidth(150);
StaticTextItem dp = new StaticTextItem("id3", "Descripcion:");
TextAreaItem dpText = new TextAreaItem();
dpText.setShowTitle(false);
dpText.setWidth(600);
dpText.setStartRow(true);
dpText.setEndRow(true);
dpText.setColSpan(2);
DynamicForm form = new DynamicForm();
form.setItems(np, npText, tcBox, prBox, dp, dpText);
form.setIsGroup(true);
form.setGroupTitle("DETALLES DE PLAN");
addMember(form);
}
private void preparaAtributosPlan(){
StaticTextItem fe = new StaticTextItem("id4", "Firma Electornica:");
CheckboxItem feCheck = new CheckboxItem();
feCheck.setShowTitle(false);
DateItem feFechaIni = new DateItem();
DateItem feFechaFin = new DateItem();
feFechaIni.setUseTextField(true);
DynamicForm form = new DynamicForm();
form.setItems(fe, feCheck, feFechaIni, feFechaFin);
form.setIsGroup(true);
form.setGroupTitle("ATRIBUTOS DE PLAN");
addMember(form);
}
private void preparaSecciones(){
DynamicForm form = new DynamicForm();
form.setIsGroup(true);
form.setGroupTitle("SECCIONES");
addMember(form);
}
}
TestCases.java (rename to your EntryPoint class name, which you don't specify):
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.google.gwt.core.client.EntryPoint;
public class TestCases implements EntryPoint {
public void onModuleLoad() {
IButton tester = new IButton("Tester");
tester.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
final Window win = new Window();
win.setTitle("Ventana Tester");
win.setWidth(900);
win.setHeight(600);
win.setIsModal(true);
win.setShowModalMask(true);
win.centerInPage();
win.setMinimized(false);
win.addCloseClickHandler(new CloseClickHandler() {
#Override
public void onCloseClick(CloseClickEvent event) {
win.destroy();
}
});
PlanBoard pb = new PlanBoard();
win.addItem(pb);
win.show();
}
});
VLayout vlPrincipal = new VLayout();
vlPrincipal.addMember(tester);
vlPrincipal.draw();
}
}
Some notes:
Don't mix GWT and SmartGWT widgets unless absolutely necessary and only when you really know what you are doing (because it will generated unexpected results, as the one you are experiencing). In your case, the mixing is unnecessary!!
You can add the titles to the different controls you are using, and they will display as labels. You can specify if the title label appears above or to one side, per control.
Take a look at this SmartGWT demo to learn how to configure and use the different types of DynamicForm controls. Many of the things you were trying to do, you were doing more difficult than necessary.
On the "stylistic" aspect, don't write your code using Spanglish. I speak Spanish, so I understand, but it limits a lot the feedback you get, because your code is so much more difficult to understand. Use English (at least for code you intend to post in SO).

How do I add a button and label to my frame?

Ok basically my problem is I don't know how to get my buttons and my labels to appear in the frame that I just created. I tried using frame.add(new JButton("VOTE1")); but that doesn't work and it says it's missing an identifier. Here is my code so far, your help is much appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VotingMachine extends JFrame implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame("Voting Machine");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JLabel Candidate1 = new JLabel("My Name");
private JLabel Candidate2 = new JLabel("Jennifer Lawrence");
private JLabel Candidate3 = new JLabel("Cristiano Ronaldo");
private JButton VOTE1 = new JButton("VOTE FOR Bishoy Morcos");
private JButton VOTE2 = new JButton("VOTE FOR Jennifer Lawrence");
private JButton VOTE3 = new JButton("VOTE FOR Cristiano Ronaldo");
int countcandidate1;
int countcandidate2;
int countcandidate3;
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o == VOTE1)
countcandidate1++;
else if(o == VOTE2)
countcandidate2++;
else if(o == VOTE3)
countcandidate3++;
}
}
First you need to create a JPanel
JPanel welcomePanel = new JPanel();
Then add the buttons and labels to that panel
welcomePanel.add(Candidate1);
welcomePanel.add(VOTE1);
ETC.... for all of your buttons and labels
Lastly, add that JPanel to your JFrame
frame.add(welcomePanel);
If this answers your question, please mark it as answered so other viewers will be informed.

displaying jpg to a jPanel

So i am trying to add an image to my program after a button is pressed with a certain value entered into a text field. I want the image to be blank until the button is pressed with the correct value. The only issue i seem to be having is making the photo appear, i know the output should be working. here is the code i have for adding the label.
photo = new JLabel();
photo.setBounds(425, 170, 400, 360);
contentPane.add(photo);
ImageIcon icon = new ImageIcon("AwsomeSauce.jpg");
here is the code for when the value is entered correctly
if (error) {
photo.setIcon(image);
}
now im still pretty new at this so go easy on me.
Here is simple example for you:
public class Frame extends JFrame {
private JLabel label;
public Frame() {
getContentPane().add(label = new JLabel(),BorderLayout.SOUTH);
JButton b = new JButton("show Icon");
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
URL resource = getClass().getResource("/res/3_disc.png");
ImageIcon ico = new ImageIcon(resource);
label.setIcon(ico);
}
});
getContentPane().add(b,BorderLayout.NORTH);
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setTitle("This is a test");
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
"/res/3_disc.png" - is my icon which is placed in my project in res folder.

How to create a click event search button in Eclipse using?

How to create a click event search button in Eclipse?? Can someone help me. This is the code im working with.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listContent1 = (ListView)findViewById(R.id.lFoodlist1);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.KEY_FOODNAME,SQLiteAdapter.KEY_CALORIES};
int[] to = new int[]{R.id.tv1, R.id.tv2};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent1.setAdapter(cursorAdapter);
//listContent.setOnItemClickListener(listContentOnItemClickListener);*/
}
This code was taken from Here and was modified/commented to help the poseter with his scenario.
You can implement swing to help with the GUI aspect of your application. Others may prefer AWT
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class myTest {
public static void main(String[] args) {
//This will create the JFrame/JPanel/JButton
final JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button1 = new JButton();
//Add your panel and button and make them visable
frame.add(panel);
panel.add(button1);
frame.setVisible(true);
//This adds the actionListener to the Button
button1.addActionListener(new ActionListener() {
//When the button is clicked this action will be performed
public void actionPerformed(ActionEvent arg0) {
//Add your code here.
}
});
}
}