GridLayout ButtonPanel not Displaying Rows/Columns specified - java

So this code specifically says that there should be three rows and one column for JPanel selectPanel, however, when I run it I get this result and just can't figure out what I'm doing wrong. Most of this code is not relevant to the problem I think. The beginning is where I make the panel that is giving me issues (selectPanel) and the end is where that panel is added to the JFrame (roiguide).
Result of Code:
JPanel selectPanel = new JPanel();
selectPanel.setLayout(new GridLayout(3,1,0,0));
selectPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
imp.getWindow().toFront();
IJ.setTool(Toolbar.RECTANGLE);
referenceButton = new JButton("Add reference region");
referenceButton.setEnabled(true);
referenceButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
refDefine();
}
});
refLabel = new JLabel(" ("+printformat.format(rno)+"/1)");
selectPanel.add(referenceButton);
selectPanel.add(refLabel);
backgroundButton = new JButton("Add background region");
backgroundButton.setEnabled(true);
backgroundButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
brDefine();
}
});
backgroundLabel = new JLabel(" ("+printformat.format(bno)+"/"+printformat.format(spotno)+")");
selectPanel.add(backgroundButton);
selectPanel.add(backgroundLabel);
spotButton = new JButton("Add spot");
spotButton.setEnabled(true);
spotButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
spotDefine();
}
});
spotLabel = new JLabel(" ("+printformat.format(sno)+"/"+printformat.format(spotno)+")");
selectPanel.add(spotButton);
selectPanel.add(spotLabel);
JPanel editPanel = new JPanel();
selectPanel.setLayout(new GridLayout(2,1,20,20));
selectPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
jComboBox1 = new JComboBox();
editPanel.add(jComboBox1);
JButton editButton = new JButton("Edit ROIs");
editButton.setEnabled(true);
editButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String item = (String) jComboBox1.getSelectedItem();
Roi roi = overlay.get(overlay.getIndex(item));
}
});
editPanel.add(editButton);
// Create the buttonPanel, which has the "Cancel" and "OK" buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,2,20,20));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JButton cancelButton = new JButton("Cancel");
cancelButton.setEnabled(true);
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
didCancel = true;
roiguide.dispose();
overlay.clear();
}
});
buttonPanel.add(cancelButton);
JButton okButton = new JButton("OK");
okButton.setEnabled(true);
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(current!=roino){
IJ.error("ROI Manager","Please select " + roino + " regions of interest.");
}else{
canContinue=true;
roiguide.dispose();
}
}
});
buttonPanel.add(okButton);
// Create and populate the JFrame
roiguide = new JFrame("Add regions of interest:");
roiguide.getContentPane().add(selectPanel, BorderLayout.NORTH);
roiguide.getContentPane().add(editPanel, BorderLayout.WEST);
roiguide.getContentPane().add(buttonPanel, BorderLayout.EAST);
roiguide.pack();
roiguide.setLocation(400,400);
roiguide.setVisible(true);
roiguide.setResizable(false);

the addition of a new feature broke the display
Then you know what you changed so you should know where to start looking:
This looks suspicious:
JPanel editPanel = new JPanel();
selectPanel.setLayout(new GridLayout(2,1,20,20));

Related

null pointer error at textfield even though value is entered

I keep getting a null exception error and I cant seem to fix it. I know a null pointer error is usually raised when your trying to use a value that is null but I pass a value to the textfield so Im confused. Looking for feedback:
exact error code: (this is where I add the actionlistner to button bt28)
"AWT-EventQueue-0" java.lang.NullPointerException
at GUI$4.actionPerformed(GUI.java:231) ...etc"
P.s. this class is called in a main class which is why that's not here. I've imported awt, swing and util.
public class GUI{
CardLayout cl=new CardLayout();
private String cardNum, pin;
JLabel header1,header2,header3,header4;
JButton bt01= new JButton("");
//bt01.setBounds(100,150,100,40);
JButton bt02= new JButton("");
JButton bt03= new JButton("");
JButton bt04= new JButton("");
JButton bt05= new JButton("");
JButton bt06= new JButton("");
JButton bt07= new JButton("");
JButton bt08= new JButton("Continue");
JButton bt21= new JButton("");
JButton bt22= new JButton("");
JButton bt23= new JButton("");
JButton bt24= new JButton("");
JButton bt25= new JButton("");
JButton bt26= new JButton("");
JButton bt27= new JButton("");
JButton bt28= new JButton("Continue");
JButton bt31= new JButton("");
JButton bt32= new JButton("");
JButton bt33= new JButton("");
JButton bt34= new JButton("");
JButton bt35= new JButton("Balance Query");
JButton bt36= new JButton("Withdraw");
JButton bt37= new JButton("Deposit");
JButton bt38= new JButton("");
JButton bt41= new JButton("A");
JButton bt42= new JButton("B");
JButton bt43= new JButton("C");
JButton bt44= new JButton("D");
JButton bt45= new JButton("E");
JButton bt46= new JButton("");
JButton bt47= new JButton("Business Accounts");
JButton bt48= new JButton("Cancel");
JPanel panelCont=new JPanel();
JPanel GUI1= new JPanel(new BorderLayout());
JPanel GUI2= new JPanel(new BorderLayout());
JPanel GUI3= new JPanel(new BorderLayout());
JPanel GUI4= new JPanel(new BorderLayout());
public GUI(){
JFrame frame1 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame1.setPreferredSize (new Dimension(500, 200));
header1= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel1= new JPanel();
nPanel1.add(header1);
GUI1.add(nPanel1,BorderLayout.NORTH);
JLabel cardnumLabel =new JLabel("Enter Card Number");
JTextField txt= new JTextField(15);
txt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cl){
cardNum=txt.getText();
txt.setText(null);
}
});
JPanel cPanel1= new JPanel();
cPanel1.add(cardnumLabel);
cPanel1.add(txt);
GUI1.add(cPanel1, BorderLayout.CENTER);
JPanel wPanel1=new JPanel(new GridLayout(4,0));
wPanel1.add(bt01);
wPanel1.add(bt02);
wPanel1.add(bt03);
wPanel1.add(bt04);
JPanel ePanel1=new JPanel(new GridLayout(4,0));
ePanel1.add(bt05);
ePanel1.add(bt06);
ePanel1.add(bt07);
ePanel1.add(bt08);
GUI1.add(wPanel1,BorderLayout.WEST);
GUI1.add(ePanel1,BorderLayout.EAST);
//................................
// .
// GUI2 .
//................................
JFrame frame2 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame2.setPreferredSize (new Dimension(500, 200));
header2= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel2= new JPanel();
nPanel2.add(header1);
GUI2.add(nPanel2,BorderLayout.NORTH);
JLabel pinLabel =new JLabel("Enter your pin");
JTextField txt2= new JTextField(15);
txt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cl){
pin=txt2.getText();
txt2.setText(null);
}
});
JPanel cPanel2= new JPanel();
cPanel2.add(pinLabel);
cPanel2.add(txt2);
GUI2.add(cPanel2, BorderLayout.CENTER);
JPanel wPanel2=new JPanel(new GridLayout(4,0));
wPanel2.add(bt21);
wPanel2.add(bt22);
wPanel2.add(bt23);
wPanel2.add(bt24);
JPanel ePanel2=new JPanel(new GridLayout(4,0));
ePanel2.add(bt25);
ePanel2.add(bt26);
ePanel2.add(bt27);
ePanel2.add(bt28);
GUI2.add(wPanel2,BorderLayout.WEST);
GUI2.add(ePanel2,BorderLayout.EAST);
//................................
// .
// GUI3 .
//................................
JFrame frame3 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame3.setPreferredSize (new Dimension(500, 200));
header3= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel3= new JPanel();
nPanel3.add(header1);
GUI3.add(nPanel3,BorderLayout.NORTH);
JLabel txt3 =new JLabel("Please select from the buttons on the right");
JPanel cPanel3= new JPanel();
cPanel3.add(txt3);
GUI3.add(cPanel3, BorderLayout.CENTER);
JPanel wPanel3=new JPanel(new GridLayout(4,0));
wPanel3.add(bt31);
wPanel3.add(bt32);
wPanel3.add(bt33);
wPanel3.add(bt34);
JPanel ePanel3=new JPanel(new GridLayout(4,0));
ePanel3.add(bt35);
ePanel3.add(bt36);
ePanel3.add(bt37);
ePanel3.add(bt38);
GUI3.add(wPanel3,BorderLayout.WEST);
GUI3.add(ePanel3,BorderLayout.EAST);
//..................................
// .
// GUI4 .
//..................................
JFrame frame4 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame4.setPreferredSize (new Dimension(500, 200));
header4= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel4= new JPanel();
nPanel4.add(header4);
GUI4.add(nPanel4,BorderLayout.NORTH);
JPanel cPanel4= new JPanel();
GUI4.add(cPanel4, BorderLayout.CENTER);
JPanel wPanel4=new JPanel(new GridLayout(4,0));
wPanel4.add(bt41);
wPanel4.add(bt42);
wPanel4.add(bt43);
wPanel4.add(bt44);
JPanel ePanel4=new JPanel(new GridLayout(4,0));
ePanel4.add(bt45);
ePanel4.add(bt46);
ePanel4.add(bt47);
ePanel4.add(bt48);
GUI4.add(wPanel4,BorderLayout.WEST);
GUI4.add(ePanel4,BorderLayout.EAST);
//---------------------------------------------------
panelCont.setLayout(cl);
panelCont.add(GUI1, "1");
panelCont.add(GUI2, "2");
panelCont.add(GUI3, "3");
cl.show(panelCont,"1");
String a ="100001 1111 15984789.74";
bt08.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"2");
}
});
bt28.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent r){
if (cardNum.equals(a.substring(0, a.indexOf(" "))) && pin.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" ")))){
cl.show(panelCont,"3");
String acc="a";
}
else{
JOptionPane.showMessageDialog(null,"Invalid accoun number or pin");
cl.show(panelCont,"3");
}
}
});
bt35.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
bt36.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
bt37.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
frame1.add(panelCont);
//(JFrame.DO_NOTHING_ON_CLOSE)
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
}
}
The issue is that your values for cardNum and pin are never assigned in the actionListeners for txt and txt2. You should pull the values in the event that the user clicks the continue button. See my code below.
bt28.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent r) {
cardNum = txt.getText();
pin = txt2.getText();
if (cardNum.equals(a.substring(0, a.indexOf(" "))) && pin.equals(a.substring(a.indexOf(" ") + 1, a.lastIndexOf(" ")))) {
cl.show(panelCont, "3");
String acc = "a";
} else {
JOptionPane.showMessageDialog(null, "Invalid accoun number or pin");
cl.show(panelCont, "3");
}
}
});

Java Swing Components Drawing Over Multiple Panels

I'm having an issue that I've been trying to solve all day. I have two panels inside a frame and in one panel I've added components. The components, however, seem to have an equal hierarchy value as the panel and as such treat the Frame as the parent instead, ignoring where the panels are located. Basically the components are added to one panel but display on top of both across the whole frame. (Unfortunately I can't post a picture at this time, please up vote the question to allow me to if needed). What is causing this and how can I fix it?
The relevant code:
JFrame arranFrame = new JFrame("Edit Arrangement");
arranFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel grid = new JPanel();
JPanel tools = new JPanel();
grid.setSize(700, 600);
tools.setSize(200, 600);
JLabel widthLabel = new JLabel("Width");
JLabel depthLabel = new JLabel("Depth");
JLabel columnLabel = new JLabel("Columns");
final JTextField rowWidthInput = new JTextField(30);
final JTextField rowsInput = new JTextField(30);
final JTextField columnInput = new JTextField(30);
rowWidthInput.setSize(40, 30);
rowsInput.setSize(40, 30);
columnInput.setSize(40, 30);
rowWidthInput.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
chartWidth = Integer.parseInt(rowWidthInput.getText());
}
});
rowsInput.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
chartRows = Integer.parseInt(rowsInput.getText());
}
});
columnInput.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
chartColumns = Integer.parseInt(columnInput.getText());
}
});
//Enter button for grid modifying creation and setup
JButton enterButton = new JButton("Enter");
enterButton.setSize(180, 50);
enterButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
chartWidth = Integer.parseInt(rowWidthInput.getText());
chartRows = Integer.parseInt(rowsInput.getText());
chartColumns = Integer.parseInt(columnInput.getText());
}
});
//Save Arrangement button creation and setup
JButton saveButton = new JButton("Save Arrangement");
saveButton.setSize(180, 50);
saveButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
SaveArrangement(userDocuments + "\\TSA Seating Chart\\Arrangements\\Period" + period + ".txt");
}
});
tools.setBackground(Color.RED); //Temp for visualization where the panes are
tools.add(widthLabel);
tools.add(rowWidthInput);
tools.add(depthLabel);
tools.add(rowsInput);
tools.add(columnLabel);
tools.add(columnInput);
tools.add(enterButton);
tools.add(saveButton);
arranFrame.getContentPane().add(grid);
arranFrame.getContentPane().add(tools);
//Finalize the GUI
arranFrame.pack(); //Pack all the content together
arranFrame.setSize(900, 600); //Set the size of the window
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
arranFrame.setLocation(dim.width/2-arranFrame.getSize().width/2, dim.height/2-arranFrame.getSize().height/2);
arranFrame.setVisible(true); //Display the seatingFrame

Adding ActionListeners to JPanel

This questions has been asked a few times but mine is a little different. I created a small application and in the view I added a few JPanels to a JFrame. I then try to add actionListeners in the controller which is where the problem happened.
The code below gives me the following error:
The method addActionListener(new ActionListener(){})
is undefined for the type JPanel
The view class
public class MainMenuGUI {
JTabbedPane tabbedPane = new JTabbedPane();
JPanel findUserPanel;
JPanel deleteUserPanel;
JPanel addUserPanel;
JFrame frame = new JFrame();
JPanel tabbedPanel = new JPanel();
//Controller class for tabbedPanel
ControllerTabbedPane listen = new ControllerTabbedPane(this);
//Controller class for findUserPanel
FindUserPanelController findUserController = new FindUserPanelController(findUserPanel);
public MainMenuGUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
findUserPanel = createFindUserPanel();
deleteUserPanel = createDeleteUserPanel();
addUserPanel = createAddUserPanel();
tabbedPane.addTab("Find User", findUserPanel);
tabbedPane.addTab("Delete User", deleteUserPanel);
tabbedPane.addTab("Add User", addUserPanel);
tabbedPanel.add(tabbedPane);
frame.add(tabbedPanel);
frame.pack();
// opens frame in the center of the screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
JPanel createFindUserPanel() {
findUserPanel = new JPanel();
findUserPanel.setPreferredSize(new Dimension(300, 300));
findUserPanel.setLayout(new GridLayout(5, 7));
JLabel firstlbl = new JLabel("First Name");
JLabel lastlbl = new JLabel("Last Name");
JLabel addresslbl = new JLabel("Address");
JLabel agelbl = new JLabel("Age");
JTextField firstNametxt = new JTextField(15);
JTextField lastNametxt = new JTextField(15);
JTextField addresstxt = new JTextField(30);
JTextField age = new JTextField(3);
JButton btn = new JButton("Submit");
JScrollPane window = new JScrollPane();
window.setViewportBorder(new LineBorder(Color.RED));
window.setPreferredSize(new Dimension(150, 150));
findUserPanel.add(firstlbl);
findUserPanel.add(firstNametxt);
findUserPanel.add(lastlbl);
findUserPanel.add(lastNametxt);
findUserPanel.add(addresslbl);
findUserPanel.add(addresstxt);
findUserPanel.add(agelbl);
findUserPanel.add(age);
findUserPanel.add(window, BorderLayout.CENTER);
findUserPanel.add(btn);
return findUserPanel;
}
Controller Class
public class ControllerTabbedPane {
MainMenuGUI mainMenuGUI;
int currentTabbedIndex = 0;
ControllerTabbedPane(MainMenuGUI mainMenuGUI){
this.mainMenuGUI = mainMenuGUI;
addTabbedPaneListeners();
}
private void addTabbedPaneListeners() {
mainMenuGUI.tabbedPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent ce) {
currentTabbedIndex = mainMenuGUI.tabbedPane.getSelectedIndex();
System.out.println("Current tab is:" + currentTabbedIndex);
}
});
}
/*ERROR saying The method addActionListener(new ActionListener(){})
is undefined for the type JPanel*/
private void findPanelListeners() {
mainMenuGUI.findUserPanel.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
}
This is the way to achieve your request:
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton bt1 = new JButton();
JButton bt2 = new JButton();
panel1.add(bt1);
panel2.add(bt2);
bt1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Bt1 on panel1 pressed");
}
});
bt2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Bt2 on panel2 pressed");
}
});
You can modify variables or other objects into the listeners to store which panel was "pressed".
I think its not possible to add addActionListner() to JPanel
Instead
You can use,
JPanel p1=new JPanel();
p1.addMouseListener(this);
And override
public void mouseClicked(MouseEvent me)
{
int x=me.getX();
int y=me.getY();
System.out.println(x+","+y);
//By using x AND y you can identify the panel
}
NB: extends MouseAdapter

How to add a focus traversal with a different hotkey?

I am having a problem implementing the last part of this program, which is to make it where pressing the Enter key moves the active field from one text field to the next one in order (1,2,3,4).
This needs to be done without removing the focus cycling that is used by the tab key.
I have no idea how to even begin doing that, the only thing I've found in the API is replacing the traversal policy with your own, but I don't want to replace it I want to create a new one that runs in parallel with the default one.
public class unit27 {
JButton button1 = new JButton("add to next cup");
JButton button2 = new JButton("add to next cup");
JButton button3 = new JButton("add to next cup");
JButton button4 = new JButton("add to next cup");
JTextField text1 = new JTextField("4");
JTextField text2 = new JTextField("4");
JTextField text3 = new JTextField("4");
JTextField text4 = new JTextField("4");
JLabel label1 = new JLabel("Cup 1");
JLabel label2 = new JLabel("Cup 2");
JLabel label3 = new JLabel("Cup 3");
JLabel label4 = new JLabel("Cup 4");
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
public unit27() {
mainPanel.setLayout(new GridLayout(2, 4));
panel1.setLayout(new GridLayout(1, 3));
panel2.setLayout(new GridLayout(1, 3));
panel3.setLayout(new GridLayout(1, 3));
panel4.setLayout(new GridLayout(1, 3));
frame.add(mainPanel);
frame.setSize(800, 800);
frame.setVisible(true);
mainPanel.add(panel1);
mainPanel.add(panel2);
mainPanel.add(panel3);
mainPanel.add(panel4);
panel1.add(label1);
panel1.add(button1);
panel1.add(text1);
panel2.add(label2);
panel2.add(button2);
panel2.add(text2);
panel3.add(label3);
panel3.add(button3);
panel3.add(text3);
panel4.add(label4);
panel4.add(button4);
panel4.add(text4);
button1.add(text1);
button1.addActionListener(new MyListener1());
button2.addActionListener(new MyListener2());
button3.addActionListener(new MyListener3());
button4.addActionListener(new MyListener4());
// end class
}
class MyListener1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(text1.getText());
int b = Integer.parseInt(text2.getText());
int c = a + b;
text2.setText(Integer.toString(c));
}
}
class MyListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(text2.getText());
int b = Integer.parseInt(text3.getText());
int c = a + b;
text3.setText(Integer.toString(c));
}
}
class MyListener3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(text3.getText());
int b = Integer.parseInt(text4.getText());
int c = a + b;
text4.setText(Integer.toString(c));
}
}
class MyListener4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(text4.getText());
int b = Integer.parseInt(text1.getText());
int c = a + b;
text1.setText(Integer.toString(c));
}
}
public static void main(String[] args) {
new unit27();
}
}
Swing does not allow 2 focus cycles to be run at the same time. If you really need to have a second focus cycle (and not just add enter to the focus traversal keys), then what you could do is:
Create a Map which determines your cycle.
On each component with the extra cycle add a key binding for Enter that gets the next visible field in your cycle and calls requestFocus on it.
You would have to maintain the map and create custom checks to see if the next component is visible or should be skipped.

"closebutton" won't respond in second frame

The button "closebutton" does not respond. I'm very new to java and I know the error has something to do with actionpreformed in the nested if. I've set up a test to see if words will print the the console when closebutton is pressed. They don't. Any help is appreciated
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myGuiTester implements ActionListener
{
JPanel pane = new JPanel();
JPanel mainPanel = new JPanel();
JFrame MenuFrame = new JFrame("Main Menu");
JFrame Instructions = new JFrame("Instructions");
JFrame game = new JFrame("Game");
JPanel gamepan = new JPanel();
JFrame inst = new JFrame("Instructions");
JPanel instpan = new JPanel(new FlowLayout());
JButton playButton = new JButton("Play");
JButton instButton = new JButton("Instructions");
JButton exitButton = new JButton("Exit");
JButton closebutton = new JButton("Close");
public static void main(String[] args)
{
new myGuiTester ();
}
public myGuiTester ()
{
MenuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inst.setVisible(false);
game.setVisible(false);
JLabel title = new JLabel(" Slot Machine");
JLabel blank = new JLabel("");
JLabel blank1 = new JLabel("");
JLabel blank2 = new JLabel("");
JLabel blank3 = new JLabel("");
playButton.addActionListener(this);
instButton.addActionListener(this);
JLabel game1 = new JLabel("Test");
JLabel inst1 = new JLabel();
exitButton.addActionListener(this);
closebutton.addActionListener(this);
pane.setLayout(new GridLayout(9,1));
pane.add(blank);
pane.add(title);
pane.add(blank1);
pane.add(playButton);
pane.add(blank2);
pane.add(instButton);
pane.add(blank3);
pane.add(exitButton);
mainPanel.add(pane);
MenuFrame.setContentPane(mainPanel);
MenuFrame.setSize(500,400);
MenuFrame.setVisible(true);
//Game Panel
game.setContentPane(gamepan);
game.setSize(500,400);
game1.setText("Game goes here");
gamepan.add(game1);
//Instructions Panel
inst.setContentPane(instpan);
inst.setSize(500,300);
inst1.setText("Instructions go here.");
instpan.add(inst1);
instpan.add(closebutton);
}
//ActionListener
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("Play"))
{
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
MenuFrame.setVisible(false);
System.out.print("test play");
}
else if (e.getActionCommand().equals("Instructions"))
{
inst.setVisible(true);
//suspected error
if (e.getActionCommand().equals("Close"))
{
System.out.print("Test inst");
inst.setVisible(false);
}
}
else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
}
You must handle "Close" in the elseif- statement but not inside "Instructions. I should be its own block like:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Play")) {
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
MenuFrame.setVisible(false);
System.out.print("test play");
} else if (e.getActionCommand().equals("Instructions")) {
inst.setVisible(true);
// suspected error
}else if (e.getActionCommand().equals("Close")) {
System.out.print("Test inst");
inst.setVisible(false);
} else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
When you check which button is called you are comparing the ActionCommand of the Event with the label of the button, which are not the same thing :
e.getActionCommand().equals("Close")
What you should do is checking if the button IS the source of the event
e.getSource() == closeButton

Categories