【发布时间】:2022-01-02 19:35:59
【问题描述】:
当我将项目添加到模型列表时,它们在JList 上显示得非常奇怪,我不知道为什么它真的很小而且它们也重叠。当我预设一个列表时,它会像setFixedCell 中的设置一样正常显示,你能给我一个提示吗?
import javax.swing.*;
public class test{
static JFrame mainFrame;
static JPanel mainPanel;
static JButton mainSwitch;
static JList mainList;
static long firstTime = 0;
static long times1;
static int i = 0;
static boolean x = false;
static DefaultListModel model = new DefaultListModel();
// hole Inferface stuff
test(){
mainFrame = new JFrame("Zeiterfassung");
mainPanel = new JPanel();
mainSwitch = new JButton("ON");
mainList = new JList(model);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(420, 420);
mainPanel.setSize(10, 10);
mainPanel.setLayout(null);
mainList.setBounds(12, 10, 380, 300);
mainList.setFixedCellHeight(30);
mainList.setFixedCellWidth(30);
mainSwitch.setBounds(12, 320, 380, 50);
mainSwitch.addActionListener(e -> messureTime());
mainPanel.add(mainSwitch);
mainPanel.add(mainList);
mainFrame.add(mainPanel);
mainFrame.setVisible(true);
}
// Method behind the button, normal boolean that changes the text on button "startTracking"
public static void messureTime() {
if(x == false) {
firstTime = System.currentTimeMillis();
mainSwitch.setText("Stop");
x = true;
}
else if(x==true){
times1 = (System.currentTimeMillis() - firstTime)/1000;
model.addElement(String.valueOf(times1) + "Seconds");
mainSwitch.setText("Start");
x = false;
mainList.setFixedCellHeight(5);
i++;
mainList.setModel(model);
}
}
public static void main(String[] args) {
Zeit zeiterfassung = new Zeit();
}
}
【问题讨论】:
-
建议 1) 使用布局管理器 2) 将 JList 放在
JScrollPane内以允许滚动
标签: java swing jpanel jbutton jlist