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));
}
}
Related
I'm getting the Co-ordinates of the mouse on the window , I'm also changing the background color using KeyListner
Letter B for Black
W for White
G for green
.......
but when I change the background color , only the background of the label is changing and not the entire window ,what I need is to change the entire window and not just the label , any ideas why is that ?
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class mouseCrd extends JFrame {
private JPanel contentPane;
public mouseCrd() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 650);
setTitle("Mouse co-ordinates");
setVisible(true);
setResizable(false);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 650, 650);
contentPane.add(panel);
JLabel label = new JLabel(".................");
label.setFont(new Font("Tahoma", Font.BOLD, 30));
panel.add(label);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
label.setText("X = "+ e.getX()+" ; Y = "+e.getY());
}
});
addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
int clr = e.getKeyChar();
if(clr==KeyEvent.VK_B)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.BLACK);
add(label);
}
else if(clr==KeyEvent.VK_W)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.WHITE);
add(label);
}
else if(clr==KeyEvent.VK_R)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.RED);
add(label);
}
else if(clr==KeyEvent.VK_O)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.ORANGE);
add(label);
}
else if(clr==KeyEvent.VK_G)
{
System.out.println("B is pressed");
label.setOpaque(true);
label.setBackground(Color.GREEN);
add(label);
}
}
});
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable()
{
public void run() {
try {
mouseCrd frame = new mouseCrd();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
if you want to change the background of your aplication, you have to change the background of the JPane that contains all your components, you are doing:
label.setBackground(Color.ORANGE);
which is okay if you want to change the background of the label, this is explicit on the instruction, i did some changes in your code, look at this:
public class mouseCrd extends JFrame {
public mouseCrd() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 650);
setTitle("Mouse co-ordinates");
setVisible(true);
setResizable(false);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 650, 650);
add(panel);
JLabel label = new JLabel(".................");
label.setFont(new Font("Tahoma", Font.BOLD, 30));
panel.add(label);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
label.setText("X = "+ e.getX()+" ; Y = "+e.getY());
}
});
addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
char keyPressed = e.getKeyChar();
if(keyPressed=='b')
{
System.out.println("B is pressed");
panel.setBackground(Color.BLACK);
}
else if(keyPressed=='w')
{
System.out.println("W is pressed");
panel.setBackground(Color.WHITE);
}
else if(keyPressed=='r')
{
System.out.println("R is pressed");
panel.setBackground(Color.RED);
}
else if(keyPressed=='o')
{
System.out.println("O is pressed");
panel.setBackground(Color.ORANGE);
}
else if(keyPressed=='g')
{
System.out.println("G is pressed");
panel.setBackground(Color.GREEN);
}
}
});
}
public static void main(String[] args){
new mouseCrd();
}
}
I removed the contentPane because it was useless an then in the key released, i just change the backgound of the panel JPane with the method setBackground(), and i removed the use of the KeyEvent constants because is easier to use chars
I know what you already get the needed answer, but I would like to recommend you the Oracle Documentation regarding Swing and GUI's.
Its begginer friendly 😁 and it stress exactly what you need to know to have good fundaments.
https://docs.oracle.com/javase/tutorial/uiswing/index.html
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();
}
}
So my goal is to disable buttons on lets say panel B depending one what button they press on panel A. so below I have 2 combo boxes that id like to be able to enable or disable base on the buttons pressed on the first panel. Ive tried googling this problem but I'm new to java so its pretty rough going so far.
heres my sample code of what I'm trying to do.
package pack2;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class tessst {
public boolean enableChk1;
public boolean enableChk2;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
tessst window = new tessst();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public tessst() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel panel = new JPanel();
frame.getContentPane().add(panel, "name_15095567731094");
panel.setLayout(null);
final JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, "name_15101078033315");
panel_1.setLayout(null);
JButton select2 = new JButton("2 boxes");
select2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = true;
enableChk2 = true;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
}
});
select2.setBounds(276, 101, 89, 23);
panel.add(select2);
JButton select1 = new JButton("1 boxes");
select1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = true;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
}
});
select1.setBounds(59, 101, 89, 23);
panel.add(select1);
JButton select0 = new JButton("no boxes");
select0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = false;
enableChk2 = false;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
}
});
select0.setBounds(166, 169, 89, 23);
panel.add(select0);
JComboBox comboBox = new JComboBox();
comboBox.setEnabled(enableChk1);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox.setBounds(52, 100, 61, 20);
panel_1.add(comboBox);
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setEnabled(enableChk2);
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox_1.setBounds(265, 100, 79, 20);
panel_1.add(comboBox_1);
JButton back = new JButton("go back");
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(true);
panel_1.setVisible(false);
}
});
back.setBounds(10, 227, 89, 23);
panel_1.add(back);
}
}
I needed to declare my comboBox's above my buttons then declare them as final.
here are the changes
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class tessst {
public boolean enableChk1;
public boolean enableChk2;
JComboBox comboBox;
JComboBox comboBox_1;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
tessst window = new tessst();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public tessst() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel panel = new JPanel();
frame.getContentPane().add(panel, "name_15095567731094");
panel.setLayout(null);
final JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, "name_15101078033315");
panel_1.setLayout(null);
JButton select2 = new JButton("2 boxes");
select2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = true;
enableChk2 = true;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
comboBox.setEnabled(true);
comboBox_1.setEnabled(true);
}
});
select2.setBounds(276, 101, 89, 23);
panel.add(select2);
JButton select1 = new JButton("1 boxes");
select1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = true;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
comboBox.setEnabled(true);
}
});
select1.setBounds(59, 101, 89, 23);
panel.add(select1);
JButton select0 = new JButton("no boxes");
select0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
enableChk1 = false;
enableChk2 = false;
panel_1.revalidate();
frame.repaint();
panel_1.repaint();
frame.repaint();
panel.setVisible(false);
panel_1.setVisible(true);
comboBox.setEnabled(false);
comboBox_1.setEnabled(false);
}
});
select0.setBounds(166, 169, 89, 23);
panel.add(select0);
comboBox = new JComboBox();
comboBox.setEnabled(enableChk1);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox.setBounds(52, 100, 61, 20);
panel_1.add(comboBox);
comboBox_1 = new JComboBox();
comboBox_1.setEnabled(enableChk2);
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox_1.setBounds(265, 100, 79, 20);
panel_1.add(comboBox_1);
JButton back = new JButton("go back");
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(true);
panel_1.setVisible(false);
}
});
back.setBounds(10, 227, 89, 23);
panel_1.add(back);
}
}
replace your code with this one. Its working Fine. Hope you are expecting this thing.
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
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);
}