2013-09-28 89 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書籍。 –