Java 7u7 setLookAndFeel不';不行?
在Netbeans 7.2中,当我键入Java 7u7 setLookAndFeel不';不行?,java,swing,netbeans,compiler-errors,Java,Swing,Netbeans,Compiler Errors,在Netbeans 7.2中,当我键入setLookAndFeel()时它表示找不到该方法。我做错了什么 import javax.swing.*; public class SalutonFrame extends JFrame { public SalutonFrame() throws UnsupportedLookAndFeelException { super("Saluton Mondo!"); setLookAndFeel();
setLookAndFeel()时代码>它表示找不到该方法。我做错了什么
import javax.swing.*;
public class SalutonFrame extends JFrame {
public SalutonFrame() throws UnsupportedLookAndFeelException {
super("Saluton Mondo!");
setLookAndFeel();
setSize(350, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
要设置框架的外观,必须在构造函数之前通过UIManager进行设置。你可以这样做:
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
JFrame someFrame = new JFrame();
或者,无论您希望使用什么样的类来代替示例中所示的基本Java外观。有关更多信息,请参阅
…找不到该方法
与方法getPonyRide()
非常相似。如果我们构造的方法未在类或它扩展的任何类中定义,编译器将通知它们不存在
如果您键入类似..的内容,IDE通常会显示一个下拉菜单
instanceOfObject.
…或
this.
键入
时(或几分钟后,取决于开发框的速度),应显示可能的方法和属性列表。在选择一种之前,请仔细查看各种可能性
建议
不要扩展框架,只需引用一个。喜欢
不要调用setSize()
,而是在添加所有组件后调用pack()
使用JFrame.DISPOSE ON\u CLOSE
进行退出操作
看起来你在读“Sams 24小时自学Java,第6版?”
无论如何,您可以这样做:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class SalutonFrame extends JFrame {
public SalutonFrame() throws UnsupportedLookAndFeelException {
super("Saluton Mondo!");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
JFrame someFrame = new JFrame();
setSize(350, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch (Exception exc) {
// error handling
}
}
}
import javax.swing.*;
public class SalutonFrame extends JFrame {
public SalutonFrame() {
super("Saluton Frame");
setLookAndFeel();
setSize(350, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception exc) {
// error handling
}
}
public static void main(String[] args) {
SalutonFrame sal = new SalutonFrame();
}
}
或者你可以这样做:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class SalutonFrame extends JFrame {
public SalutonFrame() throws UnsupportedLookAndFeelException {
super("Saluton Mondo!");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
JFrame someFrame = new JFrame();
setSize(350, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch (Exception exc) {
// error handling
}
}
}
import javax.swing.*;
public class SalutonFrame extends JFrame {
public SalutonFrame() {
super("Saluton Frame");
setLookAndFeel();
setSize(350, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception exc) {
// error handling
}
}
public static void main(String[] args) {
SalutonFrame sal = new SalutonFrame();
}
}
另外,请注意,JFrame
本身并不是一个JComponent
。除了主机平台提供的组件外,它没有任何外观和感觉。很抱歉,但我不太理解您的第一个建议。您似乎在暗示,将类作为JFrame
的实例是不好的。对我来说,这不太麻烦至少,这比反复引用框架要好得多。@fireshadow52如果框架没有自定义方法,则不应扩展它。另一种表达方式是“首选”。要降低“麻烦”因素,请使用IDE。