警告:初学者编程试图理解她的作业!!会问愚蠢的问题!
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();
}
}
这是我的主要框架代码。
这是我的面板代码,其中包含退出按钮。
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;
}
}
我一遍又一遍地试着让退出按钮工作,一半时间程序工作但是当我按下退出按钮时没有任何反应。 我想要让它达到我点击退出按钮的程度,它询问我是否确定要退出,如果我不推它不会,如果我推它是的确如此。
答案 0 :(得分:1)
我清理了所有面板,只留下底部面板用于演示。 在BottomPanel中,我将所有公共字符串修复为公共JButton。 正如帕特指出的那样,exitbtn应该属于某种东西,我让它属于BottomPanel。您实际上不需要在JFrame中实现接口ActionListener。这是StudentGUI的代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class StudentGUI extends JFrame {
private static final long serialVersionUID = 1L;
public StudentGUI()
{
super("StudentGUI Frame");
Dimension d = new Dimension(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
setBackground(Color.PINK);
this.setVisible(true);
//BottomPanel
BottomPanel bp;
bp=new BottomPanel();
this.add (bp, BorderLayout.SOUTH);
bp.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);
return;
}
});
}
public static void main(String[] args)
{
new StudentGUI();
}
}
以下是BottomPanel的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
public class BottomPanel extends JPanel {
public JButton findbtn;
public JButton insertBtn;
public JButton updateBtn;
public JButton deleteBtn;
public JButton exitBtn;
public BottomPanel() {
findbtn=new JButton("Find");
add(findbtn);
insertBtn=new JButton("Insert");
add(insertBtn);
updateBtn=new JButton("Update");
add(updateBtn);
deleteBtn=new JButton("Delete");
add(deleteBtn);
exitBtn=new JButton("Exit");
add(exitBtn);
}
}
这绝对不是最佳做法,但至少它是有效的。
答案 1 :(得分:0)
我在评论中解释了我的所有更改,只要您的TopPanel和MiddlePanel类没有问题,这应该有效。我在测试它时只是注释掉了那些线。
将第一堂课改为
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;
// removed implements ActionListener
public class StudentGUI extends JFrame {
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);
/* moved action listener within method,
* used bp.exitBtn instead of exitBtn
*/
bp.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);
}
}
}); // fixed braces, the closing paren should go here
}
public static void main(String[] args) {
new StudentGUI();
}
}
你的第二堂课应该是
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
public class BottomPanel extends JPanel {
/* instead of strings, added references to JButtons
* so that they can be accessed from other classes
*/
public JButton findbtn;
public JButton insertBtn;
public JButton updateBtn;
public JButton deleteBtn;
public JButton exitBtn;
public BottomPanel() {
findbtn=new JButton("Find");
add(findbtn);
insertBtn=new JButton("Insert");
add(insertBtn);
updateBtn=new JButton("Update");
add(updateBtn);
deleteBtn=new JButton("Delete");
add(deleteBtn);
exitBtn=new JButton("Exit");
add(exitBtn);
}
}
答案 2 :(得分:-1)
我正在写我的答案,但我被打败了。我建议你使用IDE作为IDE。你的错误不是编程错误。他们是基本的酒吧。 (我们所有人都去过:-D)
在这里下载eclipse - > https://eclipse.org/downloads/packages/eclipse-standard-432/keplersr2
红色下划线的东西是错误的。如果它以黄色加下划线通常表示它没有被使用。这有望减少您寻找缺少的berracts的时间。
享受并继续学习:)