2013-09-28 91 views
-1

我想添加一个重置按钮,我已经把关闭程序按钮,但我不知道该怎么做。我只想让它在按下确认按钮之前将当前选项卡恢复到原始状态。遇到麻烦实施重置按钮

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(); 
     } 
    }); 
} 
} 
+2

我没有看到你已经做了什么尝试尚未实现这一点。你有尝试过什么吗?你究竟在哪里卡住? –

+0

我尝试了一堆乱七八糟的东西,像removeAll();然后刷新,然后我试着调用绘制窗口的原始方法来查看它是否会再次绘制它,但我无法使其工作。我不知道如果我应该把破碎的代码看到我在哪里搞乱或者代码可以运行以查看我的意思 – Mithon

+0

你需要做的是准确地记下你的程序是什么意思“重启”。需要做什么精确的具体逻辑步骤。然后,您需要尝试一次一个地执行代码中的每个步骤。 –

回答

0

执行System.exit(0);声明你的程序将被终止以及JVM,所以操作不会在此声明后继续后。

+0

这个答案与原始问题有关系吗? –

+0

阅读重置按钮的评论。 – Masudul

+0

这是一条评论,但可能是一个尚未实现的按钮。他可能必须实现重置和关闭按钮,但从这个和他的其他问题来看,我不认为原始海报对他在做什么有任何线索。他确实需要阅读一本Java书籍。 –