gpt4 book ai didi

java - Jtable 列中的 ArrayList

转载 作者:行者123 更新时间:2023-11-29 03:06:38 26 4
gpt4 key购买 nike

我正在创建一个只有一列(月列)的 Jtable,并在按钮的帮助下在该列中添加一个数组列表。

Arraylist 包含月份的名称,具体取决于选中的月份。

我的问题是当我添加数组列表时,前一行也发生了变化。

我不知道发生了什么。

这是我的问题代码

package test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Jtable extends JFrame {

JTable table;
JButton button;
JPanel mp;

DefaultTableModel model;

JCheckBox chkJan;
JCheckBox chkFeb;
JCheckBox chkMarch;
JCheckBox chkApril;
JCheckBox chkMay;
JCheckBox chkJun;
JCheckBox chkJuly;
JCheckBox chkAug;
JCheckBox chkSep;
JCheckBox chkOct;
JCheckBox chkNov;
JCheckBox chkDec;

List<String> Months = null;

public Jtable() {

table = new JTable();
button = new JButton("Add to Table");
mp = new JPanel();
Months = new ArrayList<>();

chkJan = new JCheckBox("JAN");
chkFeb = new JCheckBox("FEB");
chkMarch = new JCheckBox("MARCH");
chkApril = new JCheckBox("APRIL");
chkMay = new JCheckBox("MAY");
chkJun = new JCheckBox("JUN");
chkJuly = new JCheckBox("JULY");
chkAug = new JCheckBox("AUG");
chkSep = new JCheckBox("SEP");
chkOct = new JCheckBox("OCT");
chkNov = new JCheckBox("NOV");
chkDec = new JCheckBox("DEC");

mp.setLayout(new FlowLayout(FlowLayout.LEFT));
mp.setSize(300, 200);

mp.add(chkJan);
mp.add(chkFeb);
mp.add(chkMarch);
mp.add(chkApril);
mp.add(chkMay);
mp.add(chkJun);
mp.add(chkJuly);
mp.add(chkAug);
mp.add(chkSep);
mp.add(chkOct);
mp.add(chkNov);
mp.add(chkDec);

model = new DefaultTableModel();
table.setModel(model);
model.addColumn("Months");

JScrollPane sp = new JScrollPane(table);

setLayout(new BorderLayout());

add(sp, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
add(mp, BorderLayout.NORTH);
setSize(800, 600);

button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Month();
Object[] row = {Months};

model.addRow(row);
}
});

}

public void Month() {
Months.removeAll(Months);
if (chkJan.isSelected()) {
Months.add("January");
} else {
Months.remove("January");
}

if (chkFeb.isSelected()) {
Months.add("Febuary");
} else {
Months.remove("Febuary");
}

if (chkMarch.isSelected()) {
Months.add("March");
} else {
Months.remove("March");
}

if (chkApril.isSelected()) {
Months.add("April");
} else {
Months.remove("April");
}

if (chkMay.isSelected()) {
Months.add("May");
} else {
Months.remove("May");
}

if (chkJun.isSelected()) {
Months.add("Jun");
} else {
Months.remove("Jun");
}

if (chkJuly.isSelected()) {
Months.add("July");
} else {
Months.remove("July");
}

if (chkAug.isSelected()) {
Months.add("August");
} else {
Months.remove("August");
}

if (chkSep.isSelected()) {
Months.add("September");
} else {
Months.remove("September");
}

if (chkOct.isSelected()) {
Months.add("Octuber");
} else {
Months.remove("Octuber");
}

if (chkNov.isSelected()) {
Months.add("November");
} else {
Months.remove("November");
}

if (chkDec.isSelected()) {
Months.add("December");
} else {
Months.remove("December");
}
}

public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;

}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Jtable.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Jtable().setVisible(true);
}
});
}

}

最佳答案

你的问题是 Months 是全局的,你想要的是一个局部变量。如果您在某处编辑此变量,则使用它的每个对象(如行或您的表)都将反射(reflect)其更改。

您可以将 Months 移动到您的方法中您只能进行本地访问的某个位置,如下所示:

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.addRow(Months());
}
});

然后,修改您的 Month 方法:

public Object[] Month() {
List<String> Months = new ArrayList<>();
if (chkJan.isSelected()) {
Months.add("January");
} else ...
...
return new Object[]{Months};
}

另一件事,一个很好的做法是用小写命名你的变量 Months 可以是 months,并将你的方法命名为更易读的东西,比如 getSelectedMonths 最后,将您的类命名为 Jtable 会让人感到困惑,因为还有另一个具有相似名称的类 (JTable)。

编辑

再次检查后,我猜你的很多代码都是多余的,你可以删除并优化,因为你只需要 Month 返回。

关于java - Jtable 列中的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31886905/

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