Overlay a picture over background picture - java

I'm making a street fighter game in java, I have all the background pictures finished, and I'm trying to add the characters onto the background, but in the output the character picture keeps appearing next to the background instead of on top of it. I want the character to appear on top of the background, just like a normal street fighter game.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.Border;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class CastleFighter extends JFrame {
Image image;
public static void main(String[] args) {
CastleFighter angela = new CastleFighter();
angela.runIt();
}
public void runIt() {
final JFrame frame = new JFrame("Castle Fighter");
frame.setSize(500, 477);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pa = new JPanel();
JButton ba1 = new JButton("Start");
ba1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
final JFrame frame2 = new JFrame("Menu");
frame2.setDefaultCloseOperation(HIDE_ON_CLOSE);
JPanel pa2 = new JPanel();
frame2.add(pa2);
ImageIcon iii = new ImageIcon(this.getClass().getResource("fight.gif"));
JLabel imageLabel2 = new JLabel();
imageLabel2.setIcon(iii);
pa2.add(imageLabel2, java.awt.BorderLayout.CENTER);
frame2.setVisible(true);
frame2.pack();
frame2.requestFocus();
imageLabel2.setLayout(new GridLayout(3, 0));
JButton ba2 = new JButton("How to play");
ba2.setFont(new Font("Merriweather", Font.PLAIN, 28));
imageLabel2.add(ba2, BorderLayout.SOUTH);
ba2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame2.dispose();
final JFrame frame3 = new JFrame("How to play");
frame3.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
JPanel pa3 = new JPanel();
frame3.add(pa3);
JButton ba5 = new JButton("Return to menu");
ba5.setFont(new Font("Merriweather", Font.PLAIN, 28));
ba5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame3.dispose();
frame2.setVisible(true);
}
});
pa3.setLayout(new BorderLayout());
pa3.add(ba5, BorderLayout.SOUTH);
ImageIcon iiiii = new ImageIcon(this.getClass().getResource("ezgif.com-crop-7.gif"));
JLabel imageLabel4 = new JLabel();
imageLabel4.setIcon(iiiii);
pa3.add(imageLabel4, java.awt.BorderLayout.CENTER);
frame3.setVisible(true);
frame3.pack();
frame3.requestFocus();
}
});
JButton ba3 = new JButton("Start game");
ba3.setFont(new Font("RussoOne", Font.PLAIN, 28));
imageLabel2.add(ba3, BorderLayout.CENTER);
ba3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame2.dispose();
final JFrame frame4 = new JFrame("Level one");
frame4.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
JPanel pa4 = new JPanel();
frame4.add(pa4);
ImageIcon iiii = new ImageIcon(this.getClass().getResource("ezgif.com-add-text-7.gif"));
JLabel imageLabel3 = new JLabel();
imageLabel3.setIcon(iiii);
pa4.add(imageLabel3, java.awt.BorderLayout.CENTER);
ImageIcon i14 = new ImageIcon(this.getClass().getResource("fightercharacter.gif"));
JLabel imageLabel14 = new JLabel();
imageLabel14.setIcon(i14);
pa4.add(imageLabel14, java.awt.BorderLayout.NORTH);
JButton ba6 = new JButton("menu");
ba6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame4.dispose();
frame2.setVisible(true);
}
});
pa4.add(ba6);
JButton ba7 = new JButton("Level two");
ba7.setSize(400, 200);
pa4.add(ba7);
ba7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame4.dispose();
final JFrame frame6 = new JFrame("Level two");
frame6.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame6.setVisible(true);
JPanel pa6 = new JPanel();
frame6.add(pa6);
ImageIcon iiiiiii = new ImageIcon(this.getClass().getResource("ezgif.com-add-text-6.gif"));
JLabel imageLabel6 = new JLabel();
imageLabel6.setIcon(iiiiiii);
pa6.add(imageLabel6, BorderLayout.CENTER);
frame6.pack();
frame6.requestFocus();
JButton ba7 = new JButton("menu");
pa6.add(ba7, BorderLayout.SOUTH);
ba7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame6.dispose();
frame2.setVisible(true);
}
});
JButton ba9 = new JButton("Level one");
pa6.add(ba9, BorderLayout.SOUTH);
ba9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame6.dispose();
frame4.setVisible(true);
}
});
frame6.setVisible(true);
frame6.pack();
frame6.requestFocus();
}
});
frame4.setVisible(true);
frame4.pack();
frame4.requestFocus();
}
});
JButton ba4 = new JButton("Quit game");
ba4.setFont(new Font("MerriWeather", Font.PLAIN, 28));
imageLabel2.add(ba4, BorderLayout.WEST);
ba4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
frame2.requestFocus();
ba2.setOpaque(false);
ba2.setContentAreaFilled(false);
ba2.setBorderPainted(false);
ba2.setForeground(Color.WHITE);
ba3.setOpaque(false);
ba3.setContentAreaFilled(false);
ba3.setBorderPainted(false);
ba3.setForeground(Color.WHITE);
ba4.setOpaque(false);
ba4.setContentAreaFilled(false);
ba4.setBorderPainted(false);
ba4.setForeground(Color.WHITE);
}
});
ba1.setPreferredSize(new Dimension(100, 100));
ba1.setFont(new Font("RussoOne", Font.PLAIN, 28));
ba1.setBackground(Color.BLUE);
ba1.setOpaque(true);
frame.add(pa);
pa.setLayout(new BorderLayout());
pa.add(ba1, BorderLayout.SOUTH);
ImageIcon ii = new ImageIcon(this.getClass().getResource("ezgif.com-add-text-2.gif"));
JLabel imageLabel = new JLabel();
imageLabel.setIcon(ii);
pa.add(imageLabel, java.awt.BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
frame.requestFocus();
}
}

Related

Set JPanel transparent for keypad

I'm actually codding a keypad for an application and I'm encountering a problem. I'm using some empty space in the JDialog I'm creating to place an exist button on the top right corner of the Dialog box. This empty space is creating a border to the dialog box which is unpleasant to see. So I've tried to make panels transparent with setOpaque(false) function, but I achieved nothing. Do I have to use the paint function (which I don't know how to use) ?
Here is my test function without the setOpaque tests I've done :
package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Test {
public static JFrame frame;
public static void main(String args[]){
frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(400, 400);
panel.setBackground(Color.CYAN);
panel.setLayout(new GridBagLayout());
JTextField text = new JTextField("coucou");
panel.add(text);
text.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
text.setText("");
openKeypad(text);
}
});
frame.setContentPane(panel);
frame.setSize(400, 400);
frame.setUndecorated(true);
frame.setLocation(50,0);
frame.setVisible(true);
}
private static void openKeypad(JTextField text) {
KeyPad pad = new KeyPad(frame, text);
}
}
KeyPad class :
package test;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class KeyPad extends JDialog {
private JTextField field;
public KeyPad(JFrame parent, JTextField field_) {
super(parent, true);
//Initializing field
field = field_;
//Setting layout
JPanel panelButton = new JPanel();
panelButton.setLayout(new GridLayout(4, 3));
//Buttons declaration
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton bp = new JButton(".");
JButton del = new JButton();
JButton close = new JButton();
//Setting buttons icons
try {
Image img = ImageIO.read(new File("ressources/closeIcon.png"));
close.setIcon(new ImageIcon(img));
img = ImageIO.read(new File("ressources/deleteIcon.png"));
del.setIcon(new ImageIcon(img));
close.setOpaque(false);
close.setBorder(new EmptyBorder(0, 0, 0, 0));
close.setContentAreaFilled(false);
}catch (Exception e) {
System.out.println(e);
}
//Buttons sizing
b0.setPreferredSize(new Dimension(64, 64));
b1.setPreferredSize(new Dimension(64, 64));
b2.setPreferredSize(new Dimension(64, 64));
b3.setPreferredSize(new Dimension(64, 64));
b4.setPreferredSize(new Dimension(64, 64));
b5.setPreferredSize(new Dimension(64, 64));
b6.setPreferredSize(new Dimension(64, 64));
b7.setPreferredSize(new Dimension(64, 64));
b8.setPreferredSize(new Dimension(64, 64));
b9.setPreferredSize(new Dimension(64, 64));
bp.setPreferredSize(new Dimension(64, 64));
del.setPreferredSize(new Dimension(64, 64));
close.setPreferredSize(new Dimension(32, 32));
//Adding buttons to the panelButton
panelButton.add(b7);
panelButton.add(b8);
panelButton.add(b9);
panelButton.add(b4);
panelButton.add(b5);
panelButton.add(b6);
panelButton.add(b1);
panelButton.add(b2);
panelButton.add(b3);
panelButton.add(bp);
panelButton.add(b0);
panelButton.add(del);
//Adding Listeners
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('0');
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('1');
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('2');
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('3');
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('4');
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('5');
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('6');
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('7');
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('8');
}
});
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('9');
}
});
bp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('.');
}
});
del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
delDigit();
}
});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
disposeDialog();
}
});
//Placing close button
JPanel panelClose = new JPanel();
panelClose.setLayout(new BoxLayout(panelClose, BoxLayout.X_AXIS));
panelClose.add(Box.createRigidArea(new Dimension(64*3, 1)));
panelClose.add(close);
JPanel test = new JPanel();
test.setLayout(new BoxLayout(test, BoxLayout.X_AXIS));
test.add(panelButton);
test.add(Box.createRigidArea(new Dimension(32, 1)));
//Setting main panel
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(panelClose);
panel.add(test);
//Setting and displaying the Dialog Box
this.setContentPane(panel);
this.setUndecorated(true);
this.pack();
this.setVisible(true);
}
private void addDigit(char i) {
field.setText(field.getText() + i);
}
private void delDigit() {
if(field.getText().length() > 0) {
field.setText(field.getText().substring(0, field.getText().length() - 1));
}
}
private void disposeDialog() {
this.dispose();
}
}
/////EDIT///////
Here is my keypad code at the moment if someone wants to use it, feel free.
package mainPackage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class KeyPad extends JDialog {
private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
private GraphicsDevice[] gs = ge.getScreenDevices();
private JTextField field;
public KeyPad(JDialog parent, JTextField field_, String placement) {
super(parent, true);
//Initializing field
field = field_;
//Setting layout
JPanel panelButton = new JPanel();
panelButton.setLayout(new GridLayout(4, 3));
//Buttons declaration
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton bp = new JButton(".");
JButton del = new JButton();
JButton close = new JButton();
//Setting buttons icons
try {
Image img = ImageIO.read(new File("ressources/closeIcon.png"));
close.setIcon(new ImageIcon(img));
img = ImageIO.read(new File("ressources/deleteIcon.png"));
del.setIcon(new ImageIcon(img));
close.setOpaque(false);
close.setBorder(new EmptyBorder(0, 0, 0, 0));
close.setContentAreaFilled(false);
}catch (Exception e) {
System.out.println(e);
}
//Buttons sizing
b0.setPreferredSize(new Dimension(64, 64));
b1.setPreferredSize(new Dimension(64, 64));
b2.setPreferredSize(new Dimension(64, 64));
b3.setPreferredSize(new Dimension(64, 64));
b4.setPreferredSize(new Dimension(64, 64));
b5.setPreferredSize(new Dimension(64, 64));
b6.setPreferredSize(new Dimension(64, 64));
b7.setPreferredSize(new Dimension(64, 64));
b8.setPreferredSize(new Dimension(64, 64));
b9.setPreferredSize(new Dimension(64, 64));
bp.setPreferredSize(new Dimension(64, 64));
del.setPreferredSize(new Dimension(64, 64));
close.setPreferredSize(new Dimension(32, 32));
//Adding buttons to the panelButton
panelButton.add(b7);
panelButton.add(b8);
panelButton.add(b9);
panelButton.add(b4);
panelButton.add(b5);
panelButton.add(b6);
panelButton.add(b1);
panelButton.add(b2);
panelButton.add(b3);
panelButton.add(bp);
panelButton.add(b0);
panelButton.add(del);
panelButton.setOpaque(false);
//Adding Listeners
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('0');
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('1');
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('2');
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('3');
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('4');
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('5');
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('6');
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('7');
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('8');
}
});
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('9');
}
});
bp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addDigit('.');
}
});
del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
delDigit();
}
});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
disposeDialog();
}
});
//Placing close button
JPanel panelClose = new JPanel();
panelClose.setLayout(new BoxLayout(panelClose, BoxLayout.X_AXIS));
panelClose.add(Box.createRigidArea(new Dimension(64*3, 1)));
panelClose.add(close);
panelClose.setOpaque(false);
JPanel panelButtonWithPadding = new JPanel();
panelButtonWithPadding.setLayout(new BoxLayout(panelButtonWithPadding, BoxLayout.X_AXIS));
panelButtonWithPadding.add(panelButton);
panelButtonWithPadding.add(Box.createRigidArea(new Dimension(32, 1)));
panelButtonWithPadding.setOpaque(false);
//Setting main panel
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(panelClose);
panel.add(panelButtonWithPadding);
panel.setOpaque(false);
//Setting dialog box transparency
panelButton.setOpaque(false);
panelClose.setOpaque(false);
panelButtonWithPadding.setOpaque(false);
panel.setOpaque(false);
//Setting and displaying the Dialog Box
this.setContentPane(panel);
this.setUndecorated(true);
this.setBackground( new Color(0, 0, 0, 0) ); // set transparency on JDialog only
this.pack();
//To place the KeyPad anywhere on the panel
switch(placement) {
case "BOTTOM_LEFT":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x, 600 - (int) this.getSize().getHeight());
break;
case "BOTTOM_CENTER":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x + (int) ((800 - this.getSize().getWidth())/2) , 600 - (int) this.getSize().getHeight());
break;
case "BOTTOM_RIGHT":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x + (int) (800 - this.getSize().getWidth()), 600 - (int) this.getSize().getHeight());
break;
case "CENTER_LEFT":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x, (int) (600 - this.getSize().getHeight())/2);
break;
case "CENTER":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x + (int) ((800 - this.getSize().getWidth())/2), (int) (600 - this.getSize().getHeight())/2);
break;
case "CENTER_RIGHT":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x + (int) (800 - this.getSize().getWidth()), (int) (600 - this.getSize().getHeight())/2);
break;
case "TOP_LEFT":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x, 0);
break;
case "TOP_CENTER":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x + (int) ((800 - this.getSize().getWidth())/2), 0);
break;
case "TOP_RIGHT":
this.setLocation(gs[Main.screen].getDefaultConfiguration().getBounds().x + (int) (800 - this.getSize().getWidth()), 0);
break;
}
this.setVisible(true);
}
private void addDigit(char i) {
field.setText(field.getText() + i);
}
private void delDigit() {
if(field.getText().length() > 0) {
field.setText(field.getText().substring(0, field.getText().length() - 1));
}
}
private void disposeDialog() {
this.dispose();
}
}
Simple example for making a JFrame transparent:
import java.awt.*;
import static java.awt.GraphicsDevice.WindowTranslucency.TRANSLUCENT;
import javax.swing.*;
class TransparentFrame extends JFrame
{
public TransparentFrame()
{
JPanel panel = new JPanel();
panel.setOpaque( false );
panel.add( new JButton("Button") );
add(panel, BorderLayout.SOUTH);
setUndecorated(true);
setSize(300, 300);
//pack();
// setOpacity(0.5f); // set transparency on frame and components
setBackground( new Color(0, 0, 0, 64) ); // set transparency on frame only
setTitle("Transparent Frame Demo");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String args[])
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
// Exit when translucent windows aren't supported
if (!gd.isWindowTranslucencySupported(TRANSLUCENT))
{
System.err.println( "Translucency is not supported" );
System.exit(0);
}
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(() -> new TransparentFrame().setVisible(true));
}
}

Centering Swing Components

How do I make my textfields and button centered?
Edit: Updated my post with SSCE. Hopefully that helps.
By the way, I want the left image to look like the right.
package pkg;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class Game {
private static JPanel panel = new JPanel();
public static JTextField username = new JTextField(20);
public static JPasswordField password = new JPasswordField(20);
JButton login = new JButton("Login");
JLabel status = new JLabel();
private static JPanel game = new JPanel();
private JButton logout = new JButton("Logout");
private static JFrame frame = new JFrame("RuneShadows");
public Game() {
panel.add(username);
panel.add(password);
panel.add(login);
panel.add(status);
game.add(logout);
frame.add(panel);
frame.setSize(806, 553);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Game();
}
public void loadGame() {
frame.remove(panel);
frame.revalidate();
frame.add(game);
frame.revalidate();
logout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(game);
frame.revalidate();
frame.add(panel);
frame.revalidate();
try {
Client.socketOut.writeUTF("logout");
Client.socketOut.writeUTF(Client.username);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
}
What I want it to look like:
I've tried to use many different layout styles but I can't seem to make it work...
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.border.*;
public class Game {
private static JPanel panel = new JPanel();
public static JTextField username = new JTextField(20);
public static JPasswordField password = new JPasswordField(20);
JButton login = new JButton("Login");
JLabel status = new JLabel();
private static JPanel game = new JPanel(new FlowLayout(FlowLayout.CENTER));
private JButton logout = new JButton("Logout");
private static JFrame frame = new JFrame("RuneShadows");
public Game() {
panel.setLayout(new GridLayout(0,1,15,15));
panel.setBorder(new EmptyBorder(50,100,50,100));
panel.add(username);
panel.add(password);
panel.add(status);
JPanel logoutConstrain = new JPanel(new FlowLayout(FlowLayout.CENTER));
logoutConstrain.add(logout);
panel.add(logoutConstrain);
frame.setLayout(new GridBagLayout());
frame.add(panel);
//frame.setSize(806, 553); // forget this nonsense, instead..
frame.pack(); // best!
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Game();
}
public void loadGame() {
frame.remove(panel);
frame.revalidate();
frame.add(game);
frame.revalidate();
logout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(game);
frame.revalidate();
frame.add(panel);
frame.revalidate();
}
});
}
}

Why is the graphic not working when the button number 8 is pressed?

I have this code and it works with everything until the bottom middle button is pressed.
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class memory extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(new Color(156, 93, 82));
g.fill3DRect(21, 3, 7, 12, true);
g.setColor(new Color(156, 23, 134));
g.fillOval(1, 15, 15, 15);
g.fillOval(16, 15, 15, 15);
g.fillOval(31, 15, 15, 15);
g.fillOval(7, 31, 15, 15);
g.fillOval(22, 31, 15, 15);
g.fillOval(16, 47, 15, 15);
}
public memory() {
GridLayout h = new GridLayout(3, 3);
final JFrame frame = new JFrame();
final JPanel pan = new JPanel(h);
frame.add(pan);
//pan=new JPanel(h);
pan.setBackground(new Color(130, 224, 190));
setFont(new Font("Serif", Font.BOLD, 28));
JButton button1 = new JButton();
pan.add(button1);
final JLabel label1 = new JLabel("hi");
label1.setVisible(false);
pan.add(label1);
JButton button2 = new JButton();
pan.add(button2);
final JLabel label2 = new JLabel("hi");
label2.setVisible(false);
pan.add(label2);
JButton button3 = new JButton();
pan.add(button3);
final JLabel label3 = new JLabel("hi");
label3.setVisible(false);
pan.add(label3);
JButton button4 = new JButton();
pan.add(button4);
final JLabel label4 = new JLabel("hi");
label4.setVisible(false);
pan.add(label4);
JButton button5 = new JButton();
pan.add(button5);
final JLabel label5 = new JLabel("hi");
label5.setVisible(false);
pan.add(label5);
JButton button6 = new JButton();
pan.add(button6);
final JLabel label6 = new JLabel("hi");
label6.setVisible(false);
pan.add(label6);
JButton button7 = new JButton();
pan.add(button7);
final JLabel label7 = new JLabel("hi");
label7.setVisible(false);
pan.add(label7);
JButton button8 = new JButton();
pan.add(button8);
final JLabel label8 = new JLabel("hi");
label8.setVisible(false);
pan.add(label8);
JButton button9 = new JButton();
pan.add(button9);
final JButton button10 = new JButton("Exit");
pan.add(button10);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("memory Game");
setLayout(new BorderLayout());
add(pan, BorderLayout.CENTER);
add(button10, BorderLayout.SOUTH);
setSize(600, 600);
setVisible(true);
final JLabel label9 = new JLabel("hi");
label9.setVisible(false);
pan.add(label9);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label1.setVisible(true);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label2.setVisible(true);
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label3.setVisible(true);
}
});
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label4.setVisible(true);
}
});
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label5.setVisible(true);
}
});
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label6.setVisible(true);
}
});
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label7.setVisible(true);
}
});
//this is where I thought it was going to do something if it was pressed.
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label8.setVisible(true);
frame.getContentPane().add(new memory());
setVisible(true);
}
}
);
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label9.setVisible(true);
}
}
);
button10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (button10.getSize() != null) {
System.exit(0);
}
}
});
}
public static void main(String args[]) {
new memory();
}
}
memory extends JFrame. Swing won't let you add a window to another window.
You should avoid extending from top level containers (this is just reason) and instead use something like JPanel.
This allows you to add the component to whatever container you like
JFrame does not have a paintComponent method, so it will never be called. The reason it compiles is that you've called super.paintComponents (note the s at the end).
Instead, you should, extend from something like JPanel and override it's paintComponent method and perform you custom painting from there

How to position JButton in a JFrame window?

My program is about a supermarket. I want to position the JButton 'b1' just below JLabel 'l1' and also below JTextField 'jt1'. I want the JButton 'b1' to also be in the centre but below 'l1' and 'jt1'. Below is the delivery() method of my program:
public static void delivery()
{
final JFrame f1 = new JFrame("Name");
f1.setVisible(true);
f1.setSize(600,200);
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setLocation(700,450);
JPanel p1 = new JPanel();
final JLabel l1 = new JLabel("Enter your name: ");
final JTextField jt1 = new JTextField(20);
JButton b1 = new JButton("Ok");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input1 = jt1.getText();
f1.setVisible(false);
}
});
p1.add(b1);
p1.add(l1);
p1.add(jt1);
f1.add(p1);
final JFrame f2 = new JFrame("Address");
f2.setVisible(true);
f2.setSize(600,200);
f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f2.setLocation(700,450);
JPanel p2 = new JPanel();
final JLabel l2 = new JLabel("Enter your address: ");
final JTextField jt2 = new JTextField(20);
JButton b2 = new JButton("Ok");
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input2 = jt2.getText();
f2.setVisible(false);
}
});
p2.add(b2);
p2.add(l2);
p2.add(jt2);
f2.add(p2);
}
}
You can use multiple JPanels to get close to what you want:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyGui {
public static void delivery()
{
JFrame f1 = new JFrame("Name");
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setBounds(200, 100, 500, 300);
Container cpane = f1.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS)); //Horizontal
JLabel l1 = new JLabel("Enter your name: ");
JTextField jt1 = new JTextField(20);
jt1.setMaximumSize( jt1.getPreferredSize() );
p1.add(l1);
p1.add(jt1);
JPanel p2 = new JPanel();
p2.setLayout(new BoxLayout(p2, BoxLayout.PAGE_AXIS)); //Vertical
p2.add(p1);
JButton b1 = new JButton("Ok");
p2.add(b1);
cpane.add(p2);
f1.setVisible(true);
}
}
public class SwingProg {
private static void createAndShowGUI() {
MyGui.delivery();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Use the GridBagLayout Class. It's for custom designing using Constraints.

Changing the text in the title bar

I'm getting an error at the line: jFrame cannot be resolved
jFrame.setTitle(titleName.getText());
public void createOption(){
Option = new JPanel();
Option.setLayout( null );
JLabel TitleLabel = new JLabel("Change the company name");
TitleLabel.setBounds(140, 15, 200, 20);
Option.add(TitleLabel);
titleName = new JTextField();
titleName.setBounds(90,40,260,20);
Option.add(titleName);
JButton Change = new JButton("Change New Name");
Change.setBounds(90,80,150,20);
Change.addActionListener(this);
Change.setBackground(Color.white);
Option.add(Change);
JButton Exit = new JButton("Exit");
Exit.setBounds(270,80,80,20);
Exit.addActionListener(this);
Exit.setBackground(Color.white);
Option.add(Exit);
Change.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(titleName.getText());
}
});
}
You will have to have a reference to your JFrame. Assuming button and textBox for the names for the controls, you can do
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(textBox.getText());
}
});
EDIT:
Here's a full example
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JFrameExample {
public static void main(String[] args) {
final JFrame jFrame = new JFrame("This is a test");
jFrame.setSize(400, 150);
Container content = jFrame.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
final JTextField jTextField = new JTextField("TestTitle");
content.add(jTextField);
final JButton button = new JButton("Change");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(jTextField.getText());
}
});
content.add(button);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}

Categories

Resources