Java Swing:将JLabel添加到JPanel(Java Swing: Adding a JLabel to a JPanel)

编程入门 行业动态 更新时间:2024-10-19 22:27:10
Java Swing:将JLabel添加到JPanel(Java Swing: Adding a JLabel to a JPanel)

我只是试图添加一个JLabel到现有的JPanel。 这看起来很简单,我已经搜索了所有的东西。 我认为这是对的,但标签没有出现在我的面板上。 有人看到我失踪了吗? 谢谢!

ResultsPanel myPanel = new ResultsPanel(pnlResults); //pnlResults is an existing JPanel myPanel.addLabel(pnlResults); public class ResultsPanel extends JPanel { JPanel myPanel; public ResultsPanel(JPanel thisPanel) { myPanel = thisPanel; } public void addLabel(JPanel myResults) { JLabel myLabel = new JLabel("test", JLabel.LEFT); myPanel.setLayout(new FlowLayout()); add(myLabel); } }

编辑:在回应下面的立即答复,我同意这似乎是完全矫枉过正。 我走了这条路,因为下面的代码也不会导致JLabel被添加到我的JPanel中:

JLabel myLabel = new JLabel("test"); pnlResults.add(myLabel);

我宁愿使用这些代码,所以如果您认为它更有可能工作(当然有一些修改),请随时对此进行评论。 再次感谢!

I'm simply trying to add a JLabel to an existing JPanel. This seems really simple, and I've searched all around. I think that this is right, but the label is not appearing on my panel. Anybody see what I am missing? Thanks!

ResultsPanel myPanel = new ResultsPanel(pnlResults); //pnlResults is an existing JPanel myPanel.addLabel(pnlResults); public class ResultsPanel extends JPanel { JPanel myPanel; public ResultsPanel(JPanel thisPanel) { myPanel = thisPanel; } public void addLabel(JPanel myResults) { JLabel myLabel = new JLabel("test", JLabel.LEFT); myPanel.setLayout(new FlowLayout()); add(myLabel); } }

EDIT: In response to the immediate replies below, I agree that this seems to be total overkill. I went down this path because the following code also does not result in a JLabel being added to my JPanel:

JLabel myLabel = new JLabel("test"); pnlResults.add(myLabel);

I would much rather use this code, so feel free to comment on this if you think it is more likely to work (with some modifications, of course). Thanks again!

最满意答案

这似乎是跳过篮球只是为了做一件基本的事情; 只需打电话

JLabel label = new JLabel("Test text");//initialize the label //do some stuff with label here maybe... panel.add(label);//now add it

没有必要让一个类扩展JPanel,并且包含一个JPanel; 如果一个类扩展了JPanel,为了获得JPanel实例,只需使用这个(所以addLabel会改为this.setLayout(blah))。 但是,当然不需要为JPanel创建子类,就像添加JLabel一样简单

总的来说,这几乎是最简单的挥杆应用程序:

JFrame frame = new JFrame("Basic Swing");//Make a frame frame.setSize(300, 300);//Give it a size frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//Make it go away on close JPanel panel = new JPanel();//Make a panel frame.add(panel);//Add it to your frame JLabel label = new JLabel("Hello StackOverflow!");//Make a label panel.add(label);//Add it to the panel (which is on the frame) frame.setVisible(true);//Show the frame

This seems to be jumping through hoops just to do a basic thing; simply call

JLabel label = new JLabel("Test text");//initialize the label //do some stuff with label here maybe... panel.add(label);//now add it

There is no need to make a class extends JPanel, and contain a JPanel; if a class extends JPanel, to get the JPanel instance, simply use this (so addLabel would instead do this.setLayout(blah)). But of course there is no need to even subclass JPanel for something as simple as adding a JLabel

Overall, here's pretty much the simplest swing application:

JFrame frame = new JFrame("Basic Swing");//Make a frame frame.setSize(300, 300);//Give it a size frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//Make it go away on close JPanel panel = new JPanel();//Make a panel frame.add(panel);//Add it to your frame JLabel label = new JLabel("Hello StackOverflow!");//Make a label panel.add(label);//Add it to the panel (which is on the frame) frame.setVisible(true);//Show the frame

更多推荐

本文发布于:2023-07-16 13:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swing   Java   JLabel   Adding   JPanel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!