0
我有這個代碼的問題 當我點擊文件,並點擊新的,新的面板來到屏幕上,當我想改變JRadioBox狀態來改變標籤狀態,標籤狀態的變化,但面板消失: (爪哇JRadioButton
public class MainClass {
public static void main(String[] args) {
new MainFrame();
}
}
class Toolbar extends JPanel {
private JRadioButton Status1;
private JRadioButton Status2;
private ButtonGroup radioButtonGroup;
public Toolbar() {
super();
setLayout(new FlowLayout());
Status1 = new JRadioButton("Status1");
Status2 = new JRadioButton("Status2");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(Status2);
radioButtonGroup.add(Status1);
Status1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
MainFrame m = new MainFrame();
m.l.setText("Status1");
}
});
Status2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
MainFrame m = new MainFrame();
m.l.setText("Status2");
}
});
add(Status1);
add(Status2);
}
}
class Panel extends JPanel {
public Panel() {
super();
setBackground(Color.MAGENTA);
}
}
class MenuBar extends JMenuBar {
private JMenu menu;
private JMenuItem fileItems;
public boolean panel = false;
public MenuBar() {
super();
menu = new JMenu("File");
add(menu);
fileItems = new JMenuItem("New");
menu.add(fileItems);
fileItems.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainFrame mf = new MainFrame();
Panel p = new Panel();
mf.addPanel(p);
mf.add(new Toolbar(), BorderLayout.NORTH);
repaint();
}
});
}
}
class MainFrame extends JFrame {
public static JLabel l;
public MainFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
l = new JLabel("No Status");
add(l, BorderLayout.SOUTH);
MenuBar mb = new MenuBar();
setJMenuBar(mb);
setVisible(true);
}
public void addPanel(Panel p) {
add(p, BorderLayout.CENTER);
}
}
無關:。請學習Java命名約定並嚴格遵守和Don」 t _extend_ JSomething,而不是_use_ them。 – kleopatra
面板消失?不,它隱藏在新面板後面。請拖動新面板。 –
您是否聽說過cardLayout? –