我收到此错误无法使用此段代码从Component类型对非静态方法setBackground(Color)进行静态引用;
public class ColourChoice {
private static JPanel myContentPane = new JPanel();
public static void main(String args[]) {
Object[] colourchoice = {"Default", "Orange"};
String UserInput = (String) JOptionPane.showInputDialog(null, "Please choose a colour","ColourChoice",JOptionPane.PLAIN_MESSAGE, null,colourchoice, "Default");
if (UserInput.equals("Orange")) myContentPane.setBackground(Color.ORANGE);
JFrame window = new JFrame();
window.setVisible(true);
window.setSize(100, 100);
JPanel myContentPane = new JPanel();
window.setContentPane(myContentPane);
}
}
我有一个JFrame,里面我有一个JPanel,并且有框架和面板加载,用户可以从下拉菜单中选择一种颜色,该颜色将出现在实际窗口加载之前,但是,当我编写颜色代码时'Panel.setBackground(Color.Orange);'它给了我上面的错误。
答案 0 :(得分:2)
==
来比较字符串,而是使用.equals(...)
方法。正如本网站已经讨论过的那样,==
检查一个对象是否与另一个对象相同;它检查身份,而equals(或equalsIgnoreCase)方法检查字符串是否具有相同顺序的相同字符,这是您感兴趣的。java.AWT.Panel
上调用您的方法。请注意,变量名称应以小写字母开头。无论如何要这样做是为了避免让自己和他人混淆。 修改强>
关于您的更新代码,您的问题出在此处:
// likely in some constructor or method
Object[] colourchoice = {"Default", "Orange"};
String UserInput = (String) JOptionPane.showInputDialog(null, "Please choose a colour","ColourChoice",JOptionPane.PLAIN_MESSAGE, null,colourchoice, "Default");
if (UserInput == "Orange") Panel.setBackground(Color.ORANGE);
GUIDesign window = new GUIDesign();
window.setVisible(true);
window.setSize(100, 100);
JPanel Panel = new JPanel(); // ***** here
window.setContentPane(Panel);
问题:
==
比较字符串后得到建议不要这样做 - 为什么?如,
public class MyGui {
private JPanel myContentPane = new JPanel();
MyGui() {
// add myContentPane to the GUI
}
现在你可以在课堂的任何地方使用myContentPane。
编辑2
例如......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ColorBackground {
private static final int PREF_W = 600;
private static final int PREF_H = 450;
// my main JPanel. I declare it in the class
private JPanel mainPanel = new JPanel() {
public Dimension getPreferredSize() {
// so kleopatra doesn't down-vote me
return ColorBackground.this.getPreferredSize();
};
};
private JComboBox colorBox = new JComboBox(ColorChoices.values());
public ColorBackground() {
mainPanel.add(colorBox);
colorBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ColorChoices choice = (ColorChoices) colorBox.getSelectedItem();
mainPanel.setBackground(choice.getColor()); // now I can call methods on the field
}
});
}
public JComponent getMainPanel() {
return mainPanel;
}
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Color Background");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ColorBackground().getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum ColorChoices {
DEFAULT("Default", null), ORANGE("Orange", Color.orange), BLUE("Blue",
Color.blue), HOT_PINK("Hot Pink", new Color(255, 64, 128));
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
public Color getColor() {
return color;
}
private ColorChoices(String name, Color color) {
this.name = name;
this.color = color;
}
private String name;
private Color color;
}
编辑3
关于您的最新代码更新: