每次在JList上单击都可以重新绘制JPanel [英] repaint JPanel with every click at JList

查看:120
本文介绍了每次在JList上单击都可以重新绘制JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

everytime i click on a JList item, i need to clear + refresh my current panel & load another panel, returned via method 'populateWithButtons()'. temp is an int variable that stores what was clicked at the JList. How do i rectify the following?

        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);
        }

推荐答案

  1. 不要使用 NullLayout

ListSelectionListener 添加到 JList 而不是

add ListSelectionListener to JList instead of MouseListener, otherwise you would need to convert point from mouse to Item in JList

使用 CardLayout 代替添加,在运行时删除 JPanel ,然后从ListSelectionListener中选择(ListSelectionModelSINGLE ...)以切换准备好的卡(具有某些内容的JPanel)

use CardLayout instead of add, remove JPanels on runtime, then selection from ListSelectionListener (ListSelectionModel to SINGLE...) to switch prepared card (JPanel with some contents)

编辑

.

.

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();
            }
        });
    }
}

这篇关于每次在JList上单击都可以重新绘制JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆