Java 如何更改JLabel的背景色?
以下是我的节目:Java 如何更改JLabel的背景色?,java,swing,background,jlabel,setbackground,Java,Swing,Background,Jlabel,Setbackground,以下是我的节目: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FinalsLabExam extends JFrame implements ActionListener, MouseListener { JLabel labelMc = new JLabel("McElvin R. Liceralde", SwingConstants.CENTER);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FinalsLabExam extends JFrame implements ActionListener,
MouseListener {
JLabel labelMc = new JLabel("McElvin R. Liceralde", SwingConstants.CENTER);
String[] fNames = { "arial", "calibri", "tahoma", "verdana", "broadway" };
JComboBox cfNames = new JComboBox(fNames);
JCheckBox bold, italic;
Font f = new Font("Times New Roman", Font.BOLD, 35);
Font font1 = new Font("Arial", Font.BOLD, 35);
Font font2 = new Font("Calibri", Font.BOLD, 35);
Font font3 = new Font("Tahoma", Font.BOLD, 35);
Font font4 = new Font("Verdana", Font.BOLD, 35);
Font font5 = new Font("Broadway", Font.BOLD, 35);
JMenuBar menubar = new JMenuBar();
JMenu mFormat, mFont, mFont2, mFontName, mFontSize, mColor, mColor2, mText,
mBColor;
JMenuItem miArial, miCalibri, miTahoma, miVerdana, miBroadway, miBlue,
miGreen, miRed, miYellow, miBlack, miOrange;
JPopupMenu pumenu = new JPopupMenu();
JToolBar tbar = new JToolBar();
public FinalsLabExam() {
mFormat = new JMenu("Format");
mFont = new JMenu("Font");
mFont2 = new JMenu("Font");
mFontName = new JMenu("Font Name");
mFontSize = new JMenu("Font Size");
mColor = new JMenu("Color");
mColor2 = new JMenu("Color");
mText = new JMenu("Text");
mBColor = new JMenu("Background");
mFormat.setMnemonic('f');
mFont.setMnemonic('f');
mFont2.setMnemonic('f');
mFontName.setMnemonic('n');
mFontSize.setMnemonic('s');
mColor.setMnemonic('c');
mColor2.setMnemonic('c');
mText.setMnemonic('t');
mBColor.setMnemonic('b');
miArial = new JMenuItem("Arial", 'a');
miCalibri = new JMenuItem("Calibri", 'c');
miTahoma = new JMenuItem("Tahoma", 't');
miVerdana = new JMenuItem("Verdana", 'v');
miBroadway = new JMenuItem("Broadway", 'w');
// miBlue = new JMenuItem("Blue", 'l');
miBlue = new JMenuItem((new ImageIcon("blue.jpg")) + "Blue", 'b');
miGreen = new JMenuItem("Green", 'g');
miRed = new JMenuItem("Red", 'r');
miYellow = new JMenuItem("Yellow", 'y');
miBlack = new JMenuItem("Black", 'k');
miOrange = new JMenuItem("Orange", 'o');
setJMenuBar(menubar);
menubar.add(mFormat);
mFormat.add(mFont);
mFormat.add(mFont2);
mFont.add(mFontName);
mFont2.add(mFontName);
mFontName.add(miArial);
mFontName.add(miCalibri);
mFontName.add(miTahoma);
mFontName.add(miVerdana);
mFontName.add(miBroadway);
mFont.add(mFontSize);
mFont2.add(mFontSize);
mFormat.add(mColor);
mFormat.add(mColor2);
mColor.add(mText);
mColor2.add(mText);
mText.add(miBlue);
mText.add(miGreen);
mText.add(miRed);
mColor.add(mBColor);
mColor2.add(mBColor);
mBColor.add(miYellow);
mBColor.add(miBlack);
mBColor.add(miOrange);
miArial.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2));
miCalibri.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 2));
miTahoma.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, 2));
miVerdana.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2));
miBroadway.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 2));
miBlue.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, 2));
miGreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 2));
miRed.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, 2));
miYellow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 2));
miBlack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, 2));
miOrange.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2));
addMouseListener(this);
miArial.addActionListener(this);
miCalibri.addActionListener(this);
miTahoma.addActionListener(this);
miVerdana.addActionListener(this);
miBroadway.addActionListener(this);
miBlue.addActionListener(this);
miGreen.addActionListener(this);
miRed.addActionListener(this);
miYellow.addActionListener(this);
miBlack.addActionListener(this);
miOrange.addActionListener(this);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.setOpaque(true);
labelMc.setBounds(350, 400, 600, 100);
pane.add(labelMc);
labelMc.setFont(f);
pane.add(tbar, BorderLayout.NORTH);
tbar.add(mFont);
tbar.add(mColor);
pumenu.add(mFont);
pumenu.add(mColor);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
setTitle("Finals Laboratory Exam");
setSize(500, 200);
setLocation(500, 300);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == miArial)
labelMc.setFont(font1);
else if (e.getSource() == miCalibri)
labelMc.setFont(font2);
else if (e.getSource() == miTahoma)
labelMc.setFont(font3);
else if (e.getSource() == miVerdana)
labelMc.setFont(font4);
else if (e.getSource() == miBroadway)
labelMc.setFont(font5);
else if (e.getSource() == miBlue)
labelMc.setForeground(Color.BLUE);
else if (e.getSource() == miGreen)
labelMc.setForeground(Color.GREEN);
else if (e.getSource() == miRed)
labelMc.setForeground(Color.RED);
else if (e.getSource() == miYellow) {
pane.setBackground(Color.YELLOW);
} else if (e.getSource() == miBlack) {
setBackground(Color.BLACK);
} else if (e.getSource() == miOrange) {
setBackground(Color.ORANGE);
}
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
pumenu.show(e.getComponent(), e.getX(), e.getY());
}
public void mousePressed(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public static void main(String[] args) {
new FinalsLabExam();
}
}
但它不会改变背景颜色:(
当我添加pane.setBackground(颜色:黄色)时
上面写着…找不到符号:(
请帮帮我:)
感谢所有回答和帮助我的人。这是我第一次在这里发布问题:)您遇到了一个可变范围的问题。
窗格
变量是在构造函数内部声明的,因此仅在其声明范围(构造函数)内可见,并且对类的其余部分完全不可见。要解决此问题,请在类中,而不是在方法或构造函数中,将变量声明为字段
例如,不是:
public class FinalsLabExam {
public FinalsLabExam() {
// ...
Container pane = getContentPane():
//....
}
而是:
public class FinalsLabExam {
// declare this as a field
// declare as a JPanel, since that's what it really is
private JPanel pane;
public FinalsLabExam() {
// ...
pane = (JPanel) getContentPane(); // cast to JPanel
//....
}
请注意,
pane
不是一个JLabel,而是一个容器,这让您的问题有点困惑——您想设置的颜色是什么?JLabel还是容器?我同意@Hovercarft您只做了setBackground(黄色)
不适用于任何容器。因此,在构造函数外部对其进行解密,以便可以在其他方法中访问它,并执行pane.setBackground(颜色.黄色)代码>
这里是完整的代码,以防您发现任何问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FinalsLabExam extends JFrame implements ActionListener, MouseListener
{
JLabel labelMc = new JLabel ("McElvin R. Liceralde", SwingConstants.CENTER);
String [] fNames = {"arial", "calibri", "tahoma", "verdana", "broadway"};
JComboBox cfNames = new JComboBox(fNames);
JCheckBox bold, italic;
Font f = new Font("Times New Roman", Font.BOLD, 35);
Font font1 = new Font("Arial", Font.BOLD, 35);
Font font2 = new Font("Calibri", Font.BOLD, 35);
Font font3 = new Font("Tahoma", Font.BOLD, 35);
Font font4 = new Font("Verdana", Font.BOLD, 35);
Font font5 = new Font("Broadway", Font.BOLD, 35);
JMenuBar menubar = new JMenuBar();
JMenu mFormat, mFont, mFont2, mFontName, mFontSize, mColor, mColor2, mText, mBColor;
JMenuItem miArial, miCalibri, miTahoma, miVerdana, miBroadway, miBlue, miGreen, miRed, miYellow, miBlack, miOrange;
JPopupMenu pumenu = new JPopupMenu();
JToolBar tbar = new JToolBar();
Container pane;// = getContentPane();
public FinalsLabExam()
{
mFormat = new JMenu("Format");
mFont = new JMenu("Font");
mFont2 = new JMenu("Font");
mFontName = new JMenu("Font Name");
mFontSize = new JMenu("Font Size");
mColor = new JMenu("Color");
mColor2 = new JMenu("Color");
mText = new JMenu("Text");
mBColor = new JMenu("Background");
mFormat.setMnemonic('f');
mFont.setMnemonic('f');
mFont2.setMnemonic('f');
mFontName.setMnemonic('n');
mFontSize.setMnemonic('s');
mColor.setMnemonic('c');
mColor2.setMnemonic('c');
mText.setMnemonic('t');
mBColor.setMnemonic('b');
miArial = new JMenuItem("Arial", 'a');
miCalibri = new JMenuItem("Calibri", 'c');
miTahoma = new JMenuItem("Tahoma", 't');
miVerdana = new JMenuItem("Verdana", 'v');
miBroadway = new JMenuItem("Broadway", 'w');
//miBlue = new JMenuItem("Blue", 'l');
miBlue = new JMenuItem((new ImageIcon("blue.jpg")) + "Blue", 'b');
miGreen = new JMenuItem("Green", 'g');
miRed = new JMenuItem("Red", 'r');
miYellow = new JMenuItem("Yellow", 'y');
miBlack = new JMenuItem("Black", 'k');
miOrange = new JMenuItem("Orange", 'o');
setJMenuBar(menubar);
menubar.add(mFormat);
mFormat.add(mFont);
mFormat.add(mFont2);
mFont.add(mFontName);
mFont2.add(mFontName);
mFontName.add(miArial);
mFontName.add(miCalibri);
mFontName.add(miTahoma);
mFontName.add(miVerdana);
mFontName.add(miBroadway);
mFont.add(mFontSize);
mFont2.add(mFontSize);
mFormat.add(mColor);
mFormat.add(mColor2);
mColor.add(mText);
mColor2.add(mText);
mText.add(miBlue);
mText.add(miGreen);
mText.add(miRed);
mColor.add(mBColor);
mColor2.add(mBColor);
mBColor.add(miYellow);
mBColor.add(miBlack);
mBColor.add(miOrange);
miArial.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2));
miCalibri.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 2));
miTahoma.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, 2));
miVerdana.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2));
miBroadway.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 2));
miBlue.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, 2));
miGreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 2));
miRed.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, 2));
miYellow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 2));
miBlack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, 2));
miOrange.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2));
addMouseListener(this);
miArial.addActionListener(this);
miCalibri.addActionListener(this);
miTahoma.addActionListener(this);
miVerdana.addActionListener(this);
miBroadway.addActionListener(this);
miBlue.addActionListener(this);
miGreen.addActionListener(this);
miRed.addActionListener(this);
miYellow.addActionListener(this);
miBlack.addActionListener(this);
miOrange.addActionListener(this);
pane = getContentPane();
pane.setLayout(new BorderLayout());
//pane.setOpaque(true);
labelMc.setBounds(350,400,600,100);
pane.add(labelMc);
labelMc.setFont(f);
pane.add(tbar, BorderLayout.NORTH);
tbar.add(mFont);
tbar.add(mColor);
pumenu.add(mFont);
pumenu.add(mColor);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
setTitle("Finals Laboratory Exam");
setSize(500,200);
setLocation(500,300);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == miArial)
labelMc.setFont(font1);
else if (e.getSource() == miCalibri)
labelMc.setFont(font2);
else if (e.getSource() == miTahoma)
labelMc.setFont(font3);
else if (e.getSource() == miVerdana)
labelMc.setFont(font4);
else if (e.getSource() == miBroadway)
labelMc.setFont(font5);
else if (e.getSource() == miBlue)
labelMc.setForeground(Color.BLUE);
else if (e.getSource() == miGreen)
labelMc.setForeground(Color.GREEN);
else if (e.getSource() == miRed)
labelMc.setForeground(Color.RED);
else if (e.getSource() == miYellow)
{pane.setBackground(Color.YELLOW);}
else if (e.getSource() == miBlack)
{pane.setBackground(Color.BLACK);}
else if (e.getSource() == miOrange)
{pane.setBackground(Color.ORANGE);}
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
pumenu.show(e.getComponent(), e.getX(), e.getY());
}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public static void main(String[] args)
{
new FinalsLabExam();
}
}
选择黄色后,您将获得
请显示完整的错误信息和足够的代码,以便我们完全理解您的错误。另外,您必须通过调用myLabel.setOpaque(true)来确保JLabel是不透明的代码>如果myLabel要显示任何背景色。C:\Users\Acer\Documents\JCreator LE\MyProjects\FinalLab\src\FinalLab.java:168:错误:找不到符号{pane.setBackground(color.YELLOW);}^symbol:变量窗格位置:class FinalLab注意:C:\Users\Acer\Documents\JCreator LE\MyProjects\FinalLab\src\FinalLab.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:未选中以获取详细信息。1错误您是否执行了label.setOpaque(true)代码>?请参阅请勿在注释中发布代码或错误消息,因为它会丢失格式,无法阅读。相反,将任何新代码发布到原始问题的底部JLabel@BladeMatrix如果你得到了答案,然后考虑接受它,让其他有同样问题的人知道问题得到了解答。