【发布时间】:2016-06-23 16:16:00
【问题描述】:
我正在使用 GUI,并且所有内容都正确显示,但是当我按下按钮时,我无法更改变量的值。该变量称为 passover,它是一个私有 int,在 0 参数构造函数中初始化为 1997 的值,我有 3 个按钮需要在按下时更改 passover 的值。
逾越节代码:
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,但我不知道该怎么做。也许有完全不同的方法可以做到这一点?
这是我第一次在这里发帖,如果我的帖子有任何错误,请见谅。
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。