gpt4 book ai didi

java - 从 JPanel 获取选定的组件

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

我通过ArrayList<Items>动态地将项目添加到我的JPanel中。基本上 items 对象看起来像这样:

public class Item {

private JComponent component;
private String functionality;

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

public JComponent getComponent() {
return component;
}

public void setComponent(JComponent component) {
this.component = component;
}

public String getFunctionality() {
return functionality;
}

public void setFunctionality(String functionality) {
this.functionality = functionality;
}
}

在这里,我动态添加按钮:(如果需要,请尝试该示例)

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>();

private JButton upButton;

private JButton downButton;

private JButton deleteButton;

public void createControlPane() {

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

addItem = new JButton("Add item");
upButton = new JButton("Up");
downButton = new JButton("Down");
deleteButton = new JButton("Delete");

itemBox = new JComboBox(itemSelect);

addItem.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

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

if(itemBox.getSelectedItem().toString().equals("test2")) {
displayedItems.add(new Item( new LabelComp().label(), "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 JLabel(" | "), BorderLayout.WEST);
addUpperPane.add(upButton, BorderLayout.WEST);
addUpperPane.add(downButton, BorderLayout.WEST);
addUpperPane.add(deleteButton, 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();
}



public class ButtonComp extends Component {

public JPanel butt() {
JPanel panel = new JPanel();

JButton button = new JButton("Test1");
JCheckBox check = new JCheckBox();

panel.add(button);
panel.add(check);

return panel;
}
}

public class LabelComp extends Component {

public JPanel label() {
JPanel panel = new JPanel();

JLabel label = new JLabel("Test2");
JCheckBox check = new JCheckBox();

panel.add(label);
panel.add(check);

return panel;
}
}
}

该程序基本上如下所示:

enter image description here

我的问题是按钮 Up, Down and Delete不起作用,因为我不知道如何从 Pane 中获取选定的元素,以将其从所有组件所在的列表中删除。有关如何使其工作的任何建议?

非常感谢您的回答!

更新

我更改了我的代码,您的规范@cghislai,但它不起作用。自己尝试一下:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public class minimumExample extends JFrame {

private JButton addItem;

private JComboBox itemBox;

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

private JPanel addUpperPane;

private JPanel addLowerPane;

private List<CheckableItem> displayedItems = new ArrayList<CheckableItem>();

private JButton upButton;

private JButton downButton;

private JButton deleteButton;

public void createControlPane() {

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

addItem = new JButton("Add item");
upButton = new JButton("Up");
downButton = new JButton("Down");
deleteButton = new JButton("Delete");

itemBox = new JComboBox(itemSelect);

addItem.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

if(itemBox.getSelectedItem().toString().equals("test1")) {
ButtonComp butt = new ButtonComp();
butt.init();
displayedItems.add(butt);
validate();
repaint();
}

if(itemBox.getSelectedItem().toString().equals("test2")) {
// displayedItems.add(new CopyOfItem( new LabelComp(), "test2"));
validate();
repaint();
}


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


deleteButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {

Iterator<CheckableItem> it = displayedItems.iterator();
while (it.hasNext()) {
CheckableItem next = it.next();
if (next.isSelected()) {
addLowerPane.remove(next.getComponent());
it.remove();
continue;
}
}

}
});

addUpperPane.add(itemBox, BorderLayout.EAST);
addUpperPane.add(addItem, BorderLayout.WEST);
addUpperPane.add(new JLabel(" | "), BorderLayout.WEST);
addUpperPane.add(upButton, BorderLayout.WEST);
addUpperPane.add(downButton, BorderLayout.WEST);
addUpperPane.add(deleteButton, 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();
}




public abstract class CheckableItem {

protected JCheckBox check;

public boolean isSelected() {
return check.isSelected();
}

public abstract Component getComponent();

}

public class ButtonComp extends CheckableItem {
JPanel panel = new JPanel();

public void init() {
JButton button = new JButton("Test1");
check = new JCheckBox();
panel.add(button);
panel.add(check);
}

@Override
public Component getComponent() {
return panel;
}

}

public class LabelComp extends JPanel {

public void label() {
// JPanel panel = new JPanel();

JLabel label = new JLabel("Test2");
JCheckBox check = new JCheckBox();

add(label);
add(check);

}
}
}

最佳答案

您需要遍历您的所有项目,检查项目复选框是否被选中,如果是这种情况,请从面板中删除您的项目。我将创建一个抽象的 CheckableItem 类,其中包含 JCheckbox 和组件的 getter。然后,对于每个项目,如果选中该复选框,则从父级中删除该组件。

public abstract class CheckableItem {
protected JCheckbox checkbox;
public boolean isSelected() {
return checkbox.isSelected();
}
public abstract Component getComponent();
}
public class ButtonComp extends CheckableItem {
private Panel panel;
public void init() {
checkbox = new JCheckbox;
panel = new JPanel();
panel.add(new JButton());
panel.add(checkbox);
}
public Component getComponent() {
return panel;
}
}

然后跟踪您的元素:

 private List<CheckableItem> items = new ArrayList<>();
// ...
ButtonComp comp = new ButtonComp();
comp.init();
items.add(comp);

然后删除所有选中的:

Iterator<CheckbleItem> it = items.iterator();
while (it.hasNext()) {
CheckableItem next = it.next();
if (next.isSelected()) {
mainPanel.remove(next.getComponent());
it.remove();
continue;
}
}

关于java - 从 JPanel 获取选定的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25159712/

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