我正在制作程序以从任何文件中删除注释行,我刚刚完成了主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);
}
}
答案 0 :(得分:1)
static
有它的用途和它的位置,这可能不是其中之一。不要使用public static void createInitialWindow(){
,而是将其设为实例方法public void createInitialWindow(){
,然后在main
方法中创建类的实例并调用方法,例如......
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(){
//...
这不仅可以解决这个问题,而且可能会遇到许多与之相关的问题
答案 1 :(得分:0)
代码实际上不起作用,方法必须是静态的
public static void replaceButtonClicked(){
System.out.println("Replace button clicked!");
// Do something else
}
public static void cancelButtonClicked(){
System.out.println(";-;");
System.exit(0);
}
以这种方式发布的代码正在运行
答案 2 :(得分:0)
通过ActionListener
更改CustomActionListener /* \/ 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 /\ */
并将方法更改为static,:
public static void replaceButtonClicked(){
System.out.println("Replace button clicked!");
// Do something else
}
public static void cancelButtonClicked(){
System.out.println(";-;");
System.exit(0);
}