【问题标题】:Displaying multiple JLabels on the same frame在同一帧上显示多个 JLabel
【发布时间】:2013-01-13 02:14:11
【问题描述】:
  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 类的一部分。我有一个组合框/卡片布局。因此,当我从一个组合框标签单击一个按钮(蛋糕),从另一个组合框标签单击另一个按钮(生日)时,出现了两个框架。我希望这些东西在同一个框架上,但我不知道该怎么做。

【问题讨论】:

  • 这取决于,是什么触发了执行的操作??
  • 那么,您只想知道如何在同一个 JPanel 上获得 2 个 JLabel?
  • @MadProgrammer,当您单击一个按钮时,一个 JLabel 应该会在框架(框架 2)中弹出。现在,按钮本身已经包含在一个框架(frame1)中。所以我所做的是在我的独立设备中创建另一个框架。我一直在尝试做的是,当我单击 frame1 上的另一个按钮时,另一个 JLabel 将显示在 frame2 上。但我一直在另一个框架(frame3)上获得该标签。 :(

标签: java swing user-interface layout-manager cardlayout


【解决方案1】:

而不是每次调用action perform方法时创建frame的新实例,你需要创建一个单独的共享实例...

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);

        }

    }

}

【讨论】:

  • 感谢您的帖子!但是,恐怕我对您的解决方案有点困惑(对 GUI 来说相对较新......)我有一个包含按钮和东西的独立类,以及一个显示该框架的驱动程序。但是,我想在单击按钮时启用另一个框架,该框架已在 ActionListener 类中定义。但是,当我单击另一个按钮时,会弹出另一个框架,其中包含不同的内容。我希望该框架的内容与第一个按钮包含在同一框架中......
  • 是的,这基本上就是我的示例所做的。而不是每次调用actionPerformed 时都创建一个新的JFrame,而是创建一个实例并使用。在我的示例中,我使用了延迟加载方法,但是您可以在实例化类时轻松创建框架,但这确实分配了您并没有真正使用的内存...
  • 那么我应该在独立的构造函数中声明并实例化一个框架吗? o-o 我一直在尝试……由于某些原因,Java 抛出了运行时错误……
  • 好的,所以我刚刚在构造函数中声明并实例化了一个框架,并在我的 actionPerformed 中添加了面板...没有错误,只是当我单击按钮时第二个框架拒绝显示任何内容.. .
猜你喜欢
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 2017-06-24
  • 2019-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多