0
幫助試圖做出增加和減少按鈕,增加和減少面板上的數字只是對我沒有工作試圖讓actionPerformed和行動監聽器沒有工作可以有人給我的指導`增加和減少gui不增加不確定是否會出錯
public class IncrDecPanel extends JPanel
{
private int INITIAL_NUMBER = 50;
private JButton Incr, Dec;
private JLabel label;
private JPanel ButtonPanel;
private int number;
public IncrDecPanel()
{
number = INITIAL_NUMBER;
label = new JLabel(String.valueOf(number));
Incr = new JButton("Incr");
Dec = new JButton("Dec");
JButton IncrButton = new JButton("Increment");
IncrButton.addActionListener (new IncrListener());
JButton DecButton = new JButton("Decrement");
DecButton.addActionListener(new DecListener());
ButtonPanel = new JPanel();
ButtonPanel.setPreferredSize(new Dimension(200, 40));
ButtonPanel.setBackground(Color.red);
ButtonPanel.add(Incr);
ButtonPanel.add(Dec);
12
setPreferredSize(new Dimension(200, 80));
setBackground(Color.black);
add(label);
add(ButtonPanel);
}
private class IncrListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
label.setText (Integer.toString (++number));
}
}
private class DecListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
label.setText (Integer.toString (--number));
}
}
}`