Java Swing 更改为 JButton 背景颜色不显示

问题描述 投票:0回答:1

以下程序代码是我的应用程序的精简版本。更改为 尽管 print 命令显示,但第 45、47,48 行中的背景颜色未显示 他们已经受到影响了。

如你所见,我已经尝试过 几个重绘、打包等命令都没有成功。我错过了什么?

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

public class ButtonColor extends JFrame implements ActionListener {
    public int Lifecols, Liferows;
    private JPanel zellPanel,FertigPanel;
    private JButton ChosenB;
    public JButton[][]  zellen;
    
    ButtonColor(){
    this.setLocationByPlatform(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        setCellPanel(10,10);
        FertigPanel = new JPanel();
    ChosenB  = new JButton("Auswahl bestätigen");
    ChosenB.setFont(new Font("Serif", Font.BOLD, 20));
    ChosenB.setPreferredSize(new Dimension(600, 50));
    ChosenB.addActionListener(this);
    FertigPanel.add(ChosenB);
       getContentPane().add(zellPanel);
       add(Box.createRigidArea(new Dimension(0,20)));
       getContentPane().add(FertigPanel);
       add(Box.createRigidArea(new Dimension(0,10)));
       pack();
       repaint();
       setVisible ( true);}
    
    public void setCellPanel(int Lifecols,int Liferows){
    JButton zelle;
    zellPanel = new JPanel();
    zellPanel.setLayout(new GridLayout(Liferows, Lifecols));
    zellPanel.setPreferredSize(new Dimension(1100, 1100));
        zellen = new   JButton[Liferows][Lifecols];
     for (int rowIndex=0; rowIndex< Liferows; rowIndex++)
        { for (int colIndex =0; colIndex < Lifecols; colIndex++)
          {zellen[rowIndex][colIndex] =  new JButton();
           zellen[rowIndex][colIndex].setBackground(Color.WHITE);
        zellPanel.add(zellen[rowIndex][colIndex]);
          }}
     return;}
    public void  SetBlinker1(){
    System.out.println(zellen[3][3].getBackground());
    zellen[3][3].setBackground(Color.BLACK);
    System.out.println(zellen[3][3].getBackground());
        zellen[3][4].setBackground(Color.BLACK);
    zellen[3][5].setBackground(Color.BLACK);
    zellen[3][3].repaint();
    zellPanel.repaint();
    getContentPane().repaint();
    pack();
    return;}
     public void actionPerformed(ActionEvent e){
    if (e.getSource() == ChosenB) {
        setCellPanel(10,10);
        SetBlinker1();
        pack();
        revalidate();
        repaint();
        return;
    }}
    public static void main(String[] args) {
    new ButtonColor();
    }}
java swing
1个回答
0
投票

在大多数平台上,

JButton
呈现平台特定样式的按钮。这往往会忽略
background
属性。

我建议确保按钮不透明并更改按钮使用的边框...

zellen[rowIndex][colIndex].setOpaque(true);
zellen[rowIndex][colIndex].setBorder(new LineBorder(Color.BLACK));

在大多数情况下,这将确保您已覆盖“平台样式”。

nb:我调整了框架大小以使图像变小

你的下一个问题是,你在

setCellPanel(10, 10);
方法中调用
actionPerformed
。这导致了各种各样的问题。如果您必须这样做,请首先从
zellPanel
中删除所有预先存在的按钮,但是,我根本不会调用它......

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == ChosenB) {
        SetBlinker1();
    }
}

我还建议您查看Java 编程语言的代码约定。它将让您更轻松地阅读/理解其他人的代码,也让其他人更轻松地阅读/理解您的代码。

© www.soinside.com 2019 - 2024. All rights reserved.