gpt4 book ai didi

java - 组件未从 ArrayList 中显示

转载 作者:行者123 更新时间:2023-12-02 05:34:21 24 4
gpt4 key购买 nike

我有一个创建项目的类。一个项目由一个 JComponent 和一个功能组成。

public class Item {

private JComponent component;
private String functionality;

/**
* constructor
*/
public Item() {
super();
}

/**
* @param component
* @param functionality
*/
public Item(JComponent component, String functionality) {
super();
this.component = component;
this.functionality = functionality;
}

/**
* @return the component
*/
public JComponent getComponent() {
return component;
}
/**
* @param component the component to set
*/
public void setComponent(JComponent component) {
this.component = component;
}
/**
* @return the functionality
*/
public String getFunctionality() {
return functionality;
}
/**
* @param functionality the functionality to set
*/
public void setFunctionality(String functionality) {
this.functionality = functionality;
}

}

在我的 gui 中,我只需添加 JComponentsArrayList<Item> :

public class minimumExample extends JFrame {

private JButton addItem;

private JComboBox itemBox;

private String[] itemSelect = { "test1", "test2" };

private JPanel addUpperPane;

private JPanel addLowerPane;

private ArrayList<Item> displayedItems = new ArrayList<Item>();

public void createControlPane() {

addUpperPane = new JPanel();
addLowerPane = new JPanel(new GridLayout(0, 1));

addItem = new JButton("Add item");

itemBox = new JComboBox(itemSelect);

addItem.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
getContentPane().setLayout(new GridLayout(0, 1));

if(itemBox.getSelectedItem().toString().equals("test1")) {
displayedItems.add(new Item( new JButton("Test1"), "test1"));
validate();
repaint();
}

if(itemBox.getSelectedItem().toString().equals("test2")) {
displayedItems.add(new Item( new JLabel("Test2"), "test2"));
validate();
repaint();
}

}
});

for (int i = 0; i < displayedItems.size(); i++) {
addLowerPane.add(displayedItems.get(i).getComponent());
validate();
repaint();
}

addUpperPane.add(itemBox, BorderLayout.EAST);
addUpperPane.add(addItem, BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));

//put everything together

add(addUpperPane, BorderLayout.NORTH);
add(addLowerPane, BorderLayout.SOUTH);

repaint();

}

private void makeLayout() {

setTitle("Test App");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));

createControlPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);

}


/**
* starts the GUI
*/
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}

public static void main(String[] args) throws IOException {
minimumExample ex = new minimumExample();
ex.start();
}

}

我的问题是没有显示任何内容。有什么建议吗?为什么会出现这种情况?我非常感谢您的回答!

最佳答案

您刚刚在列表中添加了组件,但未在 addLowerPane 中添加该组件。将循环移动到操作监听器内以将项目添加到布局中。

示例代码:

addItem.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
...
// add item in the list.
...
for (int i = 0; i < displayedItems.size(); i++) {
addLowerPane.add(displayedItems.get(i).getComponent());
validate();
repaint();
}

}
});

了解更多 How does an ActionListener work?

关于java - 组件未从 ArrayList 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25156903/

24 4 0