Results 1 to 2 of 2
  1. #1
    Celletti is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Help with multiple frames/classes

    Hi im trying to make a program that is simple and has a bunch of buttons on it. I essentially want it to work by Clicking on a button, that opens a specific window for that button and this new window will have more buttons. On this new window if you click a button, i want it to add new text/label to the original to keep track that i clicked on that button. I have made each window their own class, so that when the program starts it runs the "Eldar" class that creates a window, then if you click on the "Troops" button it runs the "EldarTroop" class which creates another window with different buttons. After that when i click on the "Guardian" button I want a new label to be made on the "Eldar" class window, in the (JPanel) mainLabels area. How would i do that? Or am i going about this the wrong way.

    Java Code:
    public class Warhammer{
    	public static void main(String[] args) {
    		Eldar gui = new Eldar();
    	}
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Eldar implements ActionListener{
    	private JButton b1;
    	private JButton b2;
    	private JButton b3;
    	private JButton b4;
    	private JButton b5;
    	private JButton b6;
    	private JFrame main;
    	private JPanel mainButtons;
    	private JPanel mainLabels;
    	
    	
    
    	public Eldar(){
    		main = new JFrame();
    		main.setSize(new Dimension(500, 500));
    		main.setLocation(new Point(650, 300));
    		main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		main.setLayout(new GridLayout(2, 1));
    		main.setResizable(false);
    		
    		mainButtons = new JPanel(new GridLayout(2, 3));
    		b1 = new JButton("Troops");
    		b1.setActionCommand("Troops");
    		b1.addActionListener(this);
    		mainButtons.add(b1);
    		
    		
    		b2 = new JButton("Elites");
    		b2.setActionCommand("Elites");
    		b2.addActionListener(this);
    		mainButtons.add(b2);
    		
    		
    		b3 = new JButton("Fast Attack");
    		b3.setActionCommand("Fast Attack");
    		b3.addActionListener(this);
    		mainButtons.add(b3);
    		
    		
    		b4 = new JButton("Heavy Support");
    		b4.setActionCommand("Heavy Support");
    		b4.addActionListener(this);
    		mainButtons.add(b4);
    		
    		
    		b5 = new JButton("HQ");
    		b5.setActionCommand("HQ");
    		b5.addActionListener(this);
    		mainButtons.add(b5);
    		
    		b5 = new JButton("Save");
    		b5.setActionCommand("Save");
    		b5.addActionListener(this);
    		mainButtons.add(b5);
    		
    		mainLabels = new JPanel(new GridLayout(5, 1));
    
    		
    		
    
    		
    		main.add(mainButtons);
    		main.add(mainLabels);
    
    		main.setVisible(true);
    		
    
    	}
    	
    	public void actionPerformed(ActionEvent event) {
    		String e = event.getActionCommand();
    		
    		if(e.equals("Troops")) {
    			System.out.println("Troops");
    			EldarTroops eTroopGui = new EldarTroops(mainLabels);
    		}else if (e.equals("Elites")) 
    			System.out.println("Elites");
    		else if (e.equals("Fast Attack")) 
    			System.out.println("Fast Attack");
    		else if (e.equals("Heavy Support")) 
    			System.out.println("Heavy Support");
    		else if (e.equals("HQ")) 
    			System.out.println("HQ");
    		
    		
    	}
    	
    
    	
    	
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class EldarTroops implements ActionListener {
    	private JButton b1;
    	private JButton b2;
    	private JButton b3;
    	private JButton b4;
    	private JButton b5;
    	private JButton b6;
    	private JFrame eTroops;
    	private JPanel mainLabels;
    	
    
    	public EldarTroops(JPanel mainLabels) {
    		this.mainLabels = mainLabels;
    		eTroops = new JFrame();
    		eTroops.setSize(new Dimension(500, 500));
    		eTroops.setLocation(new Point(650, 300));
    		eTroops.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		eTroops.setLayout(new GridLayout(2, 1));
    		eTroops.setResizable(false);
    		
    		
    		
    		
    		
    		b1 = new JButton("Guardians");
    		b1.setActionCommand("Guardians");
    		b1.addActionListener(this);
    		eTroops.add(b1);
    		
    		
    		
    		b2 = new JButton("OTher");
    		b2.setActionCommand("Elites");
    		b2.addActionListener(this);
    		eTroops.add(b2);
    		
    		
    		b3 = new JButton("Other");
    		b3.setActionCommand("Fast Attack");
    		b3.addActionListener(this);
    		eTroops.add(b3);
    		
    		
    		b4 = new JButton("Other");
    		b4.setActionCommand("Heavy Support");
    		b4.addActionListener(this);
    		eTroops.add(b4);
    		
    		
    		b5 = new JButton("ETC");
    		b5.setActionCommand("HQ");
    		b5.addActionListener(this);
    		eTroops.add(b5);
    		
    		b6 = new JButton("Back");
    		b6.setActionCommand("Back");
    		b6.addActionListener(this);
    		eTroops.add(b6);
    		
    		eTroops.setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent event) {
    		String e = event.getActionCommand();
    		
    		
    		if(e.equals("Guardians")) {
    		
    		}
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'd give Eldar a public method, such as addElement(Object element), that allows outside objects to add Strings or objects with decent toString methods to Eldar for display. For example, if Eldar had a JList to display these Strings, the addElement method could add to the list model that the JList was created with. I'd have the sub window classes placed in JDialogs and passed a reference to the Eldar object so that they can call this method. e.g.,

    Java Code:
    import java.awt.*;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Warhammer {
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            Eldar gui = new Eldar();
    
            JFrame frame = new JFrame("Eldar");
            frame.getContentPane().add(gui.getMainPanel());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
          }
        });
      }
    }
    
    class Eldar {
      private static final String[] BUTTON_SRINGS = {
        "Troops", "Elites", "Fast Attack",
        "Heavy Support", "HQ", "Save"};
      private JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
      private JPanel mainButtons;
      private DefaultListModel listModel = new DefaultListModel();
    
      public Eldar() {
    
        mainButtons = new JPanel(new GridLayout(2, 0, 10, 10));
        EldarBtnListener btnListener = new EldarBtnListener();
        for (String btnString : BUTTON_SRINGS) {
          JButton button = new JButton(btnString);
          button.addActionListener(btnListener);
          mainButtons.add(button);
        }
    
        JList mainList = new JList(listModel);
        mainList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        mainPanel.setPreferredSize(new Dimension(600, 400));
        mainPanel.add(mainButtons, BorderLayout.NORTH);
        mainPanel.add(new JScrollPane(mainList), BorderLayout.CENTER);
      }
    
      public void addElement(Object element) {
        listModel.addElement(element);
      }
    
      public JPanel getMainPanel() {
        return mainPanel;
      }
    
      private class EldarBtnListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
          String e = event.getActionCommand();
    
          if (e.equals("Troops")) {
            Window window = SwingUtilities.getWindowAncestor(mainPanel);
            EldarTroops eTroopGui = new EldarTroops(Eldar.this);
            JDialog dialog = new JDialog(window, "Troops", ModalityType.APPLICATION_MODAL);
            dialog.getContentPane().add(eTroopGui.getPanel());
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
          } else if (e.equals("Elites"))
            System.out.println("Elites");
          else if (e.equals("Fast Attack"))
            System.out.println("Fast Attack");
          else if (e.equals("Heavy Support"))
            System.out.println("Heavy Support");
          else if (e.equals("HQ"))
            System.out.println("HQ");
    
        }
      }
    
    }
    
    class EldarTroops {
      private static final String[] BUTTON_TEXTS = {
        "Guardians", "Elites", "Fast Attack",
        "Heavy Supprort", "HQ"};
      private static final String DONE = "Done";
      private static final Font BUTTON_FONT = new Font(Font.DIALOG, Font.BOLD, 18);
      private Eldar eldar;
      private JPanel troopsPanel = new JPanel();
    
      public EldarTroops(Eldar eldar) {
        this.eldar = eldar;
        troopsPanel.setLayout(new GridLayout(2, 0, 20, 20));
        troopsPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        ActionListener btnListener = new ButtonListener();
        for (String btnText : BUTTON_TEXTS) {
          JButton button = new JButton(btnText);
          button.setFont(BUTTON_FONT);
          button.addActionListener(btnListener);
          troopsPanel.add(button);
        }
        final JButton doneButton = new JButton(DONE);
        doneButton.setFont(BUTTON_FONT);
        doneButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Window window = SwingUtilities.getWindowAncestor(doneButton);
            window.dispose();
          }
        });
        troopsPanel.add(doneButton);
      }
      
      public JPanel getPanel() {
        return troopsPanel;
      }
    
      private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
          String command = event.getActionCommand();
          eldar.addElement(command);
        }
      }
    
    }

Similar Threads

  1. navigation b/w frames
    By imrankhan in forum AWT / Swing
    Replies: 3
    Last Post: 03-19-2012, 12:25 PM
  2. jsp frames
    By vasug in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-16-2010, 05:00 PM
  3. Help regarding Frames
    By ramesh.8189 in forum AWT / Swing
    Replies: 14
    Last Post: 02-15-2009, 08:12 AM
  4. How to use multiple timer classes in swings
    By theone3nu in forum AWT / Swing
    Replies: 12
    Last Post: 12-30-2008, 02:30 AM
  5. Replies: 0
    Last Post: 08-19-2008, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •