java - 在同一帧上显示多个 JLabel

标签 java swing user-interface layout-manager cardlayout

  JFrame frame = new JFrame("Picture");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  display = new JPanel();      
if(event.getSource().equals(birthday)){
    background = new JLabel(bday);
    display.add(background);

  }
  else if(event.getSource().equals(cake)){
    picture = new JLabel(pastry, SwingConstants.LEFT);
    display.add(picture);
  }
  else if(event.getSource().equals(input)){
    word = new JLabel(text);
    word.setHorizontalTextPosition(SwingConstants.RIGHT);
    word.setVerticalTextPosition(SwingConstants.CENTER);
    display.add(word);
  }
  frame.setPreferredSize (new Dimension(450, 350));
  frame.getContentPane().add(display);
  frame.pack();
  frame.setVisible(true);

这是我的独立类中 ActionListener 类的一部分。我有一个组合框/卡片布局。因此,当我单击一个组合框标签中的一个按钮(蛋糕)和另一个组合框标签中的另一个按钮(生日)时,会出现两个框架。我希望这些内容位于同一框架上,但我不知道该怎么做。

最佳答案

而不是每次调用操作执行方法时创建框架的新实例,您需要创建一个共享实例...

enter image description here

public class OneFrameToRuleThemAll {

    public static void main(String[] args) {
        new OneFrameToRuleThemAll();
    }

    public OneFrameToRuleThemAll() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public static class TestPane extends JPanel {

        private JFrame frame;

        private static final String FRUIT[] = new String[] {
            "Banana",
            "Apple",
            "Manga",
            "Pear"
        };

        public TestPane() {

            JButton button = new JButton("Fruit");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    if (frame == null) {
                        frame = new JFrame("Fruits basket");
                        frame.setSize(100, 200);
                        frame.setLayout(new GridLayout(0, 1));
                        frame.setLocationRelativeTo(TestPane.this);
                        frame.setVisible(true);
                    }

                    int index = (int)Math.round(Math.random() * (FRUIT.length - 1));
                    frame.add(new JLabel(FRUIT[index]));
                    frame.getContentPane().validate();

                }
            });

            setLayout(new GridBagLayout());
            add(button);

        }

    }

}

关于java - 在同一帧上显示多个 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14300001/

相关文章:

java - 在没有 Apache POI 的情况下解析 Excel 文件

java - 使用java编写两个方法min和max来查找链表中的最大值和最小值,但输入列表是整数数组

java - 在 javax.swing 应用程序中捕获异常

java - KeyHandler 不响应按键事件

user-interface - 编写确认(是/否)对话框

java - 如何将 JButton 添加到 Canvas()?或者如何将 JButton 添加到 Panel() 并使 Panel() 背景透明?

java - 数据截断: Data too long for column 'sql_stmt' at row 1

java - ScheduledThreadPoolExecutor线程run方法执行final block 而不完成try block

java - 如何更改 JTabbedPane 的背景颜色?

JavaFX 包组件