2016-06-23 14 views
0

新しいプロジェクトのログイン画面で作業していて、Windows上でCardLayoutを使用しているときに奇妙なレンダリング「エラー」が発生しました。 Mac上CardLayoutがウィンドウ上で正しく描画されない

サインアップの画面:SignUp Screen Mac

画面は私のMacコンピュータ上で正しくロードしていますが、「登録」をクリックした後のウィンドウの上に、彼らは、これらのように見えたりした後、「戻る」をクリックします。

窓に同じ画面:SignUp Screen Windows

あなたが好きではない、CardLayoutから新規アカウント「カード」は、他の1と万力その逆を隠すことなくログイン「カード」の上にレンダリングされるウィンドウで、見ることができるようにマック。

これは透明な背景のために起こる可能性があるので、Windowsは後ろのものがまだ見えるはずだと考えているか、スイッチするたびにまったく新しい "カード"背中にあるものをすべて一緒に隠すことを忘れてしまった?

なぜMacでは動作しますが、Windowsでは動作しませんか?

また、どうすればこの問題を解決できますか?

私はあなたがあなた自身のためにそれをテストできるようにクラス全体を入れます。

(サイドノート:あなたはまた、ボタン「登録」は、それがbtnRegister.setBorder(null)を持っているにもかかわらず、窓の上の白いボタンの形状を示していることに気づくことがあり、セットMacの(作品))

完全な1クラスコード:

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

答えて

2

が、これはおそらく理由は、透明な背景

で発生する可能性があります。スイングは透明な背景を正しくレンダリングしません。 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. アルファ値0は使用しないでください。完全に透明なので、コンポーネントのsetOpaque(false)メソッドを使用してください。

  4. 新しいColorオブジェクトを作成しないでください。すべてのコンポーネントで同じColorオブジェクトを使用できます。リソースを節約し、すべてのコンポーネントのすべてのColorを一度に簡単に変更できます。

  5. クラスのコンストラクタでvalidate()とrepaint()を使用しないでください。 setVisible(true)を呼び出す前に、すべてのコンポーネントをフレームに追加する必要があります。

+0

ご協力いただきありがとうございます。私は静的変数を持っています。なぜなら私はこれらの変数にアクセスする必要のある別々のクラスとパッケージを持っているからです(これはゲッターとセッターについてのIKですが、これは初期のテスト段階であり、ヌルレイアウトの理由です)。窓ではない?私の主な関心事です。 –

+1

@AnimeGaming、 '私はこれらの変数にアクセスする必要のある別個のクラスとパッケージを持っています ' - それは静的変数を使用する理由ではありません。クラスにはゲッター/セッターメソッドが必要です。 – camickr

関連する問題