gpt4 book ai didi

Java JRadioButton

转载 作者:行者123 更新时间:2023-12-01 14:43:06 28 4
gpt4 key购买 nike

我对这段代码有疑问当我单击文件并单击新时,新面板出现在屏幕上,当我想更改 JRadioBox 状态以更改标签状态时,标签状态发生变化,但面板也消失了:(

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 JRadioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15744180/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com