如何在鼠标进入另一个 JPanel 时更改 JPanel 的背景颜色
How to change background color of a JPanel when mouse enters another JPanel
我是 java Swing 的新手,这是我的第一个问题,所以请原谅我的错误。我正在尝试制作一个具有 GridBagLayout 并包含许多较小的 Jpanel 的 JPanel(容器),它们将用作纽扣。我为每个小 JPanel 设置了一个 mouseEntered Listener,它将背景颜色更改为进入的颜色。
是否也可以更改该面板的行和列的颜色?
例如,如果我在 Cell(4,3) 中输入鼠标,我想要单元格 (4,0) 的背景
和 (0,3) 也将被更改。
这是我的容器:
public class CellPane extends JPanel{
private List<Cell> cellList;
private final GridBagConstraints gbc;
private Cell cell;
private int counter;
public CellPane(){
this.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
cellList = new ArrayList<>();
counter = 0;
for(int row=0;row<15;row++)
for(int col=0;col<31;col++){
gbc.gridx = col;
gbc.gridy = row;
cell = new Cell(col ,row);
Border border = null;
if (row < 14) {
if (col < 30) {
border = new MatteBorder(1, 1, 0, 0, new Color(153,204,204));
} else {
border = new MatteBorder(1, 1, 0, 1, new Color(153,204,204));
}
} else {
if (col < 30) {
border = new MatteBorder(1, 1, 1, 0, new Color(153,204,204));
} else {
border = new MatteBorder(1, 1, 1, 1, new Color(153,204,204));
}
}
cell.setBorder(border);
cellList.add(counter, cell);
counter++;
this.add(cell,gbc);
}
System.out.println("Count: " + this.getComponentCount());
for(Cell c: cellList){
System.out.println(c.getCellCol()+" "+c.getCellRow());
}
}
public List getCellList(){
return cellList;
}
public int getCellCount(){
return counter;
}}
这里是单元格 Class
class Cell extends JPanel implements MouseListener, PropertyChangeListener{
private final Color defaultBackground = new Color(240,240,240);
private final Color clickedColor = new Color(204,0,102);
private final Color movingColor = new Color(153,255,153);
private final int row ,col;
//private CellPane cp = new CellPane();
public Cell(int x, int y) {
this.col = x;
this.row = y;
if (col>0 & row>0){
addMouseListener(this);
}
if(col==0 | row == 0){
addPropertyChangeListener(this);
}
if (col == 0){
this.setPreferredSize(new Dimension(100,35));
}
else{
this.setPreferredSize(new Dimension(35,35));
}
}
@Override
public void mouseClicked(MouseEvent me) {
}
@Override
public void mousePressed(MouseEvent me) {
if(col>0 & row>0 & this.getBackground().equals(movingColor)){
this.setBackground(clickedColor);
}
else if(this.getBackground().equals(clickedColor)){
this.setBackground(defaultBackground);
}
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
if(col>0 & row>0 & this.getBackground().equals(defaultBackground)){
this.setBackground(movingColor);
System.out.println("You clicked on: " + col +" " +row);
System.out.println("List Size: " );
}
}
@Override
public void mouseExited(MouseEvent me) {
if(col>0 & row>0 & this.getBackground().equals(movingColor)){
this.setBackground(defaultBackground);
}
}
@Override
public void propertyChange(PropertyChangeEvent pce) {
}
public int getCellCol(){
return this.col;
}
public int getCellRow(){
return this.row;
}}
这是主要的 Class
public class ScheduleForm {
private JFrame mainFrame;
private JPanel pnlButtons;
private JPanel pnlCalendar;
private CellPane cellPane;
public ScheduleForm(){
initButtonPanel();
initCalendarPane();
initFrame();
}
private void initCalendarPane(){
pnlCalendar = new JPanel(new BorderLayout());
pnlCalendar.setPreferredSize(new Dimension(100,500));
pnlCalendar.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
cellPane = new CellPane();
pnlCalendar.add(cellPane,BorderLayout.CENTER);
}
private void initButtonPanel(){
pnlButtons = new JPanel(new BorderLayout());
pnlButtons.setPreferredSize(new Dimension(100,100));
pnlButtons.setBorder(BorderFactory.createTitledBorder("Buttons"));
}
private void initFrame(){
mainFrame = new JFrame("Schedule");
mainFrame.setLayout(new BorderLayout());
mainFrame.add(pnlButtons, BorderLayout.NORTH);
mainFrame.add(pnlCalendar,BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setLocationRelativeTo(mainFrame);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
mainFrame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PopTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
////EDW tha mpei to neo antikeimeno
ScheduleForm scheduleForm = new ScheduleForm();
}
});
}}
可能的解决方案:
- 为 Cell public 提供允许外部 类 更改背景颜色状态的方法。
- 在 Cell 的 MouseListener 中,每当鼠标进入或离开时触发 属性 变化。
- 让您的模型(如果使用推荐的干净 MVC 模式)侦听这些更改并将这些更改通知视图,以便可以更改行和列的颜色。
- 如果不使用干净的 MVC,而是使用快速而肮脏的 GUI,则让 CellPane 将 PropertyChangeListener 添加到单元格,以便通知单个单元格状态更改 WRT 鼠标 enter/leave,然后 CellPane 可以更改通过调用上面第 1 点中的 public 方法来设置行和列颜色。
我是 java Swing 的新手,这是我的第一个问题,所以请原谅我的错误。我正在尝试制作一个具有 GridBagLayout 并包含许多较小的 Jpanel 的 JPanel(容器),它们将用作纽扣。我为每个小 JPanel 设置了一个 mouseEntered Listener,它将背景颜色更改为进入的颜色。 是否也可以更改该面板的行和列的颜色? 例如,如果我在 Cell(4,3) 中输入鼠标,我想要单元格 (4,0) 的背景 和 (0,3) 也将被更改。
这是我的容器:
public class CellPane extends JPanel{
private List<Cell> cellList;
private final GridBagConstraints gbc;
private Cell cell;
private int counter;
public CellPane(){
this.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
cellList = new ArrayList<>();
counter = 0;
for(int row=0;row<15;row++)
for(int col=0;col<31;col++){
gbc.gridx = col;
gbc.gridy = row;
cell = new Cell(col ,row);
Border border = null;
if (row < 14) {
if (col < 30) {
border = new MatteBorder(1, 1, 0, 0, new Color(153,204,204));
} else {
border = new MatteBorder(1, 1, 0, 1, new Color(153,204,204));
}
} else {
if (col < 30) {
border = new MatteBorder(1, 1, 1, 0, new Color(153,204,204));
} else {
border = new MatteBorder(1, 1, 1, 1, new Color(153,204,204));
}
}
cell.setBorder(border);
cellList.add(counter, cell);
counter++;
this.add(cell,gbc);
}
System.out.println("Count: " + this.getComponentCount());
for(Cell c: cellList){
System.out.println(c.getCellCol()+" "+c.getCellRow());
}
}
public List getCellList(){
return cellList;
}
public int getCellCount(){
return counter;
}}
这里是单元格 Class
class Cell extends JPanel implements MouseListener, PropertyChangeListener{
private final Color defaultBackground = new Color(240,240,240);
private final Color clickedColor = new Color(204,0,102);
private final Color movingColor = new Color(153,255,153);
private final int row ,col;
//private CellPane cp = new CellPane();
public Cell(int x, int y) {
this.col = x;
this.row = y;
if (col>0 & row>0){
addMouseListener(this);
}
if(col==0 | row == 0){
addPropertyChangeListener(this);
}
if (col == 0){
this.setPreferredSize(new Dimension(100,35));
}
else{
this.setPreferredSize(new Dimension(35,35));
}
}
@Override
public void mouseClicked(MouseEvent me) {
}
@Override
public void mousePressed(MouseEvent me) {
if(col>0 & row>0 & this.getBackground().equals(movingColor)){
this.setBackground(clickedColor);
}
else if(this.getBackground().equals(clickedColor)){
this.setBackground(defaultBackground);
}
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
if(col>0 & row>0 & this.getBackground().equals(defaultBackground)){
this.setBackground(movingColor);
System.out.println("You clicked on: " + col +" " +row);
System.out.println("List Size: " );
}
}
@Override
public void mouseExited(MouseEvent me) {
if(col>0 & row>0 & this.getBackground().equals(movingColor)){
this.setBackground(defaultBackground);
}
}
@Override
public void propertyChange(PropertyChangeEvent pce) {
}
public int getCellCol(){
return this.col;
}
public int getCellRow(){
return this.row;
}}
这是主要的 Class
public class ScheduleForm {
private JFrame mainFrame;
private JPanel pnlButtons;
private JPanel pnlCalendar;
private CellPane cellPane;
public ScheduleForm(){
initButtonPanel();
initCalendarPane();
initFrame();
}
private void initCalendarPane(){
pnlCalendar = new JPanel(new BorderLayout());
pnlCalendar.setPreferredSize(new Dimension(100,500));
pnlCalendar.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
cellPane = new CellPane();
pnlCalendar.add(cellPane,BorderLayout.CENTER);
}
private void initButtonPanel(){
pnlButtons = new JPanel(new BorderLayout());
pnlButtons.setPreferredSize(new Dimension(100,100));
pnlButtons.setBorder(BorderFactory.createTitledBorder("Buttons"));
}
private void initFrame(){
mainFrame = new JFrame("Schedule");
mainFrame.setLayout(new BorderLayout());
mainFrame.add(pnlButtons, BorderLayout.NORTH);
mainFrame.add(pnlCalendar,BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setLocationRelativeTo(mainFrame);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
mainFrame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PopTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
////EDW tha mpei to neo antikeimeno
ScheduleForm scheduleForm = new ScheduleForm();
}
});
}}
可能的解决方案:
- 为 Cell public 提供允许外部 类 更改背景颜色状态的方法。
- 在 Cell 的 MouseListener 中,每当鼠标进入或离开时触发 属性 变化。
- 让您的模型(如果使用推荐的干净 MVC 模式)侦听这些更改并将这些更改通知视图,以便可以更改行和列的颜色。
- 如果不使用干净的 MVC,而是使用快速而肮脏的 GUI,则让 CellPane 将 PropertyChangeListener 添加到单元格,以便通知单个单元格状态更改 WRT 鼠标 enter/leave,然后 CellPane 可以更改通过调用上面第 1 点中的 public 方法来设置行和列颜色。