gpt4 book ai didi

java - CardLayout 在 Windows 上无法正确呈现

转载 作者:行者123 更新时间:2023-12-02 03:31:42 25 4
gpt4 key购买 nike

我一直在为一个新项目开发登录屏幕,在 Windows 上使用 CardLayout 时遇到了奇怪的渲染“错误”。

Mac 上的注册屏幕:SignUp Screen Mac

屏幕在我的 Mac 计算机上正确加载,但在 Windows 上,单击“注册”或单击“返回”后,它们看起来像这样。

Windows 上的相同屏幕:SignUp Screen Windows

如您所见,在 Windows 上,CardLayout 中的注册“卡”会呈现在登录“卡”上,而不会隐藏另一张卡,反之亦然,这与 Mac 上不同。

现在我的问题是,这是否是由于透明背景造成的,因此Windows认为后面的那个应该仍然可见,或者它是否可以在我每次切换时创建一张全新的“卡片”,或者只是忘记了把后面那个都藏起来?

为什么这在 Mac 上有效但在 Windows 上不起作用?

而且,我该如何解决这个问题?

我将把整个类(class)放在一起,以便您可以自己测试。

(旁注:您可能还会注意到“注册”按钮在 Windows 上显示白色按钮形状,即使它具有 btnRegister.setBorder(null); set(适用于 Mac))

完整的一个类代码:

package testing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import utilities.ComponentMover;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;

import javax.swing.JSeparator;
import javax.swing.JPasswordField;

@SuppressWarnings("serial")
public class ClientStarter extends JFrame implements ActionListener {

JPanel cards;

private int height = 450;
private int width = 700;
private int trasparancy = 90;
private int labelWidth = 400;

final static String BACK = "Back";
final static String REGISTER = "Register";

private Color textLine = Color.GRAY;
private Color textColor = Color.WHITE;
private Color tipColor = Color.GRAY;
private Color disabledTipColor = new Color(90, 90, 90);



// LOGIN //

JPanel loginCard;

public static JTextField usernameIn = new JTextField();
private JLabel userLabel = new JLabel("Username :");

public static JPasswordField passwordIn = new JPasswordField();
private JLabel passLabel = new JLabel("Password :");

private JButton btnLogin = new JButton("Login");
private JButton btnRegister = new JButton(REGISTER);
private JLabel registerLabel = new JLabel("Don't own an Account? ");

private JSeparator separatorUser = new JSeparator();
private JSeparator separatorPass = new JSeparator();

// SIGNUP //

JPanel joinCard;

public static JTextField emailNew = new JTextField();
public static JLabel newEmailLabel = new JLabel("Email : (Not Available)");

public static JTextField usernameNew = new JTextField();
public static JLabel newUserLabel = new JLabel("Username :");

public static JTextField passwordNew = new JTextField();
public static JLabel newPassLabel = new JLabel("Password :");

public static JTextField passwordNew2 = new JTextField();
public static JLabel newPassLabel2 = new JLabel("Re-enter password :");


private JButton btnSignUp = new JButton("Signup");
private JButton btnBack = new JButton(BACK);

private JSeparator separatorMailNew = new JSeparator();
private JSeparator separatorUserNew = new JSeparator();
private JSeparator separatorPassNew = new JSeparator();
private JSeparator separatorPassNew2 = new JSeparator();

public ClientStarter() {
getContentPane().setBackground(Color.GRAY);
setUndecorated(true);
setBackground(new Color(0, 0, 0, trasparancy));
setTitle("EnChant");
setSize(width, height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);

//Create the cards
// LOGIN //
Font avenir = new Font("Avenir", Font.PLAIN, 18);

loginCard = new JPanel();
loginCard.setLayout(null);

usernameIn.setBounds(348, 150, 327, 35);
usernameIn.setColumns(10);
usernameIn.setFont(avenir);
usernameIn.setBorder(null);

passwordIn.setBounds(348, usernameIn.getY() + 74, 327, 35);
passwordIn.setColumns(10);
passwordIn.setFont(avenir);
passwordIn.setBorder(null);

userLabel.setBounds(usernameIn.getX(), usernameIn.getY() - 20, labelWidth, 16);
userLabel.setFont(avenir);

passLabel.setBounds(passwordIn.getX(), passwordIn.getY() - 20, labelWidth, 16);
passLabel.setFont(avenir);

btnLogin.setBounds(348, passwordIn.getY() + 81, 327, 45);
btnLogin.addActionListener(this);

registerLabel.setBounds(btnLogin.getX(), btnLogin.getY() + btnLogin.getHeight() + 5, labelWidth, 16);
registerLabel.setFont(new Font("Avenir", Font.PLAIN, 13));
btnRegister.setBounds(btnLogin.getX() + 130, registerLabel.getY() - 1, 70, 16);
btnRegister.addActionListener(this);
btnRegister.setBorder(null);


loginCard.setBackground(new Color(0, 0, 0, trasparancy));

usernameIn.setBackground(new Color(0, 0, 0, 0));
usernameIn.setForeground(textColor);

passwordIn.setBackground(new Color(0, 0, 0, 0));
passwordIn.setForeground(textColor);


userLabel.setForeground(tipColor);
passLabel.setForeground(tipColor);


btnLogin.setForeground(new Color(70, 130, 180));
btnLogin.setBackground(Color.WHITE);

btnRegister.setForeground(new Color(70, 130, 180));
registerLabel.setForeground(tipColor);


separatorUser.setForeground(textLine);
separatorUser.setBounds(usernameIn.getX(), usernameIn.getY()+usernameIn.getHeight()-8, usernameIn.getWidth(), 6);

separatorPass.setForeground(textLine);
separatorPass.setBounds(passwordIn.getX(), passwordIn.getY()+passwordIn.getHeight()-8, passwordIn.getWidth(), 6);

loginCard.add(usernameIn);
loginCard.add(separatorUser);
loginCard.add(userLabel);
loginCard.add(passwordIn);
loginCard.add(separatorPass);
loginCard.add(passLabel);
loginCard.add(btnLogin);
loginCard.add(btnRegister);
loginCard.add(registerLabel);



// SIGNUP //
joinCard = new JPanel();
joinCard.setLayout(null);

emailNew.setBounds(348, 62, 327, 35);
emailNew.setColumns(10);
emailNew.setFont(avenir);
emailNew.setBorder(null);
emailNew.setEditable(false);

usernameNew.setBounds(348, emailNew.getY() + 74, 327, 35);
usernameNew.setColumns(10);
usernameNew.setFont(avenir);
usernameNew.setBorder(null);

passwordNew.setBounds(348, usernameNew.getY() + 74, 327, 35);
passwordNew.setColumns(10);
passwordNew.setFont(avenir);
passwordNew.setBorder(null);

passwordNew2.setBounds(348, passwordNew.getY() + 74, 327, 35);
passwordNew2.setColumns(10);
passwordNew2.setFont(avenir);
passwordNew2.setBorder(null);

//32, 106, 180, 254 : 2, 76, 150, 224

newEmailLabel.setBounds(emailNew.getX(), emailNew.getY() - 20, labelWidth, 16);
newEmailLabel.setFont(avenir);

newUserLabel.setBounds(usernameNew.getX(), usernameNew.getY() - 20, labelWidth, 16);
newUserLabel.setFont(avenir);

newPassLabel.setBounds(passwordNew.getX(), passwordNew.getY() - 20, labelWidth, 16);
newPassLabel.setFont(avenir);

newPassLabel2.setBounds(passwordNew2.getX(), passwordNew2.getY() - 20, labelWidth, 16);
newPassLabel2.setFont(avenir);


btnSignUp.setBounds(348, passwordNew2.getY() + 71, 327, 45); //335 // +81
btnSignUp.addActionListener(this);

btnBack.setBounds(btnSignUp.getX()-70, btnSignUp.getY(), 70, 45); //380
btnBack.addActionListener(this);


joinCard.setBackground(new Color(0, 0, 0, trasparancy));

emailNew.setBackground(new Color(0, 0, 0, 0));
emailNew.setForeground(textColor);

usernameNew.setBackground(new Color(0, 0, 0, 0));
usernameNew.setForeground(textColor);

passwordNew.setBackground(new Color(0, 0, 0, 0));
passwordNew.setForeground(textColor);

passwordNew2.setBackground(new Color(0, 0, 0, 0));
passwordNew2.setForeground(textColor);

newEmailLabel.setForeground(disabledTipColor);
newUserLabel.setForeground(tipColor);
newPassLabel.setForeground(tipColor);
newPassLabel2.setForeground(tipColor);

btnSignUp.setForeground(new Color(70, 130, 180));
btnBack.setBackground(Color.WHITE);


separatorMailNew.setBounds(emailNew.getX(), emailNew.getY()+emailNew.getHeight()-8, emailNew.getWidth(), 6);
separatorMailNew.setForeground(textLine);

separatorUserNew.setBounds(usernameNew.getX(), usernameNew.getY()+usernameNew.getHeight()-8, usernameNew.getWidth(), 6);
separatorUserNew.setForeground(textLine);

separatorPassNew.setBounds(passwordNew.getX(), passwordNew.getY()+passwordNew.getHeight()-8, passwordNew.getWidth(), 6);
separatorPassNew.setForeground(textLine);

separatorPassNew2.setBounds(passwordNew2.getX(), passwordNew2.getY()+passwordNew2.getHeight()-8, passwordNew2.getWidth(), 6);
separatorPassNew2.setForeground(textLine);

joinCard.add(emailNew);
joinCard.add(newEmailLabel);
joinCard.add(usernameNew);
joinCard.add(newUserLabel);
joinCard.add(passwordNew);
joinCard.add(newPassLabel);
joinCard.add(passwordNew2);
joinCard.add(newPassLabel2);
joinCard.add(btnSignUp);
joinCard.add(btnBack);
joinCard.add(separatorMailNew);
joinCard.add(separatorUserNew);
joinCard.add(separatorPassNew);
joinCard.add(separatorPassNew2);



// End //

JPanel whiteRectLogin = new JPanel();
whiteRectLogin.setBackground( Color.WHITE );
whiteRectLogin.setBounds(0, 0, 250, height);
loginCard.add(whiteRectLogin);

JPanel whiteRectJoin = new JPanel();
whiteRectJoin.setBackground( Color.WHITE );
whiteRectJoin.setBounds(0, 0, 250, height);
joinCard.add(whiteRectJoin);



cards = new JPanel(new CardLayout());
cards.setBackground(new Color(0, 0, 0, trasparancy));

cards.add(loginCard, BACK);
cards.add(joinCard, REGISTER);

getContentPane().add(cards);

//Top, Left, bottom, right
ComponentMover cm = new ComponentMover(this, this);
cm.setEdgeInsets(new Insets(-50, 1, 0, -50));

validate();

repaint();
getContentPane().setLayout(null);


}


public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnRegister) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, REGISTER);
loginCard.setVisible(false);

}
if(e.getSource() == btnBack) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, BACK);
loginCard.setVisible(false);
}
if(e.getSource() == btnSignUp) {
//new SignUpCheck();
}
}

public static void main(String[] args) {
new ClientStarter();
}
}

最佳答案

could this be caused because of the transparent background

也许吧。 Swing 无法正确渲染透明背景。 Swing 期望组件完全不透明或完全透明。

查看Backgrounds With Transparency获取问题的完整描述和一些解决方案。

您可以使用如下代码对每个组件进行自定义绘制:

JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false); // background of parent will be painted first
panel.setBackground( new Color(255, 0, 0, 20) );
frame.add(panel);

或者您可以使用 AlphaContainer 类来为您进行绘制。

此外,您还有其他几个问题:

  1. 不要对 Swing 组件使用静态变量。这不是 static 关键字的正确用法。

  2. 不要使用 null 布局和 setBounds()。 Swing 被设计为与布局管理器一起使用。布局管理器在多个平台上都能很好地工作。

  3. 请勿使用 alpha 值 0。0 表示完全透明,因此只需在组件上使用 setOpaque(false) 方法即可。

  4. 不要不断创建新的 Color 对象。每个组件都可以使用相同的 Color 对象。它可以节省资源,并且可以更轻松地一次性更改所有组件的所有颜色。

  5. 不要在类的构造函数中使用 validate() 和 repaint()。所有组件都应在调用 setVisible(true) 之前添加到框架中,因此不需要这些方法。

关于java - CardLayout 在 Windows 上无法正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003224/

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