This question already has answers here:
Add an Background image to a Panel
(2 answers)
Closed 7 years ago.
I just making kmap program but there is a problem in the code . I take JFrame on which I use JLabel to set background image and another JPanel on which I make table of 4x4 using gridlayout. I add two JButtons compute and reset and 8 JLabels indicating the positions(A'B' etc). but the background image comes infront of buttons labels and table.
package kmap;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class kmapping extends JFrame {
static String output = "";
static int Arr[][] = new int[4][4];
static int checked[][] = new int[4][4];
static int value[] = new int[16];
JButton btn[] = new JButton[16];
JLabel lbl[] = new JLabel[10];
JPanel table;
;
JLabel text, bg;
JButton compute, reset;
kmapping() {
JFrame f = new JFrame();
f.setTitle("KARNAUGH MAP");
f.setSize(580, 430);
f.setVisible(true);
f.setLocation(300, 50);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setResizable(false);
f.setLayout(null);
bg = new JLabel(new ImageIcon("C:\\Documents and Settings\\user\\My Documents\\NetBeansProjects\\kmap\\background.png"));
bg.setBounds(0, 0, 579, 429);
f.add(bg);
table = new JPanel();
text = new JLabel();
table.setBounds(140, 30, 400, 300);
table.setBackground(Color.white);
text.setBounds(50, 300, 620, 110);
text.setBackground(Color.blue);
//adding button in table
table.setLayout(new GridLayout(4, 4));
for (int i = 0; i < 16; i++) {
btn[i] = new JButton();
btn[i].setIcon(new ImageIcon("C:\\Documents and Settings\\user\\My Documents\\NetBeansProjects\\kmap\\images\\x.png"));
table.add(btn[i]);
table.validate();
value[i] = 2;
}
//
lbl[0] = new JLabel("A'B'");
lbl[0].setBounds(110, 30, 30, 75);
f.add(lbl[0]);
lbl[1] = new JLabel("A'B");
lbl[1].setBounds(110, 105, 30, 75);
f.add(lbl[1]);
lbl[2] = new JLabel("AB");
lbl[2].setBounds(110, 180, 30, 75);
f.add(lbl[2]);
lbl[3] = new JLabel("AB'");
lbl[3].setBounds(110, 255, 30, 75);
f.add(lbl[3]);
lbl[4] = new JLabel("C'D'");
lbl[4].setBounds(160, 0, 80, 30);
f.add(lbl[4]);
lbl[5] = new JLabel("C'D");
lbl[5].setBounds(260, 0, 80, 30);
f.add(lbl[5]);
lbl[6] = new JLabel("CD");
lbl[6].setBounds(360, 0, 80, 30);
f.add(lbl[6]);
lbl[7] = new JLabel("CD'");
lbl[7].setBounds(460, 0, 80, 30);
f.add(lbl[7]);
f.validate();
compute = new JButton("COMPUTE");
compute.setBounds(5, 100, 100, 40);
f.add(compute);
reset = new JButton("RESET");
reset.setBounds(5, 160, 100, 40);
f.add(reset);
f.add(table);
compute.validate();
reset.validate();
f.add(text);
f.validate();
try this ..thanks
1. Create a subclass of JComponent.
2. Override the paintComponent(Graphics g) method to paint the image that you want to display.
3. Set the content pane of the JFrame to be this subclass.
// elsewhere
BufferedImage myImage = ImageIO.load(...);
JFrame myJFrame = new JFrame("Image pane");
myJFrame.setContentPane(new ImagePanel(myImage));
Don't worry mate the problem is that your background JLabel should be the last thing you put on your constructor no need to make drastic changes try this hope it works , base on my experience I always do this when I put a back ground image on my JFrame.
package kmap;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class kmapping extends JFrame {
static String output = "";
static int Arr[][] = new int[4][4];
static int checked[][] = new int[4][4];
static int value[] = new int[16];
JButton btn[] = new JButton[16];
JLabel lbl[] = new JLabel[10];
JPanel table;
;
JLabel text, bg;
JButton compute, reset;
kmapping() {
JFrame f = new JFrame();
f.setTitle("KARNAUGH MAP");
f.setSize(580, 430);
f.setVisible(true);
f.setLocation(300, 50);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setResizable(false);
f.setLayout(null);
table = new JPanel();
text = new JLabel();
table.setBounds(140, 30, 400, 300);
table.setBackground(Color.white);
text.setBounds(50, 300, 620, 110);
text.setBackground(Color.blue);
//adding button in table
table.setLayout(new GridLayout(4, 4));
for (int i = 0; i < 16; i++) {
btn[i] = new JButton();
btn[i].setIcon(new ImageIcon("C:\\Documents and Settings\\user\\My Documents\\NetBeansProjects\\kmap\\images\\x.png"));
table.add(btn[i]);
table.validate();
value[i] = 2;
}
//
lbl[0] = new JLabel("A'B'");
lbl[0].setBounds(110, 30, 30, 75);
f.add(lbl[0]);
lbl[1] = new JLabel("A'B");
lbl[1].setBounds(110, 105, 30, 75);
f.add(lbl[1]);
lbl[2] = new JLabel("AB");
lbl[2].setBounds(110, 180, 30, 75);
f.add(lbl[2]);
lbl[3] = new JLabel("AB'");
lbl[3].setBounds(110, 255, 30, 75);
f.add(lbl[3]);
lbl[4] = new JLabel("C'D'");
lbl[4].setBounds(160, 0, 80, 30);
f.add(lbl[4]);
lbl[5] = new JLabel("C'D");
lbl[5].setBounds(260, 0, 80, 30);
f.add(lbl[5]);
lbl[6] = new JLabel("CD");
lbl[6].setBounds(360, 0, 80, 30);
f.add(lbl[6]);
lbl[7] = new JLabel("CD'");
lbl[7].setBounds(460, 0, 80, 30);
f.add(lbl[7]);
f.validate();
compute = new JButton("COMPUTE");
compute.setBounds(5, 100, 100, 40);
f.add(compute);
reset = new JButton("RESET");
reset.setBounds(5, 160, 100, 40);
f.add(reset);
f.add(table);
compute.validate();
reset.validate();
f.add(text);
f.validate();
//Setting up the background
bg = new JLabel(new ImageIcon("C:\\Documents and Settings\\user\\My Documents\\NetBeansProjects\\kmap\\background.png"));
bg.setBounds(0, 0, 579, 429);
f.add(bg);
This will now work and all you components are going to be on top of your background
Related
I am trying to make a character generator in java swing. You click the generate button, and it randomly pulls two character names from an array and displays them as JLabels (newGenerate, newGenerate2) in the GUI (one name on top, and the other on the bottom). I would like for an image of the randomly chosen character to appear when their name is pulled. I have no clue how to do it. Forgive me if this doesn't make any sense, I am new to this.
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
class Main implements ActionListener{
JFrame f = new JFrame("βπππ£πππ₯ππ£ πΎππππ£ππ₯π π£");
Random r = new Random();
JButton generate = new JButton("Generate");
JButton clear = new JButton("Clear");
JLabel newGenerate = new JLabel("Character", JLabel.CENTER);
JLabel newGenerate2 = new JLabel("Character", JLabel.CENTER);
Color custBlue = new Color(10, 63, 250);
Color custOrange = new Color(251,167,88);
Color saturatedCustOrange = new Color(254, 127, 46);
ImageIcon back_image = new ImageIcon("images/back_ground.jpg");
JLabel img2 = new JLabel(back_image);
ImageIcon wheel = new ImageIcon("images/loading_wheel.gif");
JLabel img = new JLabel(wheel);
String[] names1 = {"<html><p align='center'>Character1</p></html>", "<html><p align='center'>Character2</p></html>", "<html><p align='center'>Character3</p></html>", "<html><p align='center'>Character4</p></html>", "<html><p align='center'>Character6</p></html>", "<html><p align='center'>Character6</p></html>", "<html><p align='center'>Character7</p></html>", "<html><p align='center'>Character8</p></html>", "<html><p align='center'>Character9</p></html>",};
public static void main(String[] args) {
Main o = new Main();
o.gui();
}
public void gui(){
f.setSize(500, 400);
this.newGenerate.setBounds(250,190, 100, 40);
this.newGenerate.setForeground(saturatedCustOrange);
f.add(this.newGenerate);
this.newGenerate2.setBounds(250,10, 100, 40);
this.newGenerate2.setForeground(saturatedCustOrange);
f.add(this.newGenerate2);
this.generate.setBounds(28, 140, 120, 40);
generate.setFont(new Font("Arial", Font.BOLD, 16));
this.generate.setForeground(custOrange);
this.generate.setBackground(custBlue);
this.generate.addActionListener(this);
f.add(this.generate);
this.clear.setBounds(28, 190, 120, 40);
clear.setFont(new Font("Arial", Font.BOLD, 16));
this.clear.setForeground(custOrange);
this.clear.setBackground(custBlue);
this.clear.addActionListener(this);
f.add(this.clear);
img.setBounds(0, -10,500,400);
f.add(img);
img2.setBounds(0, -10,500,400);
f.add(img2);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == this.clear){
img.setVisible(true);
this.newGenerate.setText("Character");
this.newGenerate2.setText("Character");
}
else{
this.newGenerate.setText(this.returnChar());
this.newGenerate2.setText(this.returnChar());
img.setVisible(false);
}
}
public String returnChar() {
int ranNum = r.nextInt(this.names1.length);
return this.names1[ranNum];
}
}
I am trying to make a simple calculator program with Java. When I added a JTextField however it made all the button and the field itself invisible until I hover over it. If I comment out the text field everything goes back to normal and all the button are visible.
Here is my code:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JFrame {
// Numbers
JButton btn_zero;
JButton btn_one;
JButton btn_two;
JButton btn_three;
JButton btn_four;
JButton btn_five;
JButton btn_six;
JButton btn_seven;
JButton btn_eight;
JButton btn_nine;
// Operators
JButton btn_add;
JButton btn_subtract;
JButton btn_multiply;
JButton btn_divide;
JButton btn_equals;
JButton btn_decimal;
JButton btn_pm;
JButton btn_clear;
// Panel
JPanel buttonPanel;
// Dimensions
final int WIDTH = 340;
final int HEIGHT = 500;
public Calculator() {
// Characteristics of frame
super("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Insets frameInsets = getInsets();
int frameWidth = WIDTH + (frameInsets.left + frameInsets.right);
int frameHeight = HEIGHT + (frameInsets.top + frameInsets.bottom);
setPreferredSize(new Dimension(frameWidth, frameHeight));
//setLayout(null);
pack();
setVisible(true);
// Add values to all buttons
btn_zero = new JButton("0");
btn_one = new JButton("1");
btn_two = new JButton("2");
btn_three = new JButton("3");
btn_four = new JButton("4");
btn_five = new JButton("5");
btn_six = new JButton("6");
btn_seven = new JButton("7");
btn_eight = new JButton("8");
btn_nine = new JButton("9");
btn_add = new JButton("+");
btn_subtract = new JButton("-");
btn_multiply = new JButton("Γ");
btn_divide = new JButton("Γ·");
btn_equals = new JButton("=");
btn_decimal = new JButton(".");
btn_pm = new JButton("Β±");
btn_clear = new JButton("C");
// Adds the panel
buttonPanel = new JPanel();
buttonPanel.setSize(new Dimension(frameWidth, frameHeight));
buttonPanel.setLayout(null);
// Textfield
JTextField AnswerBox = new JTextField ("");
AnswerBox.setBounds(0, 0, 320, 70);
buttonPanel.add(AnswerBox);
//Buttons
btn_decimal.setBounds(70, 100, 50, 50);
buttonPanel.add(btn_decimal);
btn_pm.setBounds(130, 100, 50, 50);
buttonPanel.add(btn_pm);
btn_clear.setBounds(190, 100, 50, 50);
buttonPanel.add(btn_clear);
btn_add.setBounds(250, 100, 50, 50);
buttonPanel.add(btn_add);
btn_subtract.setBounds(250, 160, 50, 50);
buttonPanel.add(btn_subtract);
btn_multiply.setBounds(250, 220, 50, 50);
buttonPanel.add(btn_multiply);
btn_divide.setBounds(250, 280, 50, 50);
buttonPanel.add(btn_divide);
btn_equals.setBounds(10, 350, 290, 50);
buttonPanel.add(btn_equals);
btn_zero.setBounds(190, 160, 50, 170);
buttonPanel.add(btn_zero);
btn_one.setBounds(10, 160, 50, 50);
buttonPanel.add(btn_one);
btn_two.setBounds(70, 160 , 50, 50);
buttonPanel.add(btn_two);
btn_three.setBounds(130, 160, 50, 50);
buttonPanel.add(btn_three);
btn_four.setBounds(10, 220, 50, 50);
buttonPanel.add(btn_four);
btn_five.setBounds(70, 220, 50, 50);
buttonPanel.add(btn_five);
btn_six.setBounds(130, 220, 50, 50);
buttonPanel.add(btn_six);
btn_seven.setBounds(10, 280, 50, 50);
buttonPanel.add(btn_seven);
btn_eight.setBounds(70, 280, 50, 50);
buttonPanel.add(btn_eight);
btn_nine.setBounds(130, 280, 50, 50);
buttonPanel.add(btn_nine);
buttonPanel.setVisible(true);
add(buttonPanel);
}
}
Also the code is also right now just showing the buttons of the calculator, and not actually doing anything, because I want to focus on fixing this bug.
Call revalidate() after you add all the widgets to recalculate the layout of the container (in your case a JFrame):
buttonPanel.setVisible(true);
add(buttonPanel);
revalidate();
Calling revalidate() at the end would work or you can simply put
setVisible(true)
to the end of your constructor.
I am trying to make an Infix to Postfix GUI calculator. I need to add normal calculator buttons to the calculator and i want there to be "PI" button that holds the value of pi. When the action is called i would like the value of "Pi" to appear in the input.setText field. However, when i attempt to do so, i run into a problem with converting the double into a type that the textField accepts.
import acm.program.*;
import acm.graphics.*; // GCanvas and G-friends
import javax.swing.*; // JButton and J-friends
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;
public class Calc extends Program
{
JTextField input;
JTextField output;
JTextField result;
public Calc()
{
start();
setSize(500, 500);
}
public void init()
{
GCanvas canvas = new GCanvas();
add(canvas);
canvas.setBackground(Color.lightGray);
JButton conversionButton = new JButton(" DEG ");
canvas.add(conversionButton, 85, 60);
JButton conversionButton2 = new JButton("RAD");
canvas.add(conversionButton2, 170, 60);
conversionButton2.setSize(conversionButton.getSize());
JButton goButton = new JButton("Go!");
canvas.add(goButton, 280, 350);
goButton.setBackground(Color.green);
goButton.setSize(conversionButton.getSize());
JButton clearButton = new JButton("Clear");
canvas.add(clearButton, 380, 350);
clearButton.setBackground(Color.red);
clearButton.setSize(conversionButton.getSize());
JButton graphButton = new JButton("Graph");
canvas.add(graphButton, 0, 120);
graphButton.setSize(conversionButton.getSize());
JButton factorialButton = new JButton("!");
canvas.add(factorialButton, 85, 120);
factorialButton.setSize(conversionButton.getSize());
JButton ANSButton = new JButton("ANS");
canvas.add(ANSButton, 0, 90);
ANSButton.setSize(conversionButton.getSize());
JButton LnButton = new JButton("Ln");
canvas.add(LnButton, 85, 90);
LnButton.setSize(conversionButton.getSize());
JButton AbsButton = new JButton("| |");
canvas.add(AbsButton, 170, 90);
AbsButton.setSize(conversionButton.getSize());
JButton PIButton = new JButton("PI");
canvas.add(PIButton, 170, 120);
PIButton.setSize(conversionButton.getSize());
JLabel infixLabel = new JLabel("Infix");
canvas.add(infixLabel, 275, 40);
JLabel postfixLabel = new JLabel("Postfix");
canvas.add(postfixLabel, 275, 160);
JLabel resultLabel = new JLabel("Result");
canvas.add(resultLabel, 275, 280);
JLabel messageLabel = new JLabel("Note: this calculator
can only graph functions of the 3rd degree or lower");
canvas.add(messageLabel, 25, 10);
input = new JTextField();
canvas.add(input, 275, 60);
input.setSize(200, 30);
output = new JTextField();
canvas.add(output, 275, 180);
output.setSize(200, 30);
result = new JTextField();
canvas.add(result, 275, 300);
result.setSize(200, 30);
addActionListeners();
}
public void actionPerformed(ActionEvent ae)
{
String action = ae.getActionCommand();
if (action.equals("Clear"))
{
input.setText("");
output.setText("");
result.setText("");
}
else if (action.equals("Go!"))
{
// Get input from user
String c = input.getText();
result.setText(input.getText());
}
if (action.equals("PI"))
{
double Pi = 3.145926;
Double.toString(Pi);
input.setText(String.Pi);
}
}
}
I've been working on a login screen for a new project and came across a weird rendering "error" while using CardLayout on windows.
SignUp screen on Mac:SignUp Screen Mac
The screens load up correctly on my Mac computer, but on windows they look like these after you click "Register" or after you click "Back".
The same screens on windows:SignUp Screen Windows
As you can see, on windows the SignUp "card" from the CardLayout is rendered over the Login "card" without hiding the other one and vise versa, not like on mac.
Now my question is, could this be caused because of the transparent background and therefore windows thinks that the one behind should still be visible, or could it be creating a brand new "card" each time i switch, or just be forgetting to hide the one in the back all together?
Why does this work on Mac but not on Windows?
And also, how could i go about fixing this?
I will put the whole Class so you can test it for yourself.
(Side note: you may also notice that the button "Register" shows the white button shape on windows even though it has btnRegister.setBorder(null); set (works on Mac))
The complete one Class code:
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
Probably. Swing does not renderer transparent backgrounds correctly. Swing expects a component to be either fully opaque or fully transparent.
Check out Backgrounds With Transparency for a complete description of the problem and a couple of solutions.
You can either do custom painting of every component with code something like:
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);
Or you can use the AlphaContainer class to do the painting for you.
Also, you have several other problems:
Don't use static variables for your Swing components. That is no the proper usage of the static keyword.
Don't use a null layout and setBounds(). Swing was designed to be used with layout managers. Layout managers work well on multiple platforms.
Don't use an alpha value of 0. The 0 means fully transparent, so just use the setOpaque(false) method on the component.
Don't keep creating new Color objects. The same Color object can be used for every component. It save resources and makes it easier to change all Color for all components all at once.
Don't use validate() and repaint() in the constructor of your class. All the components should be added to the frame BEFORE you invoke setVisible(true) so those methods are not required.
I'm new to java so bear with me:
I want the size of the window/pane/panel to chance once the tab is clicked/changed.
How would I do/impletement this?
I've done it successfully on a button click/event, but how do I do a tab click/event?
Here is my code: (I'm sorry for any bad/bloated/unnessicary code in advance)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
//Class shows how to setup up a tabbed window
public class GUI implements ActionListener
{
static JFrame aWindow = new JFrame("Project");
JTabbedPane myTabs = new JTabbedPane();
JPanel loginMainPanel = new JPanel();
JPanel displayMainPanel = new JPanel();
JPanel editMainPanel = new JPanel();
JLabel loginLabel = new JLabel("Username:");
JTextField loginField = new JTextField();
JLabel loginLabel2 = new JLabel("Password:");
JPasswordField loginPass = new JPasswordField();
JButton displayButton = new JButton("Press This Button");
JButton loginButton = new JButton("Login");
JLabel editLabel = new JLabel("Write a story:");
JTextArea editArea = new JTextArea();
public GUI()
{
Toolkit theKit = aWindow.getToolkit();
Dimension wndSize=theKit.getScreenSize();
aWindow.setBounds(wndSize.width/3, wndSize.height/3, 200, 200); //set position, then dimensions
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(1,1);
Container content = aWindow.getContentPane();
content.setLayout(grid);
createLoginPanel();
createDisplayPanel();
createEditPanel();
myTabs.addTab("Login", loginMainPanel);
myTabs.addTab("Display", displayMainPanel);
myTabs.addTab("Edit", editMainPanel);
myTabs.setSelectedIndex(0);
//myTabs.setEnabledAt(1,false);
myTabs.setEnabledAt(2, false);
content.add(myTabs);
aWindow.setVisible(true);
}
public void createLoginPanel()
{
loginMainPanel.setLayout(null);
loginLabel.setBounds(10, 15, 150, 20);
loginMainPanel.add(loginLabel);
loginField.setBounds(10, 35, 150, 20);
loginMainPanel.add(loginField);
loginLabel2.setBounds(10, 60, 150, 20);
loginMainPanel.add(loginLabel2);
loginPass.setBounds(10, 80, 150, 20);
loginMainPanel.add(loginPass);
loginButton.addActionListener(this);
loginButton.setBounds(50, 110, 80, 20);
loginMainPanel.add(loginButton);
}
public void createDisplayPanel()
{
//Toolkit theKit = aWindow.getToolkit();
//Dimension wndSize = theKit.getScreenSize();
//aWindow.setBounds(20, 20, 20, 20); //set position, then dimensions
displayMainPanel.setLayout(null);
displayButton.addActionListener(this);
displayButton.setBounds(10, 80, 150, 20);
displayMainPanel.add(displayButton);
}
public void createEditPanel()
{
editMainPanel.setLayout(null);
editLabel.setBounds(10, 15, 150, 20);
editMainPanel.add(editLabel);
editArea.setBounds(10, 65, 150, 50);
editMainPanel.add(editArea);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == loginButton)
{
//System.out.println("Working...");
Toolkit theKit = aWindow.getToolkit();
Dimension wndSize = theKit.getScreenSize();
aWindow.setBounds(wndSize.width / 4, wndSize.height / 4, 300, 300);
}
//Would the event go here?
}
public static void main(String[] args)
{
GUI tw1 = new GUI();
}
}
Thanks very much :)
James,
but how do I do a tab click/event?
Use a ChangeListener.