我希望为未来的项目测试代码(下面(。
我显示所有控制面板。
我选择了几个contPanel对象。
我只删除带有jButton 的选定控制面板
删除选定的contPanel后,它将其他contPanel放置在框架中。
但所有内容都没有显示,我收到错误消息:
已断开与目标VM的连接,地址:"127.0.0.1:60359",传输:"socket">
进程结束,退出代码为130(被信号2:SIGINT中断(
我哪里错了?我能做什么?intellij的更新不会改变任何事情。
我:
1(
ubuntu 20.04
- intellij
IntelliJ IDEA 2020.3(社区版(构建号IC-203.5981.155,构建于2020年11月30日运行时版本:11.0.9+11-b1145.21 amd64虚拟机:JetBrains s.r.o.的OpenJDK 64位服务器虚拟机。Linux 5.4.0-56通用GC:ParNew,ConcurrentMarkSweep内存:1981M核心:8非捆绑插件:com.jetbrains.edu当前桌面:ubuntu:GNOME
- java
java-1.8.0-openjdk-amd64
package suggests.deleteCorrectItem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Main extends JFrame{
public ArrayList<Integer> alInteger = new ArrayList<Integer>();
public ArrayList<ContPanel> alContPanel = new ArrayList<ContPanel>();
public JPanel mainPanel = new JPanel();
public int widthFrame = 700;
public int heightFrame = 120;
public Dimension dimensionFrame = new Dimension(widthFrame, heightFrame);
public JScrollPane jScrollPane;
public JButton jButtonDeleteAll = new JButton();
public DeleteListener deleteListener = new DeleteListener();
public ContPanel contPanel;
public static void main(String[] args){
Main myMain = new Main();
}
public Main(){
frameInitialization();
setBackground(Color.BLUE);
mainPanel.setBackground(Color.red);
initObjects();
buildLayout();
setVisible(true);
}
public void frameInitialization(){
this.setTitle("fenêtre de test");
this.setSize(dimensionFrame);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addScrollPaneFrame();
}
public void addScrollPaneFrame(){
jScrollPane = new JScrollPane(mainPanel);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(jScrollPane);
}
public void initObjects(){
for ( int i = 0; i< 15;i++){
alInteger.add(i);
contPanel = new ContPanel(alInteger.get(i));
contPanel.setBackground(Color.green);
alContPanel.add(contPanel);
}
jButtonDeleteAll.addActionListener(deleteListener);
}
public void buildLayout(){
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
jScrollPane.add(mainPanel);
fillOfContPanel();
setContentPane(jScrollPane);
}
public void fillOfContPanel(){
for (int i = 0; i< alInteger.size(); i++){
mainPanel.add(alContPanel.get(i));
}
}
class ContPanel extends JPanel{
public Integer integ;
public JLabel jLabel = new JLabel();
public JCheckBox jCheckBox = new JCheckBox();
public ContPanel(Integer integ){
setBackground(Color.BLACK);
this.integ = integ;
jLabel.setText(this.integ.toString());
add(jLabel);
add(jCheckBox);
}
}
class DeleteListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
mainPanel = new JPanel();
for(int i = alContPanel.size()-1; i>= 0 ; i--){
if(alContPanel.get(i).jCheckBox.isSelected()){
alInteger.remove(i);
alContPanel.remove(i);
}
}
}
}
}
我发现了问题。项目订单。我检索到了鲜艳的
如果它能帮助
我改变了很多事情。。。
package suggests.deleteCorrectItem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Main extends JFrame{
/**
This little project shows the display of elements from an arrayList
with a dedicated checkbox for each of them to select them. The user the items to remove and
clicks the jButton.
The selected items disappear.
*/
public ArrayList<Integer> alInteger = new ArrayList<Integer>();
public ArrayList<ContPanel> alContPanel = new ArrayList<ContPanel>();
public JPanel mainPanel = new JPanel();
public int widthFrame = 700;
public int heightFrame = 600;
public Dimension dimensionFrame = new Dimension(widthFrame, heightFrame);
public JScrollPane jScrollPane;
public JButton jButtonDeleteAll = new JButton("Delete all selected items");
public DeleteListener deleteListener = new DeleteListener();
public ContPanel contPanel;
public static void main(String[] args){
Main myMain = new Main();
}
public Main(){
frameInitialization();
initObjects();
buildLayout();
setVisible(true);
}
public void frameInitialization(){
this.setTitle("fenêtre de test");
this.setSize(dimensionFrame);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addScrollPaneFrame(){
jScrollPane = new JScrollPane(mainPanel);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(jScrollPane);
}
public void initObjects(){
for ( int i = 0; i< 15;i++){
alInteger.add(i);
contPanel = new ContPanel(alInteger.get(i));
alContPanel.add(contPanel);
}
jButtonDeleteAll.addActionListener(deleteListener);
}
public void buildLayout(){
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
addScrollPaneFrame();
displayComponents();
}
public void refreshMainPanel(){
clearMainPanel();
displayComponents();
}
public void displayComponents() {
fillOfContPanel();
mainPanel.add(jButtonDeleteAll);
setContentPane(jScrollPane);
}
public void clearMainPanel(){
mainPanel.removeAll();
}
public void fillOfContPanel(){
for (int i = 0; i< alInteger.size(); i++){
mainPanel.add(alContPanel.get(i));
}
}
class ContPanel extends JPanel{
/**
this class represents the item for a specific element of
an arrayList with a dedicated checkbox.
*/
public Integer integ;
public JLabel jLabel = new JLabel();
public JCheckBox jCheckBox = new JCheckBox();
public ContPanel(Integer integ){
this.integ = integ;
jLabel.setText(this.integ.toString());
add(jLabel);
add(jCheckBox);
}
}
class DeleteListener implements ActionListener, Runnable {
/**
*
* @param actionEvent
this class allows to remove the selected items
and display the mainPanel after deletion.
*/
@Override
public void actionPerformed(ActionEvent actionEvent) {
run();
}
@Override
public void run() {
for(int i = alContPanel.size()-1; i>= 0 ; i--){
if(alContPanel.get(i).jCheckBox.isSelected()){
alInteger.remove(i);
alContPanel.remove(i);
}
refreshMainPanel();
}
}
}
}