选择JRadioButton时如何刷新JPanel? [英] How to refresh JPanel when JRadioButton is select?

查看:130
本文介绍了选择JRadioButton时如何刷新JPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i具有此类:


i have this class:

public class TF extends JPanel implements ActionListener
{
    private ButtonGroup buttonGroup = new ButtonGroup();
    private JRadioButton rdbtnFilm;
    private JRadioButton rdbtnSerieTv;
    private JPanel pnlGeneral;
    private JPanel pnlRadioButton;
    private JLabel lblTitolo;
    private JTextField tfTitolo;

    public TF() 
    {   
        pnlGeneral = new JPanel();
        pnlRadioButton = new JPanel();

        rdbtnFilm = new JRadioButton("Film");
        rdbtnFilm.addActionListener(this);
        pnlRadioButton.add(rdbtnFilm);
        buttonGroup.add(rdbtnFilm);

        rdbtnSerieTv = new JRadioButton("Serie Tv");
        rdbtnSerieTv.addActionListener(this);
        pnlRadioButton.add(rdbtnSerieTv);
        buttonGroup.add(rdbtnSerieTv);

        pnlGeneral.add(pnlRadioButton, gbc_panel_1);
        this.add(pnlGeneral);
    }

    public void actionPerformed(ActionEvent e) 
    {
        if(rdbtnFilm.isSelected())
        {
            lblTitolo = new JLabel("Titolo");
            pnlGeneral.add(lblTitolo, gbc_lblTitolo);

            tfTitolo = new JTextField();
            tfTitolo.setColumns(10);
            tfTitolo.setText("1");
            pnlGeneral.add(tfTitolo, gbc_tfTitolo);
        }

        if(rdbtnSerieTv.isSelected())
        {
            lblTitolo = new JLabel("Titolo");
            pnlGeneral.add(lblTitolo, gbc_lblTitolo);

            tfTitolo = new JTextField();
            tfTitolo.setColumns(10);
            tfTitolo.setText("1");
            pnlGeneral.add(tfTitolo, gbc_tfTitolo);
        }
    }
}

但是当我选择收音机时按钮只是有时,JPanel向我展示了JLabel和JTextfield。我不知道是否需要使用revalidate(),repaint()或其他方法刷新JPanel。
发生这种情况是因为Swing不是线程安全的吗?

but when i select a radio button just sometimes JPanel show me JLabel and JTextfield. I don't know if i need use revalidate(), repaint() or something else to refresh JPanel. This happens because Swing isn't thread-safe?

Update

Update

public class Main
{
    public static void main(String[] args) throws IOException
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
              public void run() 
              {
                  JFrame gui=new GUI();
                  gui.setVisible(true);
                  gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  gui.setSize(800,720);
              }
        });
    }
}


public class GUI extends JFrame implements MouseListener
{
    private boolean selected=true;
    public GUI()
    {   
        //Creazione del Menu
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);

        JMenu menu = new JMenu("Switcha");
        menuBar.add(menu);
        menu.addMouseListener(this);
        this.add(new TF());
    }

    public void mouseClicked(MouseEvent e)
    {
        if(selected)
        {
            this.getContentPane().removeAll();
            this.getContentPane().add(new TF());
            // do something else
        }
        else
        {
            this.getContentPane().removeAll();
            this.getContentPane().add(new TF2());
            // do something else
        }
    }
}


推荐答案


发生这种情况是因为Swing不是线程安全的吗?

不,这是因为您将组件添加到已经显示的容器中,导致组件层次结构无效。请参见容器# add(Component comp)文档。

No, this happens because you are adding components to an already displayed container, causing the components hierarchy be invalidated. See Container#add(Component comp) docs.


我不知道是否需要使用 revalidate() repaint()或其他刷新 JPanel 的东西。 / em>

I don't know if I to need use revalidate(), repaint() or something else to refresh JPanel.

是的,按照上一点。在这种情况下,典型的顺序是:

Yes, as per previous point. The tipical sequence in this case is:

panel.add(someComponent);
panel.revalidate();
panel.repaint();






还有另一种方法可以做到这一点吗?



是的。正如我在评论中所述,恕我直言 CardLayout 是正确的处理方法。请参见如何使用CardLayout 教程。


Is there another way to accomplish this?

Yes, it is. As I've stated in my comment, IMHO CardLayout is the proper way to go through. See How to Use CardLayout tutorial.

简而言之:


  • 使用单选按钮保留面板。

  • 具有两个不同的面板:一个用于电影,另一个用于电视连续剧。

  • 具有另一个 JPanel CardLayout

  • 将这两个面板(或根据需要添加的多个面板)添加为卡片。

  • 在单选按钮上更改只需在卡布局面板上切换右侧卡即可。

  • Keep your panel with radio buttons.
  • Have two different panels: one for films and another one for TV series.
  • Have another JPanel with CardLayout.
  • Add these two panels (or many as needed) as cards.
  • On radio buttons change just switch the right "card" for the card layout panel.

这篇关于选择JRadioButton时如何刷新JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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