【问题标题】:Problem: The use of JOptionPane stops my JPanels from switching问题:使用 JOptionPane 会阻止我的 JPanel 切换
【发布时间】:2020-06-21 21:04:53
【问题描述】:

所以这段代码的目的是使用 JOptionPane 获取用户输入,验证它,确保输入是“Hello World”,如果是,则从红色 JPanel 切换到带有 JLabel Hello 的空白世界之巅。但是,当在 JPanel 的文本字段中输入“Hello World”并单击 OK 时,红色的 JPanel 会被移除,但带有 JLabel 的空白的 JPanel 不会出现。我使用 isDisplayable() 方法检查是否显示带有 JLabel 的空白 JPanel 并且结果为 true。我不确定问题是什么或如何解决。这是我的代码:

import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import javax.sound.sampled.Line;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;

import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

public class Main {

    private static JFrame frame = new JFrame();
    private static JPanel colorPanel = new JPanel();
    private static JPanel testPanel = new JPanel();
    private static JLabel testLabel = new JLabel("Hello World");

    public static void main(String[] args) {
        
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setResizable(false);
        colorPanel.setBackground(Color.RED);
        frame.add(colorPanel);

        frame.setVisible(true);
        String input = JOptionPane.showInputDialog("Enter");
        if(input.equals("Hello World"))
        {
            frame.remove(colorPanel);
            frame.validate();
            frame.repaint();
            testPanel.add(testLabel);
            frame.add(testPanel);
        }

    }
}

【问题讨论】:

  • 最好使用 CardLayout

标签: java swing jpanel joptionpane


【解决方案1】:

我知道您的答案已被接受,但我必须插话,因为我觉得虽然答案在技术上是正确的,但它可能不是最佳解决方案。

请注意,首先,您的问题与 .validate() 与 .revalidate() 的关系不大,而与您调用它的位置有关。它必须在所有更改完成后调用,这意味着在所有组件都被删除和添加之后。

还请注意,在您的容器上使用 repaint() 并在调用 revalidate() 之后调用它通常是件好事,尤其是在您删除组件时,因为这将有助于清除任何潜在的“脏”像素在您的 GUI 中。

现在我回答的重点是:最好不要推出自己的解决方案,而是使用 GUI 库为您提供的更高级别的工具,这里是 CardLayout(教程:CardLayout Tutorial)。使用此工具,您可以轻松地在“卡片”组件中向前或向后滚动,此处 JPanel 使用 .previous(...) 和 .next(...) 方法,或者通过调用 .show(...) 方法以灵活的方式交换组件。布局还将调整使用该布局并保存“卡片”组件的容器的大小,以确保所有卡片都适合。

.next(...)、.previous(...) 和 .show(...) 的使用示例可以在下面的可编译和可运行代码中看到。另外,请看一下 cmets:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.*;

public class SwapPanels extends JPanel {
    private static final long serialVersionUID = 1L;
    private CardLayout cardLayout = new CardLayout(); // the layout
    private JPanel cardHolderPanel = new JPanel(cardLayout); // the JPanel that holds the cards
    
    // a combo box and model for allowing random-access swapping
    private DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>();
    private JComboBox<String> swapPanelCombo = new JComboBox<>(comboModel);

    public SwapPanels() {
        // listener added to combobox to allow swapping of panels
        swapPanelCombo.addActionListener(e -> swapPanel());
        
        JButton previousBtn = new JButton("Previous");
        previousBtn.addActionListener(e -> cardLayout.previous(cardHolderPanel));
        
        JButton nextBtn = new JButton("Next");
        nextBtn.addActionListener(e -> cardLayout.next(cardHolderPanel));
        
        // panel to hold buttons and combo box
        JPanel controlPanel = new JPanel();
        controlPanel.add(previousBtn);
        controlPanel.add(nextBtn);
        controlPanel.add(swapPanelCombo);
        
        // create and add our "card" JPanels, showing different colors 
        addColorPanel("White", Color.WHITE);
        addColorPanel("Red", Color.RED);
        addColorPanel("Orange", Color.ORANGE);
        addColorPanel("Yellow", Color.YELLOW);
        addColorPanel("Green", Color.GREEN);
        addColorPanel("Blue", Color.BLUE);
        addColorPanel("Cyan", Color.CYAN);
        
        // add components to the main *this* JPanel
        setPreferredSize(new Dimension(600, 450));
        setLayout(new BorderLayout());
        add(cardHolderPanel);
        add(controlPanel, BorderLayout.PAGE_END);
    }

    private void addColorPanel(String text, Color color) {
        // create color JPanel
        JPanel colorPanel = new JPanel();
        colorPanel.setBackground(color);

        // label to display color name
        JLabel label = new JLabel(text, SwingConstants.CENTER);
        
        // make label big
        label.setFont(label.getFont().deriveFont(Font.BOLD, 24f));
        
        // place label in the center of the color JPanel
        colorPanel.setLayout(new GridBagLayout());
        colorPanel.add(label);

        // add text to your jcombo box as well
        comboModel.addElement(text);

        // add color panel to the card-holder JPanel
        cardHolderPanel.add(colorPanel, text);
    }
    
    private void swapPanel() {
        // get combo box's text
        String text = (String) swapPanelCombo.getSelectedItem();
        if (text != null) {
            // use the text to swap jpanels
            cardLayout.show(cardHolderPanel, text);
        }
    }

    // be sure to show our GUI on the Swing event thread
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        SwapPanels mainPanel = new SwapPanels();
        JFrame frame = new JFrame("Swap Panels");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

【讨论】:

    【解决方案2】:

    frame.validate() 应该是 frame.revalidate() 并且应该在所有组件添加/删除之后发生"

    if(input.equals("Hello World"))
    {
        frame.remove(colorPanel);
        testPanel.add(testLabel);
        frame.add(testPanel);
        frame.revalidate();
    }
    

    【讨论】:

    • 这是对错误问题的正确答案。请参阅other answer,了解针对这种情况设计的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2016-02-13
    • 2015-07-03
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多