我有这个类:
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中。 我不知道我是否需要使用重新验证(),重绘()或别的东西刷新的JPanel。 这是因为Swing是不是线程安全的?
更新
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
}
}
}