Java 操作列表器后退出按钮不工作
这是代码,我不确定出了什么问题,我错过了什么吗?代码执行了,但什么也没有出现。。 如何使动作侦听器和动作执行?它根本没有意识到这个按钮 这样,当用户单击退出按钮时,会弹出一个MessageBox窗口,询问用户是否确定要退出。如果他们说“是”,则退出应用程序 底部面板:Java 操作列表器后退出按钮不工作,java,swing,actionlistener,Java,Swing,Actionlistener,这是代码,我不确定出了什么问题,我错过了什么吗?代码执行了,但什么也没有出现。。 如何使动作侦听器和动作执行?它根本没有意识到这个按钮 这样,当用户单击退出按钮时,会弹出一个MessageBox窗口,询问用户是否确定要退出。如果他们说“是”,则退出应用程序 底部面板: import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.*; import java.awt.*; import java
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class StudentGUI extends JFrame implements ActionListener {
public StudentGUI() {
super("StudentGUI Frame");
//TopPanel
TopPanel tp;
tp=new TopPanel();
Dimension d = new Dimension(800,600);
tp.setPreferredSize(d);
this.add (tp, BorderLayout.NORTH);
tp.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
setBackground(Color.PINK);
tp.setVisible(true);
//TopPanel End
//BottomPanel
BottomPanel bp;
bp=new BottomPanel();
this.add (bp, BorderLayout.SOUTH);
tp.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
//BottomPanel End
//MiddlePanel
MiddlePanel mp;
mp=new MiddlePanel();
this.add (mp, BorderLayout.CENTER);
mp.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
//MiddlePanel End
this.setVisible(true);
}
exitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
int selectedOption = JOptionPane.showConfirmDialog(null,
"Do you want to close the window?",
"Choose",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
System.exit(1);
}
}
});
public static void main(String[] args) {
new StudentGUI();
}
}
您的
ActionListener
基本上什么都不做,请尝试将JOptionPane
对话框移动到actionPerform
方法中
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
public class BottomPanel extends JPanel {
public String findbtn="";
public String insertBtn="";
public String updateBtn="";
public String deleteBtn="";
public String exitBtn="";
public BottomPanel() {
JButton findbtn;
findbtn=new JButton("Find");
add(findbtn);
JButton insertBtn;
insertBtn=new JButton("Insert");
add(insertBtn);
JButton updateBtn;
updateBtn=new JButton("Update");
add(updateBtn);
JButton deleteBtn;
deleteBtn=new JButton("Delete");
add(deleteBtn);
JButton exitBtn;
exitBtn=new JButton("Exit");
add(exitBtn);
exitBtn.addActionListener(this);
}
}
您的
ActionListener
基本上什么都不做,请尝试将JOptionPane
对话框移动到actionPerform
方法中
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
public class BottomPanel extends JPanel {
public String findbtn="";
public String insertBtn="";
public String updateBtn="";
public String deleteBtn="";
public String exitBtn="";
public BottomPanel() {
JButton findbtn;
findbtn=new JButton("Find");
add(findbtn);
JButton insertBtn;
insertBtn=new JButton("Insert");
add(insertBtn);
JButton updateBtn;
updateBtn=new JButton("Update");
add(updateBtn);
JButton deleteBtn;
deleteBtn=new JButton("Delete");
add(deleteBtn);
JButton exitBtn;
exitBtn=new JButton("Exit");
add(exitBtn);
exitBtn.addActionListener(this);
}
}
检查此示例代码
exitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedOption = JOptionPane.showConfirmDialog(null,
"Do you want to close the window?",
"Choose",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
System.exit(1);
}
}
});
检查此示例代码
exitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedOption = JOptionPane.showConfirmDialog(null,
"Do you want to close the window?",
"Choose",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
System.exit(1);
}
}
});
你对这个项目做了什么,你期望会发生什么,以及会发生什么?你可以缩进你的代码,让它可读(对我们和你来说)。哦,好的,对不起,我想让按钮这样做:这样当用户点击退出按钮时,会弹出一个MessageBox窗口,询问用户是否确定要退出。如果他们说“是”,然后退出应用程序。这并不能回答我的问题。代码仍然没有缩进。你可能想包括bottompani,我也希望它达到我点击退出按钮的程度,它说,你确定要退出吗?我单击“是”,它退出,我单击“否”,它不退出。你对这个程序做了什么,你期望发生什么,以及会发生什么?你可以缩进你的代码,让它可读(对我们和你来说)。哦,好的,对不起,我想让按钮这样做:这样当用户点击退出按钮时,会弹出一个MessageBox窗口,询问用户是否确定要退出。如果他们说“是”,然后退出应用程序。这并不能回答我的问题。代码仍然没有缩进。你可能想包括bottompani,我也希望它达到我点击退出按钮的程度,它说,你确定要退出吗?我点击是,它退出,我点击否,它没有。我改变了它,程序现在可以运行了,但是退出按钮没有任何作用,我做错了什么?是否需要添加bp.exitBtn.ActionListener?如果是这样的话,我试过了,但没有成功。编辑后的版本现在是upI,我已经在您前面的问题中回答了这个问题,您不必在
StudentGUI
中通过说bp.exitBtn.ActionListener
来添加它,只需在BottomPanel
类中执行exitBtn.ActionListener即可。只需在BottomPanel中添加它并获得一个“实际参数底部面板无法转换为操作侦听器“不,您必须在匿名ActionListener
类中实现actionPerformed
方法,请参见我的答案。@user3741806:在这个答案中所说的,您需要在底部面板
类的构造函数中实现,而不是在外部,通过替换exitBtn.addActionListener(this)
。你确定你对这门语言的基础知识了解得那么好吗?我修改了它,程序现在可以运行了,但是退出按钮没用,我做错了什么?是否需要添加bp.exitBtn.ActionListener?如果是这样的话,我试过了,但没有成功。编辑后的版本现在是upI,我已经在您前面的问题中回答了这个问题,您不必在StudentGUI
中通过说bp.exitBtn.ActionListener
来添加它,只需在BottomPanel
类中执行exitBtn.ActionListener即可。只需在BottomPanel中添加它并获得一个“实际参数底部面板无法转换为操作侦听器“不,您必须在匿名ActionListener
类中实现actionPerformed
方法,请参见我的答案。@user3741806:在这个答案中所说的,您需要在底部面板
类的构造函数中实现,而不是在外部,通过替换exitBtn.addActionListener(this)
。你确定你对这门语言的基础知识了解得那么透彻吗?虽然我并不是投反对票的人,但可能是因为缩进不正确,没有使用Swing的事件调度程序线程-EDT
进行创建,或者不必要地转向继承,而只有编写可以完成任务。但在没有任何理由的情况下,我只能否决投票。虽然我并不是投反对票的那个人,但可能是因为缩进不当,没有使用Swing的事件调度程序线程-EDT
进行创建,或者不必要地转向继承,而只有合成可以完成任务。但在没有任何理由的情况下,我只能否决投票。但是你仍然需要在你的答案中寻找更多。