JButton ActionListener (Java 8 JDK 1.8.0_74) 不工作
JButton ActionListener (Java 8 JDK 1.8.0_74) is not working
我正在制作一个程序来从任何文件中删除注释行,我刚刚完成了主 GUI 并向我的 JButtons 添加了两个动作侦听器,然后一切都崩溃了。我觉得我在某个地方犯了一个非常愚蠢的错误,但我不知道在哪里。有任何想法吗? (主要区域被注释掉)
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);
}
}
目前的代码不起作用,方法必须是静态的
public static void replaceButtonClicked(){
System.out.println("Replace button clicked!");
// Do something else
}
public static void cancelButtonClicked(){
System.out.println(";-;");
System.exit(0);
}
通过这种方式,您发布的代码可以正常工作
static
有它的用途和地方,这可能不是其中之一。不要使用 public static void createInitialWindow(){
,而是将其设为实例方法 public void createInitialWindow(){
,然后在 main
方法中创建 class 的实例并调用该方法,例如…… .
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
ReplaceComments replaceComments = new ReplaceComments();
replaceComments.createInitialWindow()
}
});
}
public void createInitialWindow(){
//...
这可能不仅可以解决这个问题,还可以解决您可能遇到的与之相关的许多其他问题
将 CustomActionListener 更改为 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 /\ */
并将方法更改为静态,:
public static void replaceButtonClicked(){
System.out.println("Replace button clicked!");
// Do something else
}
public static void cancelButtonClicked(){
System.out.println(";-;");
System.exit(0);
}
我正在制作一个程序来从任何文件中删除注释行,我刚刚完成了主 GUI 并向我的 JButtons 添加了两个动作侦听器,然后一切都崩溃了。我觉得我在某个地方犯了一个非常愚蠢的错误,但我不知道在哪里。有任何想法吗? (主要区域被注释掉)
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);
}
}
目前的代码不起作用,方法必须是静态的
public static void replaceButtonClicked(){
System.out.println("Replace button clicked!");
// Do something else
}
public static void cancelButtonClicked(){
System.out.println(";-;");
System.exit(0);
}
通过这种方式,您发布的代码可以正常工作
static
有它的用途和地方,这可能不是其中之一。不要使用 public static void createInitialWindow(){
,而是将其设为实例方法 public void createInitialWindow(){
,然后在 main
方法中创建 class 的实例并调用该方法,例如…… .
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
ReplaceComments replaceComments = new ReplaceComments();
replaceComments.createInitialWindow()
}
});
}
public void createInitialWindow(){
//...
这可能不仅可以解决这个问题,还可以解决您可能遇到的与之相关的许多其他问题
将 CustomActionListener 更改为 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 /\ */
并将方法更改为静态,:
public static void replaceButtonClicked(){
System.out.println("Replace button clicked!");
// Do something else
}
public static void cancelButtonClicked(){
System.out.println(";-;");
System.exit(0);
}