I was in the middle of making a program to remove commented lines from any file, I had just finished the main GUI and added two action listeners to my JButtons and then it all broke. I feel like I made a really stupid mistake somewhere but I can't figure out where. Any ideas? (major region is commented out)
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.Box;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Component;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.Label;
import java.awt.Font;
public class ReplaceComments{
public static void main(String[]args){
createInitialWindow();
}
public static void createInitialWindow(){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = (int)screenSize.getWidth();
int h = (int)screenSize.getHeight();
System.out.println("Width: " + w + "\nHeight: " + h + "\nCreating customized window...");
JLabel explanation1 = new JLabel("Welcome to Comment Replacer 1.0!");
explanation1.setFont(new Font("Verdana", Font.PLAIN, w/80));
explanation1.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel explanation2 = new JLabel("Simply paste in the file you want");
explanation2.setFont(new Font("Verdana", Font.PLAIN, w/80));
explanation2.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel explanation3 = new JLabel("to remove comments from, and the");
explanation3.setFont(new Font("Verdana", Font.PLAIN, w/80));
explanation3.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel explanation4 = new JLabel("folder it should output to.");
explanation4.setFont(new Font("Verdana", Font.PLAIN, w/80));
explanation4.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel Source = new JLabel("Source: ");
Source.setFont(new Font("Verdana", Font.PLAIN, w/80));
JTextField source = new JTextField(w/130);
source.setFont(new Font("Verdana", Font.PLAIN, w/80));
JLabel Output = new JLabel("Output: ");
Output.setFont(new Font("Verdana", Font.PLAIN, w/80));
JTextField output = new JTextField(w/130);
output.setFont(new Font("Verdana", Font.PLAIN, w/80));
/* \/ This Part \/ */
JButton replace = new JButton("Replace");
replace.setFont(new Font("Verdana", Font.PLAIN, w/80));
replace.setSize(new Dimension(w/8,h/8));
replace.addActionListener(new CustomActionListener(){ /*********************/
public void actionPerformed(ActionEvent e){ /* Added this action */
replaceButtonClicked(); /* listener and it */
} /* all broke :3 */
}); /*********************/
JButton cancel = new JButton("Cancel");
cancel.setFont(new Font("Verdana", Font.PLAIN, w/80));
cancel.setSize(new Dimension(w/8,h/8));
cancel.addActionListener(new CustomActionListener(){ /*********************/
public void actionPerformed(ActionEvent e){ /* Added this action */
cancelButtonClicked(); /* listener and it */
} /* all broke :3 */
}); /*********************/
/* /\ This Part /\ */
ButtonGroup screen1 = new ButtonGroup();
screen1.add(replace);
screen1.add(cancel);
Box info = Box.createVerticalBox();
info.add(Box.createVerticalStrut(w/100));
info.add(explanation1);
info.add(explanation2);
info.add(explanation3);
info.add(explanation4);
Box sourceBox = Box.createHorizontalBox();
sourceBox.add(Source);
sourceBox.add(source);
Box outputBox = Box.createHorizontalBox();
outputBox.add(Output);
outputBox.add(output);
Box textBoxes = Box.createVerticalBox();
info.add(Box.createVerticalStrut(w/21));
textBoxes.add(sourceBox);
textBoxes.add(outputBox);
Box buttons = Box.createHorizontalBox();
buttons.add(replace);
buttons.add(Box.createHorizontalStrut(w/50));
buttons.add(cancel);
buttons.add(Box.createVerticalStrut(w/30));
JPanel upperPanel = new JPanel();
upperPanel.add(info);
JPanel middlePanel = new JPanel();
middlePanel.add(textBoxes);
JPanel lowerPanel = new JPanel();
lowerPanel.add(buttons);
BorderLayout border = new BorderLayout();
JFrame frameWindow = new JFrame("Comment Replacer v1.0");
frameWindow.add(upperPanel,BorderLayout.NORTH);
frameWindow.add(middlePanel,BorderLayout.CENTER);
frameWindow.add(lowerPanel,BorderLayout.SOUTH);
frameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameWindow.setSize(w/2,h/2);
frameWindow.setLocationRelativeTo(null);
frameWindow.setVisible(true);
System.out.println("Done!");
}
public void replaceButtonClicked(){
System.out.println("Replace button clicked!");
// Do something else
}
public void cancelButtonClicked(){
System.out.println(";-;");
System.exit(0);
}
}
change CustomActionListener by ActionListener
/* \/ This Part \/ */
JButton replace = new JButton("Replace");
replace.setFont(new Font("Verdana", Font.PLAIN, w/80));
replace.setSize(new Dimension(w/8,h/8));
replace.addActionListener(new ActionListener(){ /*********************/
public void actionPerformed(ActionEvent e){ /* Added this action */
replaceButtonClicked(); /* listener and it */
} /* all broke :3 */
}); /*********************/
JButton cancel = new JButton("Cancel");
cancel.setFont(new Font("Verdana", Font.PLAIN, w/80));
cancel.setSize(new Dimension(w/8,h/8));
cancel.addActionListener(new ActionListener(){ /*********************/
public void actionPerformed(ActionEvent e){ /* Added this action */
cancelButtonClicked(); /* listener and it */
} /* all broke :3 */
}); /*********************/
/* /\ This Part /\ */
and change the methods to static, :
public static void replaceButtonClicked(){
System.out.println("Replace button clicked!");
// Do something else
}
public static void cancelButtonClicked(){
System.out.println(";-;");
System.exit(0);
}