So.. In a nutshell, I am working on a basic GUI project. I have attempted to break everything down in to small factored down methods inside my class.
I am able to build my GUI this way, but now having issues when trying to create my ActionListener functionality. Here is the code I have below
package jLottery; import java.awt.Color; import java.awt.Container; import java.awt.Event; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; public class jLottery { //create objects and vars static JFrame frame = new JFrame("The National Lottery"); static Container con = frame.getContentPane(); Random RandomNum = new Random(); int matches = 0; int[] WinningNums = winningNUM(); //Create Window public void CreateFrame (){ frame.setSize(1200,800); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setAlwaysOnTop(true); frame.setResizable(false); } //Set Up Content Container public void CreateContainer(){ con.setBackground(new Color(56,48,44)); con.setLayout(new GridLayout(0,5)); } //Add a Label public void AddHead(String i) { JLabel label = new JLabel(i); label.setFont(new Font("Sans-Serif", Font.BOLD, 20)); label.setForeground(new Color(225,98,25)); con.add(label); } public void AddSubHead(String i) { JLabel label = new JLabel(i); label.setFont(new Font("Sans-Serif", Font.PLAIN, 20)); label.setForeground(new Color(225,98,25)); con.add(label); } //function to create check boxes and add them to frame ( complete with action listeners ) public void AddCheckButton(int i){ String ButtonNum = Integer.toString(i); JCheckBox select = new JCheckBox(ButtonNum); select.setBackground(new Color(56,48,44)); select.setForeground(new Color(225,255,255)); con.add(select); select.addActionListener(this); } public void AddButton(String i){ JButton button = new JButton(i); button.setBackground(new Color(225, 98, 25)); button.setForeground(new Color(255, 255, 255)); button.setFont(new Font("Sans-Serif", Font.BOLD, 20)); con.add(button); button.addActionListener(this); }
Here is my implementation code:
package jLottery; import java.util.Scanner; public class jMain { /** * @param args */ public static void main(String[] args) { jLottery lottery = new jLottery(); //create our main window and container lottery.CreateFrame(); lottery.CreateContainer(); //create our Header lottery.AddHead("The Lottery: "); //create our subHeader lottery.AddSubHead("Pick Six "); lottery.AddHead("");//spacer lottery.AddHead("");//spacer lottery.AddHead("");//spacer //add our check-buttons for (int i=0; i<35; i++){ lottery.AddCheckButton((i+1)); } //add our submit button lottery.AddButton("Play!"); }
The main issue I am having is that it is not letting me use:
select.addActionListener(this);
or
button.addActionListener(this);
On a side note, my layout is very inconsistent, and appears to often show partial amounts of components when ran.
Is my breaking down/factoring approach all wrong? I am just trying to avoid a huge long class (as would be the case with all the formatting and options etc)