2017-09-17 339 views
2

更改顏色

public ATMgui1() 
{ 
    setTitle("ATM Transactions"); 
    setSize(WIDTH, HEIGHT); 
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    JPanel contentPane = new JPanel(); 

    contentPane.setBackground(Color.PINK); 
    setContentPane(contentPane); 
    contentPane.setOpaque(false); 

    JLabel pinLabel = new JLabel("Enter your Pin:"); 
    pinLabel.setOpaque(false); 
    pinTextField = new JTextField(); 
    JButton pinButton = new JButton("EnterPin OK"); 
    pinButton.setOpaque(false); 

    JLabel pinChangeLabel = new JLabel("Enter your new Pin:"); 
    JTextField pinChangeTextField = new JTextField(); 
    JButton pinChangeButton = new JButton("Change Pin"); 

    JButton exitButton = new JButton("EXIT"); 
    exitButton.addActionListener(e -> this.dispose()); 

    pinButton.addActionListener(this); 

    JPanel pinPanel = new JPanel(); 
    pinPanel.setLayout(new GridLayout(3, 3)); 
    pinPanel.add(pinLabel); 
    pinPanel.add(pinTextField); 
    pinPanel.add(pinButton); 
    pinPanel.add(pinChangeLabel); 
    pinPanel.add(pinChangeTextField); 
    pinPanel.add(pinChangeButton); 

    pinPanel.add(exitButton); 

    contentPane.add(pinPanel, BorderLayout.CENTER); 

我試圖改變的背景,但它沒有徹底改變,代碼是有點所有的地方,我認爲這只是一個部分,其實就是我掙扎。

+2

此外,您試圖將影像分享的僅僅是在您的計算機上,因此它不能被其他人查看... –

+1

你需要發佈[mcve],以便我們可以看到代碼無法工作的原因。請幫助我們幫助你。 –

+0

@JohnD:css在這方面確實沒有一席之地。 –

回答

3

你看到什麼:

enter image description here

JPanel的持有你的組件,你的JTextField中,標籤和按鈕應該由非不透明的。打電話給.setOpaque(false),你應該看到它下面的顏色。

例如:

import java.awt.Color; 

import javax.swing.BorderFactory; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class PinkBackground { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> { 
      JPanel panel = new JPanel(); 
      panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); 
      panel.add(new JTextField(10)); 
      panel.add(Box.createVerticalStrut(15)); 
      panel.add(new JTextField(10)); 
      panel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15)); 

      // panel.setOpaque(false); // ******* uncomment this! ********** 

      JFrame frame = new JFrame(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.getContentPane().setBackground(Color.PINK); 
      frame.add(panel); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
     }); 
    } 
} 

讓您pinPanel非不透明。的contentPane,那你設置粉色應保持不透明組件:

public ATMgui1() { 

    setTitle("ATM Transactions"); 
    setSize(WIDTH, HEIGHT); 
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    JPanel contentPane = new JPanel(); 

    contentPane.setBackground(Color.PINK); 
    setContentPane(contentPane); 

    JLabel pinLabel = new JLabel("Enter your Pin:"); 
    // pinLabel.setOpaque(false); 
    pinTextField = new JTextField(); 
    JButton pinButton = new JButton("EnterPin OK"); 
    // pinButton.setOpaque(false); 

    JLabel pinChangeLabel = new JLabel("Enter your new Pin:"); 
    JTextField pinChangeTextField = new JTextField(); 
    JButton pinChangeButton = new JButton("Change Pin"); 

    JButton exitButton = new JButton("EXIT"); 
    exitButton.addActionListener(e -> this.dispose()); 

    pinButton.addActionListener(this); 

    JPanel pinPanel = new JPanel(); 
    pinPanel.setOpaque(false); // !! 
    pinPanel.setLayout(new GridLayout(3, 3)); 
    pinPanel.add(pinLabel); 
    pinPanel.add(pinTextField); 
    pinPanel.add(pinButton); 
    pinPanel.add(pinChangeLabel); 
    pinPanel.add(pinChangeTextField); 
    pinPanel.add(pinChangeButton); 

    pinPanel.add(exitButton); 

    contentPane.add(pinPanel, BorderLayout.CENTER); 
} 
+0

我試過了:JLabel pinLabel = new JLabel(「Enter your Pin:」); pinLabel.setOpaque(false); pinTextField = new JTextField(); JButton pinButton = new JButton(「EnterPin OK」); pinButton.setOpaque(false);它沒有做任何事情 – LiyanA

+1

@LiyanA:不,這不是我告訴你要做的。而是包含所有這些的JPanel。我們已經要求你的[mcve]代碼,但是你還沒有向我們展示它,所以沒人能告訴你你做錯了什麼。請修復。有關MCVE的示例,請參閱上面發佈的代碼。 –

+0

@LiyanA:讓你的pinPanel不透明,而不是contentPane。 –