473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to put buttons into two subpanels..??

I'm in an advance class (OOP using Java) with no background in Java programming... I was hoping that someone could help me out!? We have an assignment that we are to add voting buttons into two subpanels and also add a picture beside the canidate's name. I'm not sure what's going on with this program. Could someone PLEASE help me out??

//************************************************** *******
// VoteCounter.java
//
// Demonstrates a graphical user interface and event listeners to
// tally votes for two candidates, Joe and Sam.
//************************************************** *******


import javax.swing.JFrame;

import java.awt.*;

import javax.swing.*;

public class VoteCounter

{
//----------------------------------------------
// Creates the main program frame.
//----------------------------------------------
public static void main(String[] args)

{

JFrame frame = new JFrame("Vote Counter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

frame.getContentPane().add(new VoteCounterPanel());

frame.pack();
frame.setVisible(true);

//set up first subpanel

JPanel subPanel1 = new JPanel();
subPanel1.setPreferredSize(new Dimension (150, 100));
subPanel1.setBackground(Color.yellow);
JLabel label1 = new JLabel ("Joe");
subPanel1.add(label1);

ImageIcon icon = new ImageIcon ("joe.jpg");
JLabel label;

label = new JLabel ("Joe", icon, SwingConstants.CENTER);

//set up second subpanel

JPanel subPanel2 = new JPanel();
subPanel2.setPreferredSize(new Dimension (150, 100));
subPanel2.setBackground(Color.pink);
JLabel label2 = new JLabel ("Sam");
subPanel2.add(label2);

ImageIcon icon1 = new ImageIcon ("sam.jpg");
JLabel label3;

label3 = new JLabel ("Sam", icon1, SwingConstants.RIGHT);
label3.setHorizontalTextPosition(SwingConstants.LE FT);
label3.setVerticalTextPosition(SwingConstants.BOTT OM);


//set up primary panel

JPanel primary = new JPanel();
primary.setBackground(Color.blue);
primary.add(subPanel1);
primary.add(subPanel2);
frame.getContentPane().add(primary);




}

}


//************************************************** *******
// VoteCounterPanel.java
//
// Demonstrates a graphical user interface and event listeners to
// tally votes for two candidates, Joe and Sam.
//************************************************** *******

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class VoteCounterPanel extends JPanel
{
private int votesForJoe;
private JButton joe;
private JLabel labelJoe;

private int votesForSam;
private JButton sam;
private JLabel labelSam;

//----------------------------------------------
// Constructor: Sets up the GUI.
//----------------------------------------------
public VoteCounterPanel()

{

//initialize counts
votesForJoe = 0;
votesForSam = 0;


joe = new JButton("Vote for Joe");
joe.addActionListener(new JoeButtonListener());

labelJoe = new JLabel("Votes for Joe: " + votesForJoe);

add(joe);
add(labelJoe);

setPreferredSize(new Dimension(300, 80));
//setBackground(Color.cyan);

sam = new JButton("Vote for Sam");
sam.addActionListener(new SamButtonListener());

labelSam = new JLabel ("Votes for Sam: " + votesForSam);

add(sam);
add(labelSam);

setPreferredSize(new Dimension(300, 80));
//setBackground(Color.pink);

}

//************************************************** *
// Represents a listener for button push (action) events
//************************************************** *

private class JoeButtonListener implements ActionListener
{
//----------------------------------------------
// Updates the counter and label when Vote for Joe
// button is pushed
//----------------------------------------------
public void actionPerformed(ActionEvent event)
{
votesForJoe++;
labelJoe.setText("Votes for Joe: " + votesForJoe);

}
}

private class SamButtonListener implements ActionListener
{
//----------------------------------------------
// Updates the counter and label when Vote for Sam
// button is pushed
//----------------------------------------------
public void actionPerformed(ActionEvent event)
{
votesForSam++;
labelSam.setText("Votes for Sam: " + votesForSam);

}
}
}
Oct 26 '06 #1
1 5339
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. //************************************************** *******
  2. // VoteCounter.java
  3. //
  4. // Demonstrates a graphical user interface and event listeners to
  5. // tally votes for two candidates, Joe and Sam.
  6. //************************************************** *******
  7.  
  8.  
  9.  
  10.  
  11. import java.awt.*;
  12.  
  13. import javax.swing.*;
  14.  
  15. import java.awt.event.*;
  16. import javax.swing.*;
  17.  
  18. public class VoteCounter
  19.  
  20. {
  21. //----------------------------------------------
  22. // Creates the main program frame.
  23. //----------------------------------------------
  24. public static void main(String[] args)
  25.  
  26. {
  27.  
  28. JFrame frame = new JFrame("Vote Counter");
  29. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. frame.setSize(600, 600);
  31.  
  32.  
  33.  
  34.  
  35.  
  36. //set up first subpanel
  37.  
  38. JPanel subPanel1 = new JPanel();
  39. subPanel1.setPreferredSize(new Dimension (150, 100));
  40. subPanel1.setBackground(Color.yellow);
  41. JLabel label1 = new JLabel ("Joe");
  42. subPanel1.add(label1);
  43.  
  44. ImageIcon icon = new ImageIcon ("joe.jpg");
  45. JLabel label;
  46.  
  47. label = new JLabel ("Joe", icon, SwingConstants.CENTER);
  48.  
  49. //set up second subpanel
  50.  
  51. JPanel subPanel2 = new JPanel();
  52. subPanel2.setPreferredSize(new Dimension (150, 100));
  53. subPanel2.setBackground(Color.pink);
  54. JLabel label2 = new JLabel ("Sam");
  55. subPanel2.add(label2);
  56.  
  57. ImageIcon icon1 = new ImageIcon ("sam.jpg");
  58. JLabel label3;
  59.  
  60. label3 = new JLabel ("Sam", icon1, SwingConstants.RIGHT);
  61. label3.setHorizontalTextPosition(SwingConstants.LEFT);
  62. label3.setVerticalTextPosition(SwingConstants.BOTTOM);
  63.  
  64.  
  65. //set up primary panel
  66.  
  67. JPanel primary = new JPanel();
  68. primary.setBackground(Color.blue);
  69. primary.add(subPanel1);
  70. primary.add(subPanel2);
  71. frame.getContentPane().add(primary, "Center");
  72. frame.getContentPane().add(new VoteCounterPanel(), "South");
  73. //frame.pack();
  74. frame.setVisible(true);
  75.  
  76.  
  77. }
  78.  
  79. }
  80.  
  81.  
  82. //************************************************** *******
  83. // VoteCounterPanel.java
  84. //
  85. // Demonstrates a graphical user interface and event listeners to
  86. // tally votes for two candidates, Joe and Sam.
  87. //************************************************** *******
  88.  
  89.  
  90.  
  91. class VoteCounterPanel extends JPanel
  92. {
  93. private int votesForJoe;
  94. private JButton joe;
  95. private JLabel labelJoe;
  96.  
  97. private int votesForSam;
  98. private JButton sam;
  99. private JLabel labelSam;
  100.  
  101. //----------------------------------------------
  102. // Constructor: Sets up the GUI.
  103. //----------------------------------------------
  104. public VoteCounterPanel()
  105.  
  106. {
  107.  
  108. //initialize counts
  109. votesForJoe = 0;
  110. votesForSam = 0;
  111.  
  112.  
  113. joe = new JButton("Vote for Joe");
  114. joe.addActionListener(new JoeButtonListener());
  115.  
  116. labelJoe = new JLabel("Votes for Joe: " + votesForJoe);
  117.  
  118. add(joe);
  119. add(labelJoe);
  120.  
  121. setPreferredSize(new Dimension(300, 80));
  122. //setBackground(Color.cyan);
  123.  
  124. sam = new JButton("Vote for Sam");
  125. sam.addActionListener(new SamButtonListener());
  126.  
  127. labelSam = new JLabel ("Votes for Sam: " + votesForSam);
  128.  
  129. add(sam);
  130. add(labelSam);
  131.  
  132. setPreferredSize(new Dimension(300, 80));
  133. //setBackground(Color.pink);
  134.  
  135. }
  136.  
  137. //************************************************** *
  138. // Represents a listener for button push (action) events
  139. //************************************************** *
  140.  
  141. private class JoeButtonListener implements ActionListener
  142. {
  143. //----------------------------------------------
  144. // Updates the counter and label when Vote for Joe
  145. // button is pushed
  146. //----------------------------------------------
  147. public void actionPerformed(ActionEvent event)
  148. {
  149. votesForJoe++;
  150. labelJoe.setText("Votes for Joe: " + votesForJoe);
  151.  
  152. }
  153. }
  154.  
  155. private class SamButtonListener implements ActionListener
  156. {
  157. //----------------------------------------------
  158. // Updates the counter and label when Vote for Sam
  159. // button is pushed
  160. //----------------------------------------------
  161. public void actionPerformed(ActionEvent event)
  162. {
  163. votesForSam++;
  164. labelSam.setText("Votes for Sam: " + votesForSam);
  165.  
  166. }
  167. }
  168. }
This will make the pictures appear. You were adding the panels one on top of the other. That is why only one panel was appearing
Oct 27 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ralph Freshour | last post by:
Is it possible to inhibit the browser Back/Fwd buttons via PHP? Thanks...
4
by: Bob Lehmann | last post by:
Hi, I pretty sure I've seen this problem addressed before, but I can't find any references. The short story is that I have I have multiple submit buttons on a page, each providing different...
5
by: Lyn | last post by:
Hi, I hope someone can help. I have a main form which mostly fills the Access window. In the bottom half of this form I have a tab control to display various types of data related to the main...
0
Denburt
by: Denburt | last post by:
This code is for a Toggle Button layout on a form, with this code you can set a number of toggle buttons visible and have multiple submenus that will stay hidden when not in use. My main menu is set...
4
by: Ron | last post by:
I want to create 10 buttons on a form at runtime, the code below is only creating one button labeled 1. Any idea what I am doing wrong? Public Class Form1 Public code(10) As String Public...
3
by: Ron | last post by:
Can anyone help me out? I am trying to add buttons numbered one through 10 at runtime to a form. I think they are getting added but they seem to be getting stacked one on top of each other. so...
2
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on...
4
by: Blasting Cap | last post by:
I have a page that has a number of radio buttons that will be displayed to different access levels of a user who logs in to my website. For instance, if there are a dozen buttons, user1 will see...
2
by: remya1000 | last post by:
hai i'm using Vb.net. i'm creating 64 dynamic created buttons of 8 rows and 8 columns. And i have 1 Go button, 1 textbox. those were created dynamically. if i enter one number inside textbox and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.