Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help With Panels?

setpreferredsize

  • Please log in to reply
5 replies to this topic

#1 Koola1d

Koola1d

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 12 December 2010 - 12:09 PM

Hey guys. I'm kinda new to Java (or Panels for that matter) and I was wondering if I could get some help with a project for school. I'm making a little mock text-based videogame called Warlog Hunter (inside joke in the class).

I currently have a panel with 4 buttons on it. (New Game, Load Game, Settings, Exit). When the user clicks "New Game" they are prompted to enter a Username, Class, and Race. When the user hits 'Ok' after entering in the information, I would like for another panel to pop up with more options on it, but I'm having some trouble making it do that. And knowing me, I'm probably just making a bonehead mistake and not seeing it.

It's kind of hard to explain this so I'll just post my current code;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;

public class WarlogHunter extends JFrame 
{
    public WarlogHunter() throws NumberFormatException
    {
        player = new ArrayList();
        
        class addButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                if(player.size() > 0)
                {
                    JOptionPane.showMessageDialog(null, "Error - Please exit your current game before starting a new one.");
                }
                else
                {
                    AddPanel ap = new AddPanel(appbook);
                    ap.setTitle("New Game");
                    ap.show();
                }
            }
        }
        
        class deleteButtonListener implements ActionListener 
        {
            public void actionPerformed(ActionEvent event)
            {
                {
                    JOptionPane.showMessageDialog(null, "Error - No saved games found.");
                    return;
                }            
            }
        }
        
        class cancelButtonListener implements ActionListener 
        {
            public void actionPerformed(ActionEvent event)
            {
                JOptionPane.showMessageDialog(null, "Settings unavailable.");               
            }
        }
        
        class exitButtonListener implements ActionListener 
        {
            public void actionPerformed(ActionEvent event)
            {   
                System.exit(0);
            }
        }   
        
        appbook = this;
        int numPlayers = 0;
        
        newGame = new JButton("New Game");
        ActionListener addListener = new addButtonListener();
        newGame.addActionListener(addListener);
        
        load = new JButton("Load Game");
        ActionListener delListener = new deleteButtonListener();
        load.addActionListener(delListener);
        
        settings = new JButton("Settings");
        ActionListener cancelListener = new cancelButtonListener();
        settings.addActionListener(cancelListener);
        
        exit = new JButton("Exit");
        ActionListener exitListener = new exitButtonListener();
        exit.addActionListener(exitListener);
        
        appointments = new JTextArea();
        appointments.setLineWrap(true);
        appointments.setEditable(false);
        
        JPanel northPanel = new JPanel();
        northPanel.add(newGame);
        northPanel.add(load);
        northPanel.add(settings);
        northPanel.add(exit);
        
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(1,4));
        
        northPanel.setPreferredSize(new Dimension(575,35));
        centerPanel.setPreferredSize(new Dimension(575,150));
        
        getContentPane().add(northPanel, BorderLayout.NORTH);
        getContentPane().add(centerPanel, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        
        ImageIcon icon = new ImageIcon("warlog.jpg"); 
        JLabel label = new JLabel();
        label.setIcon(icon);
        centerPanel.add(label);
        northPanel.setBackground(Color.BLACK);
        
        this.getContentPane().add(centerPanel); 
        setVisible(true); 
        
    }
    
    class adventureButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            //code for adventure, dont worry about this
        }
    }
      
    public void addGame(NewGame a)
    {
        player.add(a);
             
        if(player.size() > 0)
        {
           //code for new panel..? 
        }
    }

    public static void main(String[] args)
    {
        WarlogHunter ab = new WarlogHunter();
        ab.setTitle("Warlog Hunter - ALPHA");
        ab.setSize(368,256);
        ab.setResizable(false);
        ab.setDefaultLookAndFeelDecorated(true);
        ab.show();
    }
    
    private JButton adventureTime;
    private Image warlog;
    private JButton newGame;
    private JButton load;
    private JButton settings;
    private JButton exit;
    private JTextArea appointments;
    private ArrayList player;
    private WarlogHunter appbook;
   
}
Specifically

public void addGame(NewGame a)
    {
        player.add(a);
             
        if(player.size() > 0)
        {
           //code for new panel..? 
        }
    }
^Would this be an acceptable way of testing to see if I should open a new panel? I've tested it with other things (System.out.println, JOptionPane.showMessageDialog) and those pop up right after I hit "Ok"... so I must be doing something wrong.

I've been trying something like this;
public void addGame(NewGame a)
    {
        player.add(a);
             
        if(player.size() > 0)
        {
            JPanel adventurePanel = new JPanel();
            
            adventureTime = new JButton("Go Adventure!");
            ActionListener adventureListener = new adventureButtonListener();
            adventureTime.addActionListener(adventureListener);
            
            adventurePanel.add(adventureTime);
            
            adventurePanel.setVisible(true);
        }
    }
That compiles but when I hit "Ok" nothing happens....

I basically just need a new panel with a "Go Adventure!" button and an "Exit" button on. (maybe a sleep button (for saving a game? idek if you can do that)).

Anyway, any help would be appreciated. I do have other classes in my program but I don't think they would help that much.

-Koola1d
  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 13 December 2010 - 12:10 AM

I'm kinda confused, is it a new dialog you want, a total new frame?
Or do you just need to change the centerpanel and northpanel?
  • 0

#3 Koola1d

Koola1d

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 13 December 2010 - 05:07 AM

Well I was originally thinking to just have a new frame pop up.... but maybe it would make more sense to just change the north and center panels? It would probably be better to just change the panels... since once I learn how to do that correctly ill probably be using it a lot more throughout the program.
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 13 December 2010 - 09:12 AM

I got some code lieing / lying / laying .. whatever . I got some code around, it's on my laptop i'll post it when i get on it... propably tomorrow.
  • 0

#5 Koola1d

Koola1d

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 13 December 2010 - 01:13 PM

Awesome thanks
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 14 December 2010 - 09:37 AM

There we go:
public class Gui extends JFrame {
    private JPanel mainPanel;
    private String state;

    public Gui() {
        super();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel = new JPanel();
        Color color = new Color(0, 24, 122);
        mainPanel.setBackground(color);
        add(mainPanel);
        setSize(1680, 1000);
        createMainMenu();
        setVisible(true);
    }

    public void createMainMenu() {
        state = "menu";
        mainPanel.add(new MainMenu(), 0);
        mainPanel.revalidate();
    }

    public void createGameFrame() {
        mainPanel.remove(0);
        mainPanel.add(new GamePanel(), 0);
        mainPanel.revalidate();         
        state = "game";
    }
....   
}

MainMenu and GamePanel are just 2 classes which extend JPanel.
My MainMenu looks like:
public class MainMenu extends JPanel {
    private JPanel centerPanel, northPanel, centerCenterPanel;
    private Color color = new Color(0, 24, 122);
    private JButton startButton, quitButton;
    private JLabel welcomeLabel;

    public MainMenu() {
        super(new BorderLayout());
        init();
        createMainMenu();
    }

    private void init() {
        northPanel = new JPanel();
        centerPanel = new JPanel();
        centerCenterPanel = new JPanel();
        centerCenterPanel.setLayout(new BoxLayout(centerCenterPanel, BoxLayout.PAGE_AXIS));
        startButton = new MenuButton("start");
        quitButton = new MenuButton("quit");
        welcomeLabel = new JLabel(new ImageIcon("Images\\welcome.png"));
    }

    private void createMainMenu() {
        northPanel.add(welcomeLabel);
        centerCenterPanel.add(startButton);
        centerCenterPanel.add(quitButton);

        centerPanel.add(centerCenterPanel);
        add(centerPanel, BorderLayout.CENTER);
        add(northPanel, BorderLayout.NORTH);

        centerPanel.setBackground(color);
        centerCenterPanel.setBackground(color);
        northPanel.setBackground(color);
    }
}

  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download