1

我被一个必须在学校做的项目困住了。

我试图JList为“简单”用户界面创建一个自定义:用例应该显示一个订单列表,用户应该能够选择一个并通过按下两个按钮(读取和完成)来设置是否读取/完成也作为“unsetters”(我在 Order 类中使用 2 个布尔值,它继续通过 t,f,t,f...)。按下完成后,所选行应变为绿色(并永久保持该颜色),直到再次按下该行上的“完成”按钮(=未设置)。与“读取”按钮相同,该按钮应将与 Order 类中的“is_read”标志相关的选定行永久变为黄色。

在阅读了一些相关的帖子后,我写了这个自定义ListCellRenderer

public class CookListCellRender extends JLabel implements ListCellRenderer<Order>{

public CookListCellRender(){
    setOpaque(true);
}

@Override
public Component getListCellRendererComponent(JList<? extends Order> list, Order value, int index, boolean isSelected, boolean cellHasFocus) {

    setLayout(new FlowLayout());
    setText(value.getNameEl());
    list.setVisibleRowCount(30);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    setFont(new Font("Arial",Font.ITALIC,17));

    if (isSelected) {
        setBackground(Color.BLUE);
        setForeground(Color.BLUE);
    } else {
        setBackground(getBackground());
        setForeground(getForeground());
    }

    if(value.getOrderRead()){
        setBackground(Color.YELLOW);
    }else{
        setBackground(Color.WHITE);
    }

    if(value.getOrderDone()){
        setBackground(Color.GREEN);
    }else{
        setBackground(Color.WHITE);
    }
    return this;
}

}

我的问题是,一旦运行我的测试文件,一切都会正确弹出,但是当我选择一行并按下这两个按钮中的一个时,在我选择一个新行之前什么都没有发生:然后一直变绿或变黄(取决于我按下的按钮) 我选择的行的颜色。

我很确定这是与我的自定义单元格渲染器相关的问题,也是因为我仍在学习如何使用它,我开始变得很困惑。

这是我与 UI 相关的其余相关代码,也许它很有用。按钮及其侦听器在一个单独的类(自定义JPanel)中,但它们工作正常,所以......

InitGraphics(由我的测试类直接调用):

public class InitGraphics {

Cook c;
JList list;

public InitGraphics(Cook cuoco){
    c=cuoco;
    initUi();
  }

private void initUi() {
    JFrame frame = new JFrame("Cucina");
    list =new JList(c.getOrdersCopy().toArray());
    list.setCellRenderer(new CookListCellRender());
    JScrollPane panel = new JScrollPane(list);
    panel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    panel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    ButtonPanel buttonPanel = new ButtonPanel(c.getOrdersCopy(),list);
    buttonPanel.setLayout(new FlowLayout());
    frame.add(buttonPanel,BorderLayout.NORTH);
    frame.add(panel,BorderLayout.CENTER);
    frame.setVisible(true);
    frame.setSize(600, 400);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
}
4

1 回答 1

4

这正在杀死你:

    } else {
        setBackground(getBackground());
        setForeground(getForeground());
    }

它根本不会改变任何东西。我认为你想要:

    } else {
        setBackground(null);
        setForeground(null);
    }

请注意,要对这段代码做任何工作,我必须创建自己的SSCCE

import java.awt.*;
import javax.swing.*;

public class InitGraphics {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            final DefaultListModel<Order> orderModel = new DefaultListModel<>();
            orderModel.addElement(new Order("One"));
            orderModel.addElement(new Order("Two"));
            orderModel.addElement(new Order("Three"));
            orderModel.addElement(new Order("Four"));
            orderModel.addElement(new Order("Five"));

            JList<Order> orderList = new JList<>(orderModel);
            orderList.setCellRenderer(new CookListCellRender());

            JPanel panel = new JPanel();
            panel.add(new JScrollPane(orderList));

            JOptionPane.showMessageDialog(null, panel);
        });
    }
}

class CookListCellRender extends JLabel implements ListCellRenderer<Order> {

    public CookListCellRender() {
        setOpaque(true);
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends Order> list, Order value, int index,
            boolean isSelected, boolean cellHasFocus) {

        setLayout(new FlowLayout());
        setText(value.getNameEl());
        list.setVisibleRowCount(30);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setFont(new Font("Arial", Font.ITALIC, 17));

        if (isSelected) {
            setBackground(Color.BLUE);
            setForeground(Color.BLUE);
        } else {
            // TODO: fix!
            // setBackground(getBackground());
            // setForeground(getForeground());
            setBackground(null);
            setForeground(null);
        }

        if (value.getOrderRead()) {
            setBackground(Color.YELLOW);
        } else {
            setBackground(Color.WHITE);
        }

        if (value.getOrderDone()) {
            setBackground(Color.GREEN);
        } else {
            setBackground(Color.WHITE);
        }
        return this;
    }

}

class Order {

    private String nameE1;
    private boolean orderDone;
    private boolean orderRead;

    public Order(String nameE1) {
        this.nameE1 = nameE1;
    }

    public String getNameEl() {
        return nameE1;
    }

    public boolean getOrderDone() {
        return orderDone;
    }

    public boolean getOrderRead() {
        return orderRead;
    }

    public void setOrderDone(boolean orderDone) {
        this.orderDone = orderDone;
    }

    public void setOrderRead(boolean orderRead) {
        this.orderRead = orderRead;
    }

}

将来,请为我们做这项工作,因为您是寻求帮助的人,而我们是志愿者。

还有这个:

    list.setVisibleRowCount(30);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

永远不要在渲染器中做这种事情。这段代码应该放在你创建 JList 的地方,而不是在渲染器中。

这是我最新的 SSCCE,其中包括清除/读取/完成按钮

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.*;
import java.util.List;

import javax.swing.*;

public class InitGraphics {
    @SuppressWarnings("serial")
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            final DefaultListModel<Order> orderModel = new DefaultListModel<>();
            orderModel.addElement(new Order("One"));
            orderModel.addElement(new Order("Two"));
            orderModel.addElement(new Order("Three"));
            orderModel.addElement(new Order("Four"));
            orderModel.addElement(new Order("Five"));

            final JList<Order> orderList = new JList<>(orderModel);
            orderList.setCellRenderer(new CookListCellRender());
            orderList.setVisibleRowCount(5);

            JPanel panel = new JPanel();
            // panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
            panel.add(new JScrollPane(orderList));

            panel.add(new JButton(new AbstractAction("Read") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    List<Order> selectedOrders = orderList.getSelectedValuesList();
                    for (Order order : selectedOrders) {
                        order.setOrderRead(true);
                    }
                    orderList.repaint();
                }
            }));
            panel.add(new JButton(new AbstractAction("Done") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    List<Order> selectedOrders = orderList.getSelectedValuesList();
                    for (Order order : selectedOrders) {
                        order.setOrderDone(true);
                    }
                    orderList.repaint();
                }
            }));
            panel.add(new JButton(new AbstractAction("Clear") {

                @Override
                public void actionPerformed(ActionEvent e) {

                    for (int i = 0; i < orderModel.size(); i++) {
                        orderModel.getElementAt(i).setOrderDone(false);
                        orderModel.getElementAt(i).setOrderRead(false);
                    }
                    orderList.repaint();
                }
            }));


            JFrame frame = new JFrame("Test List");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

@SuppressWarnings("serial")
class CookListCellRender extends JLabel implements ListCellRenderer<Order> {

    public CookListCellRender() {
        setOpaque(true);
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends Order> list, Order value, int index,
            boolean isSelected, boolean cellHasFocus) {

        // setOpaque(true); // !! 

        // !! setLayout(new FlowLayout());
        setText(value.getNameEl());
        // !! list.setVisibleRowCount(30);
        // !! list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setFont(new Font("Arial", Font.ITALIC, 17));

        if (isSelected) {
            setBackground(Color.BLUE);
            setForeground(Color.BLUE);
        } else {
            // TODO: fix!
            // setBackground(getBackground());
            // setForeground(getForeground());
            setBackground(null);
            setForeground(null);
        }

        if (value.getOrderRead()) {
            setBackground(Color.YELLOW);
        } else {
            setBackground(Color.WHITE);
        }

        if (value.getOrderDone()) {
            setBackground(Color.GREEN);
        } else {
            setBackground(Color.WHITE);
        }
        return this;
    }

}

class Order {

    private String nameE1;
    private boolean orderDone;
    private boolean orderRead;

    public Order(String nameE1) {
        this.nameE1 = nameE1;
    }

    public String getNameEl() {
        return nameE1;
    }

    public boolean getOrderDone() {
        return orderDone;
    }

    public boolean getOrderRead() {
        return orderRead;
    }

    public void setOrderDone(boolean orderDone) {
        this.orderDone = orderDone;
    }

    public void setOrderRead(boolean orderRead) {
        this.orderRead = orderRead;
    }

}
于 2016-08-04T22:27:14.410 回答