复合组件背景/透明度

use*_*884 2 java swing

我正在开发这个组件的视觉问题.它JPanel有2个JTextFields和2个JLabel.我无法建立坚实的背景.我尝试了几种不透明/背景色组合但没有成功.

我不允许附加图像所以样本图像在这里!:

有什么帮助吗?

package javaapplication1;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class JavaApplication1 {

public static void main(String[] args) {
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {
    }
    JFrame testFrame = new JFrame("Test Frame");
    testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JDecimal decimal = new JDecimal();
    decimal.setPreferredSize(new Dimension(300, 100));
    testFrame.setLayout(null);
    testFrame.getContentPane().add(decimal);
    decimal.setSize(80, 25);
    testFrame.setLocationRelativeTo(null);
    testFrame.setSize(300, 200);
    testFrame.setVisible(true);
}

public static class JDecimal extends JPanel {

    private String currencyString = "00";
    private java.text.DecimalFormat format = (java.text.DecimalFormat) java.text.DecimalFormat.getInstance();
    JTextField integerField = new JTextField();
    JLabel comaLabel = new JLabel();
    JTextField fractionField = new JTextField();
    JLabel plusMinusLabel = new JLabel();
    JDecimalLayout JDecimalLayout = new JDecimalLayout();

    public JDecimal() {
        init();
    }

    public String getText() {
        return integerField.getText() + "." + fractionField.getText();
    }

    private void init() {
        this.setLayout(JDecimalLayout);
        this.add(integerField);
        this.add(fractionField);
        this.add(comaLabel);
        this.add(plusMinusLabel);
        integerField.setText("0");
        integerField.setHorizontalAlignment(JTextField.RIGHT);
        comaLabel.setText(format.getDecimalFormatSymbols().getDecimalSeparator() + "");
        fractionField.setText(currencyString);
        plusMinusLabel.setText("");
        format.setDecimalSeparatorAlwaysShown(true);
        // borders
        javax.swing.border.Border b = integerField.getBorder();
        integerField.setBorder(null);
        fractionField.setBorder(null);
        comaLabel.setBorder(null);
        plusMinusLabel.setBorder(null);
        this.setBorder(b);
    }

    public void reshape() {
        invalidate();
        validate();
    }

    private class JDecimalLayout implements java.awt.LayoutManager {

        public void addLayoutComponent(String name, Component comp) {
        }

        public void layoutContainer(Container parent) {
            Rectangle r = parent.getBounds();
            FontMetrics fm = parent.getFontMetrics(integerField.getFont());
            int sirinaSlova = fm.stringWidth("0");
            plusMinusLabel.setBounds(new Rectangle(r.width - sirinaSlova - 2, 2, sirinaSlova, r.height - 4));
            fractionField.setBounds(new Rectangle(r.width - sirinaSlova - 2 - sirinaSlova * 2, 2, sirinaSlova * 2 + 1, r.height - 4));
            comaLabel.setBounds(new Rectangle(r.width - 2 - sirinaSlova - sirinaSlova * 2 - sirinaSlova, 0, sirinaSlova, r.height));
            integerField.setBounds(new Rectangle(2, 2, comaLabel.getBounds().x - 2, r.height - 4));
        }

        public Dimension minimumLayoutSize(Container parent) {
            return parent.getPreferredSize();
        }

        public Dimension preferredLayoutSize(Container parent) {
            return parent.getPreferredSize();
        }

        public void removeLayoutComponent(Component comp) {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

cam*_*ckr 5

您不能只设置JLabel的背景,因为默认情况下它是非透明的.所以你需要做类似的事情:

label.setOpaque(true); 
label.setBackground(Color.WHITE);
Run Code Online (Sandbox Code Playgroud)