Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [Java] How to a JFrame/JSplitPane?! HELP
New Posts  All Forums:Forum Nav:

[Java] How to a JFrame/JSplitPane?! HELP

post #1 of 3
Thread Starter 
I'm remaking FloodIt!/InkSpill for a class project and my teacher said that we need to learn graphics on our own. This is my program (I didn't include other classes) I tried .initialize() .update() .pack() and I can't get it to work. I don't expect anyone to read the whole program but any suggestions are great!

I will highlight important parts

Draw class creates a random grid of JPanels and returns it

Quote:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSplitPane;


public class GUI extends JFrame implements ActionListener
{

public int colorSelected;

private static final long serialVersionUID = 1L;
private JMenuBar jJMenuBar = null;
private JMenu File = null;
private JMenu Options = null;
private JMenu Difficulty = null;
private JRadioButtonMenuItem ChooseRadioButton = null;
private JMenuItem Pause = null;
private JMenuItem Exit = null;
private JRadioButton Impossible = null;
private JRadioButton Hard = null;
private JRadioButton Medium = null;
private JRadioButton Easy = null;
private JMenu Help = null;
private JMenuItem HowToPlay = null;
private JMenuItem About = null;
private JPanel jPanel = null;
private JSplitPane jSplitPane = null;
private JPanel jContentPane2 = null;
private JPanel jPanel2 = null;
private JPanel jPanelGame=null;
private JButton Redbutton = null;
private JButton Bluebutton = null;
private JButton Purplebutton = null;
private JButton Yellowbutton = null;
private JButton GreenButton = null;
private int numMovesLeft=0;
private JPanel jPanel3 = null;
private JLabel jLabel = null;
private ButtonGroup DifficultyMenu = null;
public Draw draw= new Draw();


/**
* This is the default constructor
*/
public GUI()
{

super();
jPanelGame=draw.GetPanel();
initialize();



}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(491, 353);
this.setContentPane(getJPanel());
this.setJMenuBar(getJJMenuBar());
this.setTitle("FloodIt! ");
}


/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.setLayout(new BoxLayout(getJJMenuBar(), BoxLayout.X_AXIS));
jJMenuBar.add(getFile());
jJMenuBar.add(getOptions());
jJMenuBar.add(getHelp());
}
return jJMenuBar;
}

/**
* This method initializes File
*
* @return javax.swing.JMenu
*/
private JMenu getFile() {
if (File == null) {
File = new JMenu();
File.setText("File");
File.add(getPause());
File.add(getExit());
}
return File;
}

/**
* This method initializes Options
*
* @return javax.swing.JMenu
*/
private JMenu getOptions() {
if (Options == null) {
Options = new JMenu();
Options.setText("Options");
Options.add(getDifficulty());
}
return Options;
}

/**
* This method initializes Difficulty
*
* @return javax.swing.JMenu
*/
private JMenu getDifficulty() {
if (Difficulty == null) {
Difficulty = new JMenu();
Difficulty.setText("Difficulty");
Difficulty.add(getChooseRadioButton());
Difficulty.add(getImpossible());
Difficulty.add(getHard());
Difficulty.add(getMedium());
Difficulty.add(getEasy());
DifficultyMenu = new ButtonGroup();
DifficultyMenu.add(getChooseRadioButton());
DifficultyMenu.add(getImpossible());
DifficultyMenu.add(getHard());
DifficultyMenu.add(getMedium());
DifficultyMenu.add(getEasy());
}
return Difficulty;
}

/**
* This method initializes ChooseRadioButton
*
* @return javax.swing.JRadioButtonMenuItem
*/
private JRadioButtonMenuItem getChooseRadioButton() {
if (ChooseRadioButton == null) {
ChooseRadioButton = new JRadioButtonMenuItem();
ChooseRadioButton.setText("Please choose one:");
}
return ChooseRadioButton;
}

/**
* This method initializes Pause
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getPause() {
if (Pause == null) {
Pause = new JMenuItem();
Pause.setText("Pause");
}
return Pause;
}

/**
* This method initializes Exit
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getExit() {
if (Exit == null) {
Exit = new JMenuItem();
Exit.setText("Exit");
}
return Exit;
}

/**
* This method initializes Impossible
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getImpossible() {
if (Impossible == null) {
Impossible = new JRadioButton();
Impossible.setText("Impossible");
}
return Impossible;
}

/**
* This method initializes Hard
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getHard() {
if (Hard == null) {
Hard = new JRadioButton();
Hard.setText("Hard");
}
return Hard;
}

/**
* This method initializes Medium
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getMedium() {
if (Medium == null) {
Medium = new JRadioButton();
Medium.setText("Medium");
Medium.setSelected(true);
}
return Medium;
}

/**
* This method initializes Easy
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getEasy() {
if (Easy == null) {
Easy = new JRadioButton();
Easy.setText("Easy");
}
return Easy;
}

/**
* This method initializes Help
*
* @return javax.swing.JMenu
*/
private JMenu getHelp() {
if (Help == null) {
Help = new JMenu();
Help.setText("Help");
Help.add(getHowToPlay());
Help.add(getAbout());
}
return Help;
}

/**
* This method initializes HowToPlay
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getHowToPlay() {
if (HowToPlay == null) {
HowToPlay = new JMenuItem();
HowToPlay.setText("How To Play");
}
return HowToPlay;
}

/**
* This method initializes About
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getAbout() {
if (About == null) {
About = new JMenuItem();
About.setText("About");
}
return About;
}

/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
jPanel = new JPanel();
jPanel.setBackground(Color.darkGray);
jPanel.setLayout(new GridBagLayout());
jPanel.add(getJSplitPane(), gridBagConstraints);
}
return jPanel;
}

/**
* This method initializes jSplitPane
*
* @return javax.swing.JSplitPane
*/
private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jSplitPane = new JSplitPane();
jSplitPane.setBackground(Color.darkGray);
jSplitPane.setForeground(Color.darkGray);
}

jSplitPane.setOneTouchExpandable(false);
jSplitPane.setLeftComponent(getJContentPane2());
jSplitPane.setRightComponent(jPanelGame);
jSplitPane.setDividerSize(0);
jSplitPane.setDividerLocation(150);
return jSplitPane;
}


/**
* This method initializes jContentPane2
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane2() {
if (jContentPane2 == null) {
jLabel = new JLabel();
jLabel.setBackground(Color.darkGray);
jLabel.setForeground(Color.white);
jLabel.setText("You have "+numMovesLeft+" moves left");
jContentPane2 = new JPanel();
jContentPane2.setBackground(Color.darkGray);
jContentPane2.setLayout(new BorderLayout());
jContentPane2.add(getJPanel2(), BorderLayout.SOUTH);
jContentPane2.add(jLabel, BorderLayout.NORTH);
}
return jContentPane2;
}

/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
gridBagConstraints7.gridx = 0;
gridBagConstraints7.gridy = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 3;
gridBagConstraints2.insets = new Insets(7, 0, 7, 0);
gridBagConstraints2.ipadx = 10;
gridBagConstraints2.ipady = 25;
gridBagConstraints2.gridy = 10;
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.gridx = 3;
gridBagConstraints5.insets = new Insets(7, 0, 7, 0);
gridBagConstraints5.ipadx = 10;
gridBagConstraints5.ipady = 25;
gridBagConstraints5.anchor = GridBagConstraints.CENTER;
gridBagConstraints5.gridy = 1;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.gridx = 3;
gridBagConstraints4.insets = new Insets(7, 0, 7, 0);
gridBagConstraints4.ipadx = 10;
gridBagConstraints4.ipady = 25;
gridBagConstraints4.gridy = 9;
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.gridx = 3;
gridBagConstraints3.insets = new Insets(7, 0, 7, 0);
gridBagConstraints3.ipadx = 10;
gridBagConstraints3.ipady = 25;
gridBagConstraints3.gridy = 8;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 3;
gridBagConstraints1.anchor = GridBagConstraints.CENTER;
gridBagConstraints1.insets = new Insets(7, 0, 7, 0);
gridBagConstraints1.ipadx = 10;
gridBagConstraints1.ipady = 25;
gridBagConstraints1.fill = GridBagConstraints.NONE;
gridBagConstraints1.gridy = 7;
jPanel2 = new JPanel();
jPanel2.setLayout(new GridBagLayout());
jPanel2.setBackground(Color.darkGray);
jPanel2.add(getRedbutton(), gridBagConstraints1);
jPanel2.add(getBluebutton(), gridBagConstraints3);
jPanel2.add(getPurplebutton(), gridBagConstraints4);
jPanel2.add(getYellowbutton(), gridBagConstraints5);
jPanel2.add(getGreenButton(), gridBagConstraints2);
jPanel2.add(getJPanel3(), gridBagConstraints7);
}
return jPanel2;
}

/**
* This method initializes Redbutton
*
* @return javax.swing.JButton
*/
private JButton getRedbutton() {
if (Redbutton == null) {
Redbutton = new JButton();
Redbutton.setBackground(Color.red);
Redbutton.addActionListener(this);

}
return Redbutton;
}

/**
* This method initializes Bluebutton
*
* @return javax.swing.JButton
*/
private JButton getBluebutton() {
if (Bluebutton == null) {
Bluebutton = new JButton();
Bluebutton.setBackground(Color.blue);
Bluebutton.addActionListener(this);
}
return Bluebutton;
}

/**
* This method initializes Purplebutton
*
* @return javax.swing.JButton
*/
private JButton getPurplebutton() {
if (Purplebutton == null) {
Purplebutton = new JButton();
Purplebutton.setBackground(Color.magenta);
Purplebutton.addActionListener(this);
}
return Purplebutton;
}

/**
* This method initializes Yellowbutton
*
* @return javax.swing.JButton
*/
private JButton getYellowbutton() {
if (Yellowbutton == null) {
Yellowbutton = new JButton();
Yellowbutton.setBackground(Color.yellow);
Yellowbutton.addActionListener(this);
}
return Yellowbutton;
}

/**
* This method initializes GreenButton
*
* @return javax.swing.JButton
*/
private JButton getGreenButton() {
if (GreenButton == null) {
GreenButton = new JButton();
GreenButton.setBackground(Color.green);
GreenButton.addActionListener(this);
}
return GreenButton;
}

/**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
jPanel3 = new JPanel();
jPanel3.setBackground(Color.darkGray);
jPanel3.setLayout(new GridBagLayout());
}
return jPanel3;
}

@Override
public void actionPerformed(ActionEvent event)
{
//gets the button that was clicked
Object source = event.getSource();
if(source==Bluebutton)
{
colorSelected=2;
draw.setChosen(2);
draw.Sort();
jPanelGame=draw.GetPanel();
}
else if(source==Redbutton)
{
colorSelected=0;
draw.setChosen(0);
draw.Sort();
jPanelGame=draw.GetPanel();
}
else if(source==Purplebutton)
{
colorSelected=4;
draw.setChosen(4);
draw.Sort();
jPanelGame=draw.GetPanel();

}
else if(source==Yellowbutton)
{
colorSelected=1;
draw.setChosen(1);
draw.Sort();
jPanelGame=draw.GetPanel();
jSplitPane.setRightComponent(jPanelGame);



}
else if(source==GreenButton)
{
colorSelected=3;
draw.setChosen(3);
draw.Sort();
jPanelGame=draw.GetPanel();
jSplitPane.setRightComponent(jPanelGame);




}
}

} // @jve:decl-index=0:visual-constraint="165,30"
Mitx Love
(13 items)
 
  
CPUMotherboardRAMHard Drive
Pentium G6950 Zotac H55 ITX 2x2GB Kingston 500GB Hitachi 
OSPowerMouseMouse Pad
Windows Server 2008 R2 Lite-on 300w Logitech Wireless Laser ULTRA wrist rest 
  hide details  
Reply
Mitx Love
(13 items)
 
  
CPUMotherboardRAMHard Drive
Pentium G6950 Zotac H55 ITX 2x2GB Kingston 500GB Hitachi 
OSPowerMouseMouse Pad
Windows Server 2008 R2 Lite-on 300w Logitech Wireless Laser ULTRA wrist rest 
  hide details  
Reply
post #2 of 3
Did not read code, however to add a Pane to the ContentPane of a JFrame,

aFrame.getContentPane().add(yourInstanceOfaJSplitPane)
http://download.oracle.com/javase/tutorial/uiswing/components/splitpane.html

Also, you should consider redesigning this, just looking at it, it is not clear why certain methods are the way they are-- why is Draw an object that has a getPanel() method? It sounds like you use it to initialize a Panel that has a grid layout with JPanels in every section?

protip: use windowbuilder or netbeans GUI builder, GUI is not meant to be written by hand, drag and drop elements where you can, add logic where necessary.
Edited by wcdolphin - 3/18/11 at 6:11pm
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
post #3 of 3
Thread Starter 
I used Eclipse Visual Editor. After hours of working on it, it finally worked somehow. As far as the code, my friend is competing in the General Electric(GE) programming competition and they were sent code to their own version own the game. The have to write a solver class which solves it in the least number of steps .The code is SOOOOOOO bad that he asked for a copy of mine to understand the basics of what they were attempting to do.
Mitx Love
(13 items)
 
  
CPUMotherboardRAMHard Drive
Pentium G6950 Zotac H55 ITX 2x2GB Kingston 500GB Hitachi 
OSPowerMouseMouse Pad
Windows Server 2008 R2 Lite-on 300w Logitech Wireless Laser ULTRA wrist rest 
  hide details  
Reply
Mitx Love
(13 items)
 
  
CPUMotherboardRAMHard Drive
Pentium G6950 Zotac H55 ITX 2x2GB Kingston 500GB Hitachi 
OSPowerMouseMouse Pad
Windows Server 2008 R2 Lite-on 300w Logitech Wireless Laser ULTRA wrist rest 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [Java] How to a JFrame/JSplitPane?! HELP