我正在使用GUI,并且我的所有内容都正确显示,但我在按按钮时无法更改变量的值。该变量称为passover,它是一个私有的int,它在0参数构造函数中初始化为1997的值,并且我有3个按钮需要在按下时更改passover的值。对于逾越节在使用ActionListener更改变量时遇到问题
代码:
panel.setBorder(
BorderFactory.createEmptyBorder(0, 0, 0, 0));
panel.setLayout(new BorderLayout(0, 0));
JPanel topPanel = getPanel();
topPanel.setLayout(new GridBagLayout());
topPanel.setBackground(new Color(173, 216, 230));
JLabel moveLabel = getLabel("Move Data");
moveLabel.setFont(new Font("Serif", Font.PLAIN, 20));
addComp(topPanel, moveLabel, 0, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHEAST);
JLabel passoverLabel = getLabel(" Passover : " + passover);
passoverLabel.setFont(new Font("Serif", Font.PLAIN, 20));
addComp(topPanel, passoverLabel, 1, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
代码的按钮:
JPanel bottomRightPanel = getPanel();
bottomRightPanel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
bottomRightPanel.setLayout(new GridBagLayout());
bottomRightPanel.setBackground(new Color(255, 105, 180));
JPanel passoverButtonPanel = getPanel();
passoverButtonPanel.setBackground(new Color(255, 105, 180));
passoverButtonPanel.setLayout(new BoxLayout(passoverButtonPanel, BoxLayout.Y_AXIS));
nextPassoverButton = new JButton("Next Passover");
goToPassoverButton = new JButton("Go To Passover: ");
previousPassoverButton = new JButton("Previous Passover");
JLabel filler = getLabel(" ");
goToField = new JTextField(6);
goToField.setPreferredSize(new Dimension(5, 30));
theHandler handler = new theHandler();
nextPassoverButton.addActionListener(handler);
goToPassoverButton.addActionListener(handler);
previousPassoverButton.addActionListener(handler);
goToField.addActionListener(handler);
我想逾越的值被按压nextPassoverButton当被升高一个,降低了一个被按下previousPassoverButton时,和当goToPassoverButton被按下时,改变为用户输入到goToField的值。
我的ActionListener类的样子:
public class theHandler extends MyDataPalV2 implements ActionListener{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == goToField)
{
this.passover = Integer.parseInt(String.format("%s", event.getActionCommand()));
}
if (event.getSource() == nextPassoverButton)
{
this.passover++;
}
if (event.getSource() == previousPassoverButton)
{
this.passover--;
}
}
}
的错误是一个私人访问错误,所以我想我会需要使用一个getter和setter,但我不知道我该怎么做。也许有完全不同的方式来做到这一点?
这是我第一次在这里发帖很抱歉,如果我在我的文章中发生任何错误。
为了更快地获得更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 –