Java 执行重置按钮时遇到问题

Java 执行重置按钮时遇到问题,java,swing,user-interface,jpanel,reset,Java,Swing,User Interface,Jpanel,Reset,我想添加一个复位按钮,我已经把关闭程序按钮,但我不知道如何做到这一点。我只想让它将当前选项卡恢复到按下确认按钮之前的原始状态 public class A5 extends JPanel { public A5() { super(new GridLayout(1, 1)); //creating tabbed pane JTabbedPane tabbedPane = new JTabbedPane(); //calling question 1 tab me

我想添加一个复位按钮,我已经把关闭程序按钮,但我不知道如何做到这一点。我只想让它将当前选项卡恢复到按下确认按钮之前的原始状态

public class A5 extends JPanel {

public A5() {
    super(new GridLayout(1, 1));
    //creating tabbed pane
    JTabbedPane tabbedPane = new JTabbedPane();
    //calling question 1 tab method
    JComponent q1 = makeq1panel("Question 1");
    q1.setPreferredSize(new Dimension(420, 150));
    tabbedPane.addTab("Question 1", q1);
    add(tabbedPane);    
}


private JComponent makeq1panel(String question) {
    //making panel and title for it
    final JPanel panel = new JPanel(false);
    panel.setLayout(new GridLayout(3, 1));
    JLabel title = new JLabel("Enter a number and press confirm");
    title.setHorizontalAlignment(JLabel.CENTER);
    panel.add(title);
    //spinner for input
    int spinnerstart = 1;
    SpinnerModel number = new SpinnerNumberModel(spinnerstart, spinnerstart - 1, spinnerstart + 50, 1);
    final JSpinner spin = addSpinner(panel,number);
    //confirm button
    JButton btconfirm = new JButton("Confirm");
    btconfirm.addActionListener( new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            String output = null;
            //checking if value is correct
            int inputINT = (Integer)spin.getValue();
            if (inputINT <10 && inputINT >1) 
                output = "True";
            else output = "False";
            //Question output
            JLabel d2 = new JLabel("Output: " + output);
            java.awt.Font subfont = new java.awt.Font("Dialog",Font.BOLD,14);
            d2.setFont(subfont);
            d2.setHorizontalAlignment(JLabel.CENTER);
            panel.removeAll();
            panel.add(d2);
            //reset button
            JButton btclose = new JButton("Close Program");
            btclose.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }
            });
            //adding close button and refreshing
            panel.add(btclose);
            revalidate();
            repaint();
        }
    });
    panel.add(btconfirm);
    return panel;
}



private static void makewindow() {
    //Create and set up the window.
    JFrame frame = new JFrame("Assignment 5");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Add content to the window.
    frame.add(new A5(), BorderLayout.CENTER);
    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

static protected JSpinner addSpinner(Container c, SpinnerModel model) {
    JSpinner spinner = new JSpinner(model);
    c.add(spinner);

    return spinner;
}

public static void main(String[] args) {
    //run it
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            makewindow();
        }
    });
}
}
公共类A5扩展了JPanel{
公共A5(){
超级(新网格布局(1,1));
//创建选项卡式窗格
JTabbedPane tabbedPane=新的JTabbedPane();
//调用问题1选项卡方法
JComponent q1=makeq1panel(“问题1”);
q1.设置首选尺寸(新尺寸(420150));
tabbedPane.addTab(“问题1”,q1);
添加(选项卡窗格);
}
专用JComponent makeq1panel(字符串问题){
//制作面板和标题
最终JPanel面板=新JPanel(假);
panel.setLayout(新网格布局(3,1));
JLabel title=新的JLabel(“输入一个数字并按确认”);
标题.设置水平对齐(JLabel.中心);
小组.添加(标题);
//输入微调器
int喷丝头开始=1;
喷丝头型号=新喷丝头型号(喷丝头启动,喷丝头启动-1,喷丝头启动+50,1);
最终JSpinner spin=addSpinner(面板,编号);
//确认按钮
JButton btconfirm=新JButton(“Confirm”);
btconfirm.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
字符串输出=null;
//检查值是否正确
int inputINT=(整数)spin.getValue();
如果(输入1)
输出=“真”;
else输出=“False”;
//问题输出
JLabel d2=新的JLabel(“输出:”+输出);
java.awt.Font子字体=新建java.awt.Font(“对话框”,Font.BOLD,14);
d2.设置字体(子字体);
d2.设置水平对齐(JLabel.中心);
panel.removeAll();
增补(d2);
//复位按钮
JButton btclose=新JButton(“关闭程序”);
addActionListener(新的ActionListener()
{
已执行的公共无效操作(操作事件e)
{
系统出口(0);
}
});
//添加关闭按钮和刷新
面板。添加(btclose);
重新验证();
重新油漆();
}
});
面板。添加(btconfirm);
返回面板;
}
私有静态void makewindow(){
//创建并设置窗口。
JFrame=新JFrame(“分配5”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//向窗口添加内容。
frame.add(新A5(),BorderLayout.CENTER);
//显示窗口。
frame.pack();
frame.setVisible(true);
}
静态保护JSpinner addSpinner(容器c,SpinnerModel){
JSpinner spinner=新JSpinner(型号);
c、 添加(微调器);
返回旋转器;
}
公共静态void main(字符串[]args){
//运行它
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
makewindow();
}
});
}
}

执行
系统后退出(0)语句您的程序和JVM都将被终止,因此在此语句之后操作将不会继续。

我看不出您在何处尝试实现此操作。你试过什么吗?你到底被困在哪里了?我在按钮上尝试了一堆乱七八糟的东西,比如removeAll();然后刷新,然后我尝试调用原始的方法来绘制窗口,看看它是否会再次绘制它,但我无法让它工作。我不确定我是否应该把坏代码放在哪里,看看我在哪里乱搞,或者运行它的代码,看看我的意思,你需要做的是写下你的程序“重置”的确切含义。需要执行哪些精确的具体逻辑步骤。然后你需要试着在代码中实现这些步骤中的每一步,一次一个。这个答案与原始问题有什么关系?阅读reset button的注释。这是一个注释,但可能是一个尚未实现的按钮。他可能需要执行重置和关闭按钮,但从这个问题和其他问题来看,我不认为原始海报对他在做什么有任何线索。他最需要的是阅读Java书籍,我之所以使用它,是因为我找不到实现重置按钮的方法,有点像占位符。对不起,我不清楚