一个 JList/JCheckBox 组合不起作用

One JList/JCheckBox combination not working

提问人:stupidmoron 提问时间:5/15/2022 更新时间:5/15/2022 访问量:23

问:

我正在一本书中做一个练习,你必须测试事件处理。我有一个 JList,有 3 种字体选择(Serif、SansSerif 和 Monospaced)以及两个粗体和斜体复选框。我有一个显示用户选择的 JLabel。出于某种疯狂的原因,当我从 JList 中选择 Monospaced 并从 JCheckBox 中选择 Bold 和 Italic 时,JLabel 上没有任何内容显示。我需要另一双眼睛来观察它,因为我什么也找不到。代码发布如下:

import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Events extends JFrame {
    private JLabel insLabel;
    private JLabel fontLabel;
    private JLabel mouseLabel;
    private JLabel resultLabel;
    private JList fontList;
    private static final String[] fontNames = {"Serif", "SansSerif", "Monospaced"};
    private JCheckBox boldCb;
    private JCheckBox italicCb;
    private JButton submitButton;
    private JPanel middlePanel;
    private JPanel bottomPanel;
    private String fontName;
    private String fontStyle;
    
    public Events() {
        super("Demonstrating Events");
        setLayout(new BorderLayout());
        insLabel = new JLabel("Please feel free to click anywhere or press any key.");
        fontLabel = new JLabel("Choose a font:");
        fontList = new JList(fontNames);
        fontList.setVisibleRowCount(3);
        fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        add(new JScrollPane(fontList));
        boldCb = new JCheckBox("Bold");
        italicCb = new JCheckBox("Italic");
        submitButton = new JButton("Submit");
        mouseLabel = new JLabel("");
        resultLabel = new JLabel("");
        middlePanel = new JPanel();
        middlePanel.setLayout(new FlowLayout());
        bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        add(insLabel, BorderLayout.NORTH);
        middlePanel.add(fontLabel);
        middlePanel.add(fontList);
        middlePanel.add(boldCb);
        middlePanel.add(italicCb);
        middlePanel.add(submitButton);
        add(middlePanel, BorderLayout.CENTER);
        bottomPanel.add(mouseLabel);
        bottomPanel.add(resultLabel);
        add(bottomPanel, BorderLayout.SOUTH);
        
        // set up event handling
        
        fontList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                fontName = fontNames[fontList.getSelectedIndex()];
            }
        });
        
        CheckBoxHandler handler = new CheckBoxHandler();
        boldCb.addItemListener(handler);
        italicCb.addItemListener(handler);
        
        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(fontStyle == "both") {
                    resultLabel.setFont(new Font(fontName, Font.BOLD + Font.ITALIC, 18));
                    resultLabel.setText("You chose " + fontName + " bold and italic");
                } else if(fontStyle == "bold") {
                    resultLabel.setFont(new Font(fontName, Font.BOLD, 18));
                    resultLabel.setText("You chose " + fontName + " bold");
                } else if(fontStyle == "italic") {
                    resultLabel.setFont(new Font(fontName, Font.ITALIC, 18));
                    resultLabel.setText("You chose " + fontName + " italic");
                } else {
                    resultLabel.setFont(new Font(fontName, Font.PLAIN, 18));
                    resultLabel.setText("You chose " + fontName + " plain");
                }
            }
        });
        
        addMouseListener(new MouseClickHandler());
        addMouseMotionListener(new MouseMotionHandler());
        addKeyListener(new KeyHandler());
    } // end constructor
    
    private class CheckBoxHandler implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            if(boldCb.isSelected() && italicCb.isSelected())
                fontStyle = "both";
            else if(boldCb.isSelected())
                fontStyle = "bold";
            else if (italicCb.isSelected())
                fontStyle = "italic";
            else
                fontStyle = "plain";
        }
    }
    
    private class MouseClickHandler extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            int xPos = e.getX();
            int yPos = e.getY();
            mouseLabel.setText("Mouse clicked at (" + xPos + ", " + yPos + ")");
        }
    }
    
    private class MouseMotionHandler extends MouseMotionAdapter {
        public void mouseMoved(MouseEvent e) {
            int xPos = e.getX();
            int yPos = e.getY();
            mouseLabel.setText("Mouse moved at (" + xPos + ", " + yPos + ")");
        }
    }
    
    private class KeyHandler extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            resultLabel.setText("You pressed " + KeyEvent.getKeyText(e.getKeyCode()));
        }
    }
}
    
    

Java jlist jcheckbox

评论

0赞 Abdullah Leghari 5/15/2022
您的程序在我的系统上运行良好。窗户 11。OpenJDK 17.你用的是什么操作系统和Java版本?
0赞 user11350388 5/15/2022
在macOS上运行良好,也许您的系统没有等宽字体?
0赞 stupidmoron 5/15/2022
我使用的是 Windows 10 和 JDK 16。此外,我的 Key 事件处理不起作用。

答: 暂无答案