netbeans 向Java应用程序添加状态栏

1dkrff03  于 2022-11-10  发布在  Java
关注(0)|答案(3)|浏览(418)

我想在我的Java应用程序中添加一个状态栏。
我在谷歌上搜索了一下,发现了这篇文章:
How can I create a bar in the bottom of a Java app, like a status bar?
我做了和那篇文章一样的事情,但是我有一个错误。
这是我试过的代码:

public void run() {

    PersonelMainForm personelMainForm = new PersonelMainForm();

    personelMainForm.setExtendedState(
        personelMainForm.getExtendedState()|JFrame.MAXIMIZED_BOTH );

    // create the status bar panel and shove it down the bottom of the frame
    statusPanel = new JPanel();
    statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
    PersonelMainForm.add(statusPanel, BorderLayout.SOUTH);
    statusPanel.setPreferredSize(new Dimension(PersonelMainForm.getWidth(), 16));
    statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
    JLabel statusLabel = new JLabel("status");
    statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
    statusPanel.add(statusLabel);

    personelMainForm.setVisible(true);
}

这是PersonelMainForm.add(statusPanel, BorderLayout.SOUTH);行的错误消息:
无法从静态内容指涉非静态方法add(java.awt.Component,java.lang.Object)
这是statusPanel.setPreferredSize(new Dimension(PersonelMainForm.getWidth(), 16));行的错误消息:
线程“AWT-EventQueue-0”java.lang.RuntimeException中出现异常:无法编译的源代码-不能从静态上下文中引用非静态方法getWidth()

tzdcorbm

tzdcorbm1#

PersonelMainForm.add(statusPanel, BorderLayout.SOUTH);

这里你尝试使用一个staticadd方法,这个方法并不存在(记住,以大写字母开头的标识符必须是类)。从你的代码看,你似乎已经创建了一个示例,并且对示例进行了正确的大写(小写),所以只需将其更改为:

personelMainForm.add(statusPanel, BorderLayout.SOUTH);

对另一个错误执行相同的操作。
记住在java中大小写是很重要的(大写和小写的标识符是不一样的)。你只能从类标识符调用一个方法,如果它是static

93ze6v8z

93ze6v8z2#

您只是将p错误地键入了P
变更

PersonelMainForm.add(/* ... */)
PersonelMainForm.getWidth()

结束日期

personelMainForm.add(/* ... */)
personelMainForm.getWidth()
qc6wkl3g

qc6wkl3g3#

考虑在NetBeans平台(一个基于Swing的RCP)上构建应用程序。它附带StatusBar支持和更多功能:
https://netbeans.apache.org/kb/docs/platform/
http://wiki.netbeans.org/BookNBPlatformCookbookCH0211

相关问题