gpt4 book ai didi

java - 在 GUI 上更改颜色

转载 作者:行者123 更新时间:2023-11-30 07:52:04 25 4
gpt4 key购买 nike

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);

我尝试更改背景,但并没有完全改变,代码到处都是,我认为只有一个部分才是我正在努力解决的问题。

最佳答案

你看到了什么:

enter image description here

包含组件、JTextFields、标签和按钮的 JPanel 应该是透明的。对其调用 .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);
}

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

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