我想添加一個重置按鈕,我已經把關閉程序按鈕,但我不知道該怎麼做。我只想讓它在按下確認按鈕之前將當前選項卡恢復到原始狀態。遇到麻煩實施重置按鈕
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();
}
});
}
}
我沒有看到你已經做了什麼嘗試尚未實現這一點。你有嘗試過什麼嗎?你究竟在哪裏卡住? –
我嘗試了一堆亂七八糟的東西,像removeAll();然後刷新,然後我試着調用繪製窗口的原始方法來查看它是否會再次繪製它,但我無法使其工作。我不知道如果我應該把破碎的代碼看到我在哪裏搞亂或者代碼可以運行以查看我的意思 – Mithon
你需要做的是準確地記下你的程序是什麼意思「重啓」。需要做什麼精確的具體邏輯步驟。然後,您需要嘗試一次一個地執行代碼中的每個步驟。 –