gpt4 book ai didi

java - 不可见的 JRadioButtons

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:46 24 4
gpt4 key购买 nike

我有三个不同的 JRadioButton,它们都添加到一个 JPanel 中。当我将 JPanel 添加到 JFrame 时,仅显示 lvl3 JRadioButton。如果我注释掉该行,则不会显示任何按钮。我在做什么事吗?或者我应该使用 JRaduiButtonGroups?

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.Timer;


public class CastleDefenders {
static Timer timer;
Toolkit tlkt;
private JMenuBar mb2 = new JMenuBar();
private static boolean startGame = false;
private static JFrame f = new JFrame("Castle Defenders");
private static JFrame g = new JFrame("Castle Defenders");
private JRadioButton lvl1 = new JRadioButton("Easy");
private JRadioButton lvl2 = new JRadioButton("Normal");
private JRadioButton lvl3 = new JRadioButton("Hard");
private JPanel pnl = new JPanel();
private JTextField lbl = new JTextField("Welcome to Castle Defenders!");
private JButton btn = new JButton("START");
private JMenuBar mb = new JMenuBar();
private JMenu mnuFile = new JMenu("File");
private JPanel pnl2 = new JPanel();
private JMenuItem mnuItemQuit = new JMenuItem("Quit");
private JMenu mnuHelp = new JMenu("Help");
private JMenuItem mnuItemAbout = new JMenuItem("About");

public CastleDefenders(){
lvl1.setToolTipText("Play the game on Easy");
lvl2.setToolTipText("Play the game on Normal");
lvl3.setToolTipText("Play the game on Hard");
pnl2.add(lvl1);
pnl2.add(lvl2);
pnl2.add(lvl3);
tlkt = Toolkit.getDefaultToolkit();
timer = new Timer();
pnl2.setBounds(50, 5, 1, 10);
btn.setBounds(220, 240, 5, 1);
f.setJMenuBar(mb2);
g.setJMenuBar(mb2);
mnuFile.add(mnuItemQuit);
mnuHelp.add(mnuItemAbout);
mb.add(mnuFile);
mb.add(mnuHelp);
mb2.add(mnuFile);
mb2.add(mnuHelp);
pnl.add(btn);
pnl2.add(lbl);
lbl.setBounds(0, 240, 10, 2);
lbl.setEditable(false);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(pnl, BorderLayout.CENTER);
f.addWindowListener(new ListenCloseWdw());
g.getContentPane().setLayout(new BorderLayout());
g.getContentPane().add(pnl2);
g.addWindowListener(new ListenCloseWdw());
mnuItemAbout.addActionListener(new GetAboutInfo());
btn.addActionListener(new GetButtonClick());
mnuItemQuit.addActionListener(new ListenMenuQuit());


}

public class GetButtonClick implements ActionListener{
public void actionPerformed(ActionEvent e){
startGame = true;
//JOptionPane.showMessageDialog(null, "WIP");
}
}
public class GetAboutInfo implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Made By Byron Lathi");
}
}
public class ListenCloseWdw extends WindowAdapter{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public class ListenMenuQuit implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
public void launchFrameF(){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setBounds(0, 0, 640, 680);
}
public void launchFrameG(){
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g.pack();
g.setBounds(0, 0, 640, 680);
g.getContentPane().add(lvl1).setBounds(1, 1, 1, 1);
g.getContentPane().add(lvl2).setBounds(2, 2, 1, 1);
g.getContentPane().add(lvl3).setBounds(3, 3, 1/2, 1/2);

}
public static void main(String args[]) {
final CastleDefenders main = new CastleDefenders();
main.launchFrameF();
f.setVisible(true);
do {
System.out.print("");
}while(startGame == false);
if (startGame == true){
JOptionPane.showMessageDialog(null, "We've come so far...");
f.setVisible(false);
main.launchFrameG();
g.setVisible(true);
}

}

}

最佳答案

在您的代码中,pnl2 JPanel 中已经有这些单选按钮,并且该面板已添加到框架内容 Pane 中。通过调用 launchFrameG() 方法中的 getContentPane().add() 方法再次重新添加单选按钮,替换框架的内容 Pane ,这使您的面板被替换。仅添加内容 Pane 中保留的最后添加的组件。

关于java - 不可见的 JRadioButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15961344/

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