[英]How to change JList?
I am working on a programm with some buttons and a JList
.我正在开发一个带有一些按钮和JList
的程序。 It is very simple and the only thing it can do is to show the clicks made in a JList
.它非常简单,它唯一能做的就是显示在JList
中所做的点击。 Its a school programm, I have to make it with a JList
.它是一个学校课程,我必须使用JList
来完成。
I managed to get it working with a JLabel
(which counts all clicks and shows it on the bottom) but not with an JList
.我设法让它与JLabel
一起工作(它计算所有点击并显示在底部)但不是JList
。
There are four buttons, and each of them has a own counter.有四个按钮,每个按钮都有自己的计数器。 How can I update the JList
(the count of the specific button which is clicked) in the JList
?如何更新JList
中的JList
(单击的特定按钮的计数)?
I made for each button a int
variable which is updated in the actionPerformed
method whenever the button is clicked.我为每个按钮创建了一个int
变量,只要单击该按钮,该变量就会在actionPerformed
方法中更新。 The JList
shows those variables, with the button names in front of it. JList
显示了这些变量,并在其前面显示了按钮名称。 But it stays on 0
, how can I update the JList
?但它保持在0
,我该如何更新JList
?
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
public class Task1 extends JFrame implements ActionListener {
public int m1 = 0;
public int m2 = 0;
public int m3 = 0;
public int m4 = 0;
JButton Menue1;
JButton Menue2;
JButton Menue3;
JButton Menue4;
JLabel unten;
JLabel text;
JList<String> Liste;
public Task1(){
setTitle("Menüauswahl");
// Bereits gewählte Menüs
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
add(panel, BorderLayout.EAST);
text = new JLabel("Bereits gewählte Menüs ");
panel.add(text);
String[] menues = {"Menü 1: " + m1+"x", "Menü 2: " + m2+"x", "Menü 3: " + m3+"x", "Menü 4: " + m4+"x"};
Liste = new JList<String>(menues);
panel.add(Liste);
// Menürbar
JMenuBar menubar = new JMenuBar();
JMenu Datei = new JMenu("Datei");
menubar.add(Datei);
setJMenuBar(menubar);
// Gesamt Menüs
unten = new JLabel("Menüs Insgesamt: 0", SwingConstants.CENTER);
unten.setBorder(new LineBorder(Color.BLACK));
add(unten, BorderLayout.SOUTH);
// Menü-Auswahl / Buttons
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,2, 10, 10));
add(panel1, BorderLayout.CENTER);
Menue1 = new JButton("Menü 1");
Menue1.addActionListener(this);
panel1.add(Menue1);
Menue2 = new JButton("Menü 2");
Menue2.addActionListener(this);
panel1.add(Menue2);
Menue3 = new JButton("Menü 3");
Menue3.addActionListener(this);
panel1.add(Menue3);
Menue4 = new JButton("Menü 4");
Menue4.addActionListener(this);
panel1.add(Menue4);
//Fenster Config
setSize(450 , 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent click) {
if(click.getSource()==Menue1){
m1+=1;
System.out.println(m1);
unten.setText(String.format("Menüs Insgesamt: " + (m1+m2+m3+m4)));
}
if(click.getSource()==Menue2){
m2+=1;
System.out.println(m2);
unten.setText(String.format("Menüs Insgesamt: " + (m1+m2+m3+m4)));
}
if(click.getSource()==Menue3){
m3+=1;
System.out.println(m3);
unten.setText(String.format("Menüs Insgesamt: " + (m1+m2+m3+m4)));
}
if(click.getSource()==Menue4){
m4+=1;
System.out.println(m4);
unten.setText(String.format("Menüs Insgesamt: " + (m1+m2+m3+m4)));
}
}
public static void main(String[] args){
new Task1();
}
} }
As your example is school-level (not as an offence, I just don't want to rewrite it all), we'll keep it simple.由于您的示例是学校级别的(不是冒犯,我只是不想全部重写),我们将保持简单。
As of now you're directly imputting items in the JList
through its constructor到目前为止,您正在通过 JList 的构造函数直接在JList
中输入项目
String[] menues = {
"Menü 1: " + m1+"x",
"Menü 2: " + m2+"x",
"Menü 3: " + m3+"x",
"Menü 4: " + m4+"x"
};
Liste = new JList<String>(menues);
This works for showing only, as you don't have access to a ListModel
that is capable of updating.这仅适用于显示,因为您无权访问能够更新的ListModel
。
Thus we need to instantiate ourself a DefaultListModel
因此我们需要为自己实例化一个DefaultListModel
private final DefaultListModel<String> listModel;
and add our initial values并添加我们的初始值
listModel = new DefaultListModel<>();
listModel.addElement("Menü 1: 0x");
listModel.addElement("Menü 2: 0x");
listModel.addElement("Menü 3: 0x");
listModel.addElement("Menü 4: 0x");
Now, construct the JList
with that DefaultListModel
现在,使用DefaultListModel
构造JList
Liste = new JList<>(listModel);
We can then update the list items using our model directly in actionPerformed
然后我们可以直接在actionPerformed
中使用我们的 model 更新列表项
if (click.getSource() == Menue1) {
...
listModel.setElementAt("Menü 1: " + m1 + "x", 0); // Indexes start at 0!
}
if (click.getSource() == Menue2) {
...
listModel.setElementAt("Menü 2: " + m2 + "x", 1);
}
if (click.getSource() == Menue3) {
...
listModel.setElementAt("Menü 3: " + m3 + "x", 2);
}
if (click.getSource() == Menue4) {
...
listModel.setElementAt("Menü 4: " + m4 + "x", 3);
}
Anyway, remember to read the documentation , always!无论如何,请记住始终阅读文档!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.