[英]How to increase value of a button after button number has reached certain value?
我正在尝试使用 java swing 创建一组按钮。 我想用数字 5 增加按钮值,直到它们达到 60。之后它们应该随着数字 20 增加。在 100 之后,它们应该随着 50 增加。我的 for 循环构造有什么问题?
ValueButton btn = new ValueButton((x + 1) * 5.0);
for (int s = 0; s < btn.getValue(); s++) {
if (btn.getValue() > 60.0) {
btn.setValue((x + 1) * 20.0);
System.out.println("value: " + btn.getValue());
if (btn.getValue() > 100.0) {
btn.setValue((x + 1) * 50.0);
System.out.println("value: " + btn.getValue());
}
}
}
添加到界面的按钮:
myButtons.add(btn, new GridBagConstraints(x % 4, y, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
}
根据 Anand Kumar 的回答编辑代码:
pnlValueButtons.setLayout(new GridBagLayout());
for (int x = 0, y = 0; x < 16; x++) {
if (x % 4 == 0) {
y++;
}
double buttonValue = 5.0;
while (buttonValue <= 200) {
ValueButton btn = new ValueButton(buttonValue);
if (buttonValue < 60) {
buttonValue += 5.0;
} else if (buttonValue < 100) {
buttonValue += 20.0;
} else {
buttonValue += 50;
}
pnlValueButtons.add(btn, new GridBagConstraints(x % 4, y, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
}
}
我做的有点不同。
由于我们知道JButtons
所需的值,因此我只需将 16 个值放入一个 int 数组中。 根据 OP 迄今为止告诉我们的内容,无需计算它们。
这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class IncreasingJButtonExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new IncreasingJButtonExample());
}
@Override
public void run() {
JFrame frame = new JFrame("Increasing JButtons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridLayout(0, 4, 5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(24f);
int[] values = { 5, 10, 15, 20, 40, 60, 80, 100, 150, 200,
250, 300, 350, 400, 450, 500 };
for (int i = 0; i < values.length; i++) {
JButton button = new JButton(Integer.toString(values[i]));
button.setFont(font);
button.setPreferredSize(new Dimension(100, 100));
panel.add(button);
}
return panel;
}
}
pnlValueButtons.setLayout(new GridBagLayout());
double buttonValue = 5.0;
for (int x = 0, y = 0; x < 16; x++) {
if (x % 4 == 0) {
y++;
}
ValueButton btn = new ValueButton(buttonValue);
if (buttonValue < 60) {
buttonValue += 5.0;
} else if (buttonValue < 100) {
buttonValue += 20.0;
} else {
buttonValue += 50;
}
pnlValueButtons.add(btn, new GridBagConstraints(x % 4, y, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.