gpt4 book ai didi

java - 更改 ContentPane 的背景颜色

转载 作者:行者123 更新时间:2023-12-02 05:57:37 25 4
gpt4 key购买 nike

我正在开发 GUI,并且在 Pane 方面没有遇到任何问题。
我的 GUI 分为两部分(topPanebottomPane)。

我的两个 Pane 上都有按钮和标签,但其中一个按钮功能我想更改背景颜色,但它没有完成这项工作。

我所做的是使用Container(称为thisContentPane)来更改整个 GUI 的背景颜色。

这是我当前的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TempConverter extends JFrame
{
//Creating a contentPane down inside the inner class
Container thisContentPane;
//class scope variables : DO NOT CREATE THIS OBJECTS HERE.
JButton calculateButton, clearButton;
JTextField celsiusField, fahrenheitField, kelvinField;

//menu
JMenuBar menuBar = new JMenuBar();
JMenu backgroundColor = new JMenu("Background Color");
JMenu help = new JMenu("Help");

JMenuItem lightGray, white, black, blue, howToUse, about;


//constructor
TempConverter()
{
super("Temperature Converter App");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setSize(400,200);;
this.setLocationRelativeTo(null);


//menuBar
this.setJMenuBar(menuBar);


menuBar.add(backgroundColor);

//adding JMenu to JMenuBar
menuBar.add(backgroundColor);
menuBar.add(help);

//adding JMenuItems
lightGray = backgroundColor.add("LIGHTGRAY");
white = backgroundColor.add("WHITE");
black = backgroundColor.add("BLACK");
blue = backgroundColor.add("BLUE");

howToUse = help.add("How To Use");
about = help.add("Help");


//babysitter
MaryPoppins babysitter = new MaryPoppins();

//adding action listener to the menu item
lightGray.addActionListener(babysitter);
white.addActionListener(babysitter);
black.addActionListener(babysitter);
blue.addActionListener(babysitter);
howToUse.addActionListener(babysitter);
about.addActionListener(babysitter);


//building JPanels
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(3,2,0,20));

//add this to JFrame in centerzone
this.add(topPanel, BorderLayout.CENTER);

//bottom panel
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout());

//add this to JFrame in bottom
this.add(bottomPanel, BorderLayout.SOUTH);

//add components to the panels
//add the buttons
calculateButton = new JButton("Calculate");
clearButton = new JButton("Clear");

//add buttons
bottomPanel.add(calculateButton);
bottomPanel.add(clearButton);

//register listeners
calculateButton.addActionListener(babysitter);
clearButton.addActionListener(babysitter);

//add components to the top panel
JLabel labelOne = new JLabel("Celsius:");
JLabel secondOne = new JLabel("Fahrenheit:");
JLabel thirdOne = new JLabel("Kelvin:");

celsiusField = new JTextField("");
fahrenheitField = new JTextField("");
kelvinField = new JTextField("");

//add the label and text fields
topPanel.add(labelOne);
topPanel.add(celsiusField);
topPanel.add(secondOne);
topPanel.add(fahrenheitField);
topPanel.add(thirdOne);
topPanel.add(kelvinField);

this.setVisible(true);

} // end constructor

public static void main (String[] args) {
new TempConverter();
}


private class MaryPoppins implements ActionListener
{

//implement the abstract method from the interface
public void actionPerformed(ActionEvent ev)
{
thisContentPane = getContentPane();

if(ev.getActionCommand().equals("LIGHTGRAY"))
{
thisContentPane.setBackground(Color.lightGray);
}
else if (ev.getActionCommand().equals("BLUE"))
{
thisContentPane.setBackground(Color.BLUE);
}
else if(ev.getActionCommand().equals("WHITE") )
{
thisContentPane.setBackground(Color.WHITE);
}
else if (ev.getActionCommand().equals("BLACK"))
{
thisContentPane.setBackground(Color.BLACK);
}else if (ev.getActionCommand().equals("Clear"))
{
thisContentPane.setBackground(Color.BLACK);
}
else if (ev.getActionCommand().equals("BLACK"))
{
thisContentPane.setBackground(Color.BLACK);
}



}//end ActionPerformed()

}//end inner class

} // end class

当我单击按钮或菜单项时,它不会执行任何操作。

最佳答案

你的问题是你的contentPanel的背景颜色不是“可见”:您的 topPanel和你的bottomPanel就在它上面:)

你应该这样做:

if (ev.getActionCommand().equals("LIGHTGRAY")) {
thisTopPanel.setBackground(Color.lightGray);
thisBottemPanel.setBackground(Color.lightGray);
}

...并为您的每个 if 执行此操作条件(你知道我的意思)。

但这并不是最好的方法。在我看来,另一种选择非常有意义,因为它反射(reflect)了您正在寻找的确切行为,即:

topPanel.setOpaque(false);
bottomPanel.setOpaque(false);

我显然会推荐第二种选择;)

<小时/>

此外,由于我正在这样做,所以我更喜欢使用 Color.LIGHTGRAY (以及 Color.BLACKColor.WHITE 等)而不是 Color.lightGrey ,因为这些别名遵循常量必须为大写的约定

关于java - 更改 ContentPane 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22950096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com