-3

So I am working in java, and have the buttons set up. The only thing I want to do now is separate the buttons by putting so many on one side of the JFrame and so many on the other side of the JFrame. Any suggestions? oh and there is a JList that goes with one of the buttons.

[Update] Just updated my code, and so far, no improvement, still states that it won't open up the GUI.

  package SystemandDesign.RISK;


import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;

//Now for the JList options;

import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;
import javax.swing.JPanel;
//Will be used to created JList options

//Colors to be used for testing purposes.

import java.awt.Color;

//panel to separate buttons.
import java.awt.GridLayout;
import java.awt.BorderLayout;


public class GamePage extends JFrame{

  private JButton surrender;
  private JButton draw;
  private JButton attackRoll;
  private JButton defendRoll;
  private JButton trade;
  private JButton main;
  private JButton quit;
  private JFrame frame;
  private JPanel east;
  private JPanel west;



  private JList terNames;//Used primarily at the beginning of the game to reinforce territories.
  //terNames is also used to reinforce current owned areas.
  private JList terAttack;//Fills up as terrritories that are able to attack
  private static final String[] terProp = {"A-bombing site", "'A'ir-defense", "AA-testing",
    "A hint", "A-field", "'A'ir Force", "A-prep", "A-landing"};
  private static final Color[] terColor = {Color.RED, Color.BLUE,Color.GREEN,Color.PINK,
    Color.ORANGE,Color.MAGENTA,Color.LIGHT_GRAY, Color.YELLOW};//The use of colors is being used to test if the List is
  //responsive. Will later be taken out and replaced to highlight territories.



  public GamePage(){

    super("Risk");
    //set up the borders

    frame.add(east,BorderLayout.EAST);
    frame.add(west,BorderLayout.WEST);
    frame.setLayout(new BorderLayout());

    east.setLayout(new GridLayout(2,1));
    west.setLayout(new GridLayout(6,1));

    //JList
    terNames = new JList(terProp);
    terNames.setVisibleRowCount(3);
    terNames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    west.add(new JScrollPane(terNames));

    terNames.addListSelectionListener(new ListSelectionListener(){
      public void valueChanged(ListSelectionEvent event){
        getContentPane().setBackground(terColor[terNames.getSelectedIndex()]);
      }
    }
                                      );



    //First set the buttons. 
    //These will include the options to surrender:
    //Trade Cards
    //Draw Cards
    //Roll your attack dice
    //Roll your defense dice

    //JButtons
    surrender = new JButton("Surrender");
    draw = new JButton("Draw");
    attackRoll = new JButton("Attack");
    defendRoll = new JButton("Defend");
    trade = new JButton("Trade");
    main = new JButton("Main Menu");
    quit = new JButton("Quit");
    east.add(quit);
    east.add(main);
    west.add(surrender);
    west.add(draw);
    west.add(attackRoll);
    west.add(defendRoll);
    west.add(trade);


    ButtonHandler handler = new ButtonHandler();
    surrender.addActionListener(handler);
    draw.addActionListener(handler);
    attackRoll.addActionListener(handler);
    defendRoll.addActionListener(handler);
    trade.addActionListener(handler);
    main.addActionListener(handler);
    quit.addActionListener(handler);
    //End of JButtons
  }

  public static void main(String[]args){
    new GamePage();
  }
  //This is to handle the Buttons.
  private class ButtonHandler implements ActionListener{

    public void actionPerformed(ActionEvent event){

      if(event.getSource().equals(surrender)){
      }
      else if(event.getSource().equals(draw)){
      }
      else if(event.getSource().equals(attackRoll)){
      }
      else if(event.getSource().equals(defendRoll)){
      }
      else if(event.getSource().equals(trade)){
      }
      else if(event.getSource().equals(quit)){
        dispose();
      }
      else if(event.getSource().equals(main)){
        dispose();
        MainPage mainPage = new MainPage();
        mainPage.setDefaultCloseOperation(EXIT_ON_CLOSE);
        mainPage.setSize(900,600);
        mainPage.setVisible(true);
      }
    }
  }
}
  • 1
    _"Any suggestions?"_ - Yea, show us some code. If you have none, then get started. – Paul Samsotha Mar 11 '14 at 17:22
  • Here is the code I been working on. Just need to get the buttons to separate, but for some reason I can not get the GUI to show up because there is an error in the code preventing it to show up. – user3255993 Mar 11 '14 at 17:40

2 Answers2

2

You can create two JPanels into which you put your different buttons as you wish, then using a suitable layout on the JFrame (BorderLayout, GridBagLayout, there are a few possibilities), add the panels.

blueygh2
  • 1,538
  • 10
  • 15
1
  1. Don't create another JFrame, since your class is already a JFrame. OR just use the instance JFrame and don't extend JFrame for the class. See Extends JFrame vs. creating it inside the the program

  2. You are trying to add the buttons to the instance JFrame and the JScrollPane to the class JFrame you need to choose one or the other and stick to it.

  3. Use JPanel or JComponent as a container and not Container

  4. You can nest containers (JPanels) with different layout managers to get your desired look. See Laying out Components Within a Container for help if your confused. See this example for how you can use nested JPanels with different layout managers.

  5. "oh and there is a JList that goes with one of the buttons." - doesn't really make sense.

  6. I see you trying to dispose of your main frame and opening a new one. Don't do that. Instead use a CardLayout. The layout will "layer" panels and allow you to navigate through them. See How to Use CardLayout. Also see a simple example here

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720