每次单击JList重新绘制JPanel

时间:2013-10-22 06:40:36

标签: java swing jpanel jlist mouse-listeners

每当我点击JList项目时,我需要清除+刷新我当前的面板&加载另一个面板,通过方法'populateWithButtons()'返回。 temp是一个int变量,用于存储在JList中单击的内容。我如何纠正以下内容?

        list_1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {

                //refresh + populate JPanel
                Food food = new Food();             
                JPanel panel2 = new JPanel();
                JPanel pane11 = new JPanel();


                panel2.add(panel1);
                panel1.validate();
                panel1.repaint();
                panel1.setBounds(153, 74, 281, 269);

                panel1.add(food.populateWithButtons(temp));             
                contentPane.add(panel2);
        }

3 个答案:

答案 0 :(得分:3)

  1. 不要使用NullLayout

  2. ListSelectionListener添加到JList而不是MouseListener,否则您需要将点从鼠标转换为JList中的Item

  3. 使用CardLayout代替添加,在运行时删除JPanel,然后从ListSelectionListenerListSelectionModelSINGLE选择...)切换准备好的卡片(JPanel包含一些内容)

  4. 修改

    enter image description here

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.util.Vector;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.ListSelectionModel;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class CardlayoutTest {
    
        private Color[] colors = new Color[]{Color.BLACK, Color.RED, Color.GREEN, Color.BLUE};
        private JFrame frame = new JFrame();
        private JList list = new JList();
        private JPanel panel = new JPanel();
        private CardLayout card = new CardLayout();
    
        public CardlayoutTest() {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            panel.setLayout(card);
            Vector<String> items = new Vector<String>();
            for (int x = 0; x < colors.length; x++) {
                JPanel pnl = new JPanel(new BorderLayout());
                pnl.setBackground(colors[x]);
                panel.add(pnl, colors[x].toString());
                items.add(colors[x].toString());
            }
            list = new JList(items);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        String card = list.getSelectedValue().toString();
                        CardLayout cL = (CardLayout) (panel.getLayout());
                        cL.show(panel, card);
                    }
                }
            });
            frame.add(new JScrollPane(list), BorderLayout.WEST);
            frame.add(panel);
            frame.setPreferredSize(new Dimension(400, 150));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new CardlayoutTest();
                }
            });
        }
    }
    

答案 1 :(得分:0)

在添加到validate()之后移动repaint()contentPane,因为在那一点上它将被重绘。

答案 2 :(得分:0)

使用正确的架构模式 - 例如MVC。您拥有here的基本用法示例。

  • 然后您的MouseAdapter将更新model中所选项目的变量,并使用repaint()方法刷新视图(而不是直接在GUI上工作)。
  • 视图将使用覆盖JPanel方法保留paint(),该方法根据模型中的所选元素显示内容。

你以后会感谢我的。 ;)在你的方法中,你将失去灵活性,很快就会遇到另一个问题。

糟糕的方式(但会起作用)

  • 在您的视图中,为所需的面板创建setter和getter。
  • MouseAdapter(或其他听众)中保留对该视图的引用。
  • 在您的mouseClicked()方法中创建一个新面板,然后使用setter覆盖旧面板。
相关问题