1

我想在 netbeans 中的 Java 应用程序中添加一个状态栏。

我用谷歌搜索了一下,发现了这篇文章:

如何在 Java 应用程序的底部创建一个栏,例如状态栏?

我在那篇文章中做了同样的事情,但我有一个错误。

这是我试过的代码:

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

4

3 回答 3

0
 PersonelMainForm.add(statusPanel, BorderLayout.SOUTH);

在这里,您尝试使用static add不存在的方法(请记住,以大写字母开头的标识符必须是类)。从您的代码中,您似乎已经创建了一个实例,实例的大小写正确(小写),因此只需将其更改为:

 personelMainForm.add(statusPanel, BorderLayout.SOUTH);

对另一个错误执行相同的操作。

请记住,在 java 中大写很重要(大写和小写标识符不相同)。如果它是,则只能从类标识符调用方法static

于 2013-05-24T11:31:00.420 回答
0

考虑在 NetBeans 平台(基于 Swing 的 RCP)之上构建您的应用程序。它带有 StatusBar 支持等等:

https://netbeans.org/features/platform/

http://wiki.netbeans.org/BookNBPlatformCookbookCH0211

于 2013-05-24T11:33:49.593 回答
0

你只是打错pP

改变

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

personelMainForm.add(/* ... */)
personelMainForm.getWidth()
于 2013-05-24T11:34:29.057 回答