为什么我的某些组件(例如Jlabel和Jcombobox)有时在使用Jbutton后不会出现?

发布于 2025-02-02 20:13:52 字数 3149 浏览 4 评论 0原文

我正在尝试通过将JPanel(菜单)添加到框架并隐藏当前可见的面板(背景),从主屏幕过渡到选项菜单。我正在使用动作听众激活过渡。 有时在运行代码时(大多数时间更像),菜单的某些部分不会出现,例如Jcombobox和两个Jlabels。我没有JSlider的问题不过,但是我无法确定其他组件的问题。我尝试在我的代码末尾实现菜单和菜单>菜单>菜单。我还尝试执行frame.revalidate()frame.setvisible(true)。解决此问题的另一个尝试是绕菜单移动。

我使用的代码作为提供的

窗口。Java

JFrame frame;
JPanel background;
JPanel menu;
JSlider difficulty;
JLabel difficultyLabel;
JComboBox exerciseChoice;
JPanel dropDownPanel;
JLabel exerciseChoiceLabel;
JPanel difficultyPanel;

public ActionListener startButtonPressed() throws IOException {
      ActionListener temp = 
         new ActionListener() {
         
            @Override
            public void actionPerformed(ActionEvent e) {
               try {
                  background.setVisible(false);
                  menu = new JPanel();
                  menu.setLayout(new BoxLayout (menu, BoxLayout.Y_AXIS));   
                  menu.setBounds(menu.getLocation().x+50, 50, 700, 100);
                  menu.setBackground(Color.WHITE);
                  startButton.setEnabled(false);
                  createDifficultySlider(); 
                  createWorkoutDropDown();
                  
                  frame.add(menu);
                  frame.setVisible(true);
               
               } catch (IOException r) {}
            }
         
         };
      return temp;
   
   }
   
   public void createWorkoutDropDown() throws IOException {
      dropDownPanel = new JPanel();
      dropDownPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
      dropDownPanel.setBackground(Color.WHITE);
      String[] choices = {"Abs", "Cardio", "Full Body", "Upper Body", "Stretches", "Lower Body"};
      exerciseChoice = new JComboBox(choices);
      exerciseChoice.setFont(new Font("Verdana", Font.PLAIN, 20));
      exerciseChoiceLabel = new JLabel("Workout Options");
      exerciseChoiceLabel.setFont(new Font("Verdana", Font.PLAIN, 20));
      dropDownPanel.add(exerciseChoiceLabel);
      dropDownPanel.add(exerciseChoice);
      menu.add(dropDownPanel);
      
      
   }
   
   public void createDifficultySlider() throws IOException {
      difficultyPanel = new JPanel();
      difficultyPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
      difficultyPanel.setBackground(Color.WHITE);
      difficulty = new JSlider(JSlider.HORIZONTAL, 1, 10, 5);
      difficulty.setPreferredSize(new Dimension(difficulty.getPreferredSize().width + 60, difficulty.getPreferredSize().height + 5));
      difficultyLabel = new JLabel("Difficulty Level (Easy 1- Hard 10)");  
      difficultyLabel.setFont(new Font("Verdana", Font.PLAIN, 20));
      difficulty.setFont(new Font("Verdana", Font.PLAIN, 10)); 
      difficulty.setMajorTickSpacing(1);
      difficulty.setPaintLabels(true);
      difficulty.setPaintTicks(true);
      difficulty.setSnapToTicks(true);
      difficultyPanel.add(difficultyLabel, BorderLayout.PAGE_START);
      difficultyPanel.add(difficulty, BorderLayout.LINE_START);
      menu.add(difficultyPanel); 
   
   }

编辑:我不得不扔IOExceptions,因为我正在从驱动器中导入字体。

I'm trying to transition from the home screen to the option menu through a JButton by adding a JPanel (menu) to the frame and hiding the currently visible panel (background). I'm using an Action Listener to activate the transition. Sometimes when running the code (more like most of the time), some parts of the menu won't appear such as the JComboBox and two of the JLabels. I'm not having an issue with the JSlider though, but I haven't been able to identify the problem for the other components. I tried implementing menu.revalidate() and menu.setVisible(true) at the end of my code, but to no prevail. I also tried doing frame.revalidate() and frame.setVisible(true). Another attempt at fixing this was by moving around the menu.add([insert JLabel/JComboBox]).

The code that I used is as provided

Window.java

JFrame frame;
JPanel background;
JPanel menu;
JSlider difficulty;
JLabel difficultyLabel;
JComboBox exerciseChoice;
JPanel dropDownPanel;
JLabel exerciseChoiceLabel;
JPanel difficultyPanel;

public ActionListener startButtonPressed() throws IOException {
      ActionListener temp = 
         new ActionListener() {
         
            @Override
            public void actionPerformed(ActionEvent e) {
               try {
                  background.setVisible(false);
                  menu = new JPanel();
                  menu.setLayout(new BoxLayout (menu, BoxLayout.Y_AXIS));   
                  menu.setBounds(menu.getLocation().x+50, 50, 700, 100);
                  menu.setBackground(Color.WHITE);
                  startButton.setEnabled(false);
                  createDifficultySlider(); 
                  createWorkoutDropDown();
                  
                  frame.add(menu);
                  frame.setVisible(true);
               
               } catch (IOException r) {}
            }
         
         };
      return temp;
   
   }
   
   public void createWorkoutDropDown() throws IOException {
      dropDownPanel = new JPanel();
      dropDownPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
      dropDownPanel.setBackground(Color.WHITE);
      String[] choices = {"Abs", "Cardio", "Full Body", "Upper Body", "Stretches", "Lower Body"};
      exerciseChoice = new JComboBox(choices);
      exerciseChoice.setFont(new Font("Verdana", Font.PLAIN, 20));
      exerciseChoiceLabel = new JLabel("Workout Options");
      exerciseChoiceLabel.setFont(new Font("Verdana", Font.PLAIN, 20));
      dropDownPanel.add(exerciseChoiceLabel);
      dropDownPanel.add(exerciseChoice);
      menu.add(dropDownPanel);
      
      
   }
   
   public void createDifficultySlider() throws IOException {
      difficultyPanel = new JPanel();
      difficultyPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
      difficultyPanel.setBackground(Color.WHITE);
      difficulty = new JSlider(JSlider.HORIZONTAL, 1, 10, 5);
      difficulty.setPreferredSize(new Dimension(difficulty.getPreferredSize().width + 60, difficulty.getPreferredSize().height + 5));
      difficultyLabel = new JLabel("Difficulty Level (Easy 1- Hard 10)");  
      difficultyLabel.setFont(new Font("Verdana", Font.PLAIN, 20));
      difficulty.setFont(new Font("Verdana", Font.PLAIN, 10)); 
      difficulty.setMajorTickSpacing(1);
      difficulty.setPaintLabels(true);
      difficulty.setPaintTicks(true);
      difficulty.setSnapToTicks(true);
      difficultyPanel.add(difficultyLabel, BorderLayout.PAGE_START);
      difficultyPanel.add(difficulty, BorderLayout.LINE_START);
      menu.add(difficultyPanel); 
   
   }

EDIT: I had to throw IOExceptions because I was importing fonts from my drive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文