gpt4 book ai didi

java - Java动态生成按钮

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:01:16 24 4
gpt4 key购买 nike

我正在尝试动态生成一个表单。基本上,我想加载要购买的商品列表,并为每个商品生成一个按钮。我可以确认按钮是用调试器生成的,但它们没有显示。这是在 JPanel 的子类中:

private void generate() {
JButton b = new JButton("height test");
int btnHeight = b.getPreferredSize().height;
int pnlHeight = this.getPreferredSize().height;
int numButtons = pnlHeight / btnHeight;

setLayout(new GridLayout(numButtons, 1));

Iterator<Drink> it = DrinkMenu.iterator();

for (int i = 0; i <= numButtons; ++i) {
if (!it.hasNext()) {
break;
}
final Drink dr = it.next();
b = new DrinkButton(dr);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
order.addDrink(dr);
}});
add(b);
}
revalidate();
}

DrinkBut​​tonJButton 的子类。有什么想法吗?

最佳答案

关于 validate() revalidate() 和 repaint() 的例子,看起来像正确输出到 GUI 所需要的,由一些 LayourManagers 放置

编辑:正如 trashgod 注意到的,我添加了 Schedule a job for the EDT

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ValidateRevalidateRepaint {

private JPanel panel;
private GridBagConstraints gbc;
private boolean validate, revalidate, repaint;

public ValidateRevalidateRepaint() {
validate = revalidate = repaint = false;
panel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 20, 0, 20);
panel.add(getFiller(), gbc);
JFrame f = new JFrame();
f.setJMenuBar(getMenuBar());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(panel);
f.getContentPane().add(getRadioPanel(), "East");
f.getContentPane().add(getCheckBoxPanel(), "South");
f.setSize(400, 200);
f.setLocation(200, 200);
f.setVisible(true);
}

private JMenuBar getMenuBar() {
JMenu menu = new JMenu("change");
ActionListener l = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem) e.getSource();
int n = Integer.parseInt(item.getActionCommand());
makeChange(n);
}
};
for (int j = 1; j < 5; j++) {
String s = String.valueOf(j) + " component";
if (j > 1) {
s += "s";
}
JMenuItem item = new JMenuItem(s);
item.setActionCommand(String.valueOf(j));
item.addActionListener(l);
menu.add(item);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
return menuBar;
}

private JPanel getRadioPanel() {
JPanel panel1 = new JPanel(new GridBagLayout());
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.insets = new Insets(2, 2, 2, 2);
gbc1.weighty = 1.0;
gbc1.gridwidth = GridBagConstraints.REMAINDER;
ButtonGroup group = new ButtonGroup();
ActionListener l = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JRadioButton radio = (JRadioButton) e.getSource();
int n = Integer.parseInt(radio.getActionCommand());
makeChange(n);
}
};
for (int j = 0; j < 4; j++) {
String s = String.valueOf(j + 1);
JRadioButton radio = new JRadioButton(s);
radio.setActionCommand(s);
radio.addActionListener(l);
group.add(radio);
panel1.add(radio, gbc1);
}
return panel1;
}

private JPanel getCheckBoxPanel() {
final String[] operations = {"validate", "revalidate", "repaint"};
ActionListener l = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JCheckBox checkBox = (JCheckBox) e.getSource();
String ac = checkBox.getActionCommand();
boolean state = checkBox.isSelected();
if (ac.equals("validate")) {
validate = state;
}
if (ac.equals("revalidate")) {
revalidate = state;
}
if (ac.equals("repaint")) {
repaint = state;
}
}
};
JPanel panel2 = new JPanel();
for (int j = 0; j < operations.length; j++) {
JCheckBox check = new JCheckBox(operations[j]);
check.setActionCommand(operations[j]);
check.addActionListener(l);
panel2.add(check);
}
return panel2;
}

private void makeChange(int number) {
panel.removeAll();
for (int j = 0; j < number; j++) {
panel.add(getFiller(), gbc);
}
if (validate) {
panel.validate();
}
if (revalidate) {
panel.revalidate();
}
if (repaint) {
panel.repaint();
}
}

private JPanel getFiller() {
JPanel panel3 = new JPanel();
panel3.setBackground(Color.red);
panel3.setPreferredSize(new Dimension(40, 40));
return panel3;
}

public static void main(String[] args) {//added Schedule a job for the EDT
javax.swing.SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
ValidateRevalidateRepaint rVR = new ValidateRevalidateRepaint();
}
});
}
}

关于java - Java动态生成按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6390240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com