My problem is, that I have 3 different JFrames (I can not put 3 in one because all include ImageIcons and Mouslisteners). When I open the programm they are exactly where I want them to be, but when I click on the JFrame in the background it appears in the foreground and the others are moving to the background..
I know that I can set a JFrame.alwaysOnTop(true) but when I set one on top, the other one is moving to the background.
So I need something like a command where I can set one JFrame as the Background, one as the Foreground and one as something in between and it should not be switchable by clicking. Can somebody help me?
ImageIcon img1 = new ImageIcon(getClass().getResource("/image/x.png"));
ImageIcon img2 = new ImageIcon(getClass().getResource("/image/y.png"));
ImageIcon img3 = new ImageIcon(getClass().getResource("/image/z.png"));
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();
JFrame frame3 = new JFrame();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
label1.setIcon(img1);
label2.setIcon(img2);
label3.setIcon(img3);
JPanel panel1 = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel1 = new JPanel();
panel1.add(label1);
panel2.add(label2);
panel3.add(label3);
frame1.setContentPane(panel1);
frame2.setContentPane(panel2);
frame3.setContentPane(panel3);
panel1.setBorder(null);
panel2.setBorder(null);
panel3.setBorder(null);
frame1.setVisible(true);
frame2.setVisible(true);
frame3.setVisible(true);
The expectet result is a full screen background jframe with one big jframe in the middle of it and on top of the middle-JFrame there is another JFrame. And I dont want the JFrames to change their position.
Thank you!
You have to set the JFrames' "FocusableWindowState" to false.
frame1.setFocusableWindowState(false);
frame2.setFocusableWindowState(false);
frame3.setFocusableWindowState(false);
To put one to the background an anotherone to the foreground you just have to set them visible in a specific order.
frame1.setFocusableWindowState(false);
frame2.setFocusableWindowState(false);
frame3.setFocusableWindowState(false);
Like that the first JFrame will be in background, the second in between and the last in foreground.
ImageIcon img1 = new ImageIcon(getClass().getResource("/image/x.png"));
ImageIcon img2 = new ImageIcon(getClass().getResource("/image/y.png"));
ImageIcon img3 = new ImageIcon(getClass().getResource("/image/z.png"));
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();
JFrame frame3 = new JFrame();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
label1.setIcon(img1);
label2.setIcon(img2);
label3.setIcon(img3);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
panel1.add(label1);
panel2.add(label2);
panel3.add(label3);
frame1.setContentPane(panel1);
frame2.setContentPane(panel2);
frame3.setContentPane(panel3);
panel1.setBorder(null);
panel2.setBorder(null);
panel3.setBorder(null);
frame1.setFocusableWindowState(false);
frame2.setFocusableWindowState(false);
frame3.setFocusableWindowState(false);
frame1.setVisible(true);
frame2.setVisible(true);
frame3.setVisible(true);
I hope that I could help you!
Related
I made a JPanel, which has 6 string JLabels, and one image JLabel. What I need is to be able to position the Jlabel strings somewhere inside of my image JLabel's bounds. I'm pretty sure my problem would be solved if I knew how to resize JLabels.
Some Code:
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("rainbow seizure");
frame.setResizable(false);
frame.setSize(500, 500);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.SOUTH);
JButton newCardButton = new JButton("New Card");
newCardButton.addActionListener(actionEvent -> {
JPanel panel2 = new JPanel();
panel2.setLayout(new OverlayLayout(panel2));
JLabel jImage = new JLabel(new ImageIcon("C:\\Users\\unkno\\java\\Card1.PNG"));
Dimension dimension = jImage.getPreferredSize(); // dimensions for me are 72x122
System.out.println(dimension.width);
System.out.println(dimension.height);
JLabel label1 = new JLabel("name");
label1.setBounds(new Rectangle(dimension.width, dimension.height)); // doesn't do anything :(
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label1.setAlignmentY(Component.TOP_ALIGNMENT);
JLabel label2 = new JLabel("cost");
JLabel label3 = new JLabel ("atk");
JLabel label4 = new JLabel("type");
JLabel label5 = new JLabel ("hp");
JLabel label6 = new JLabel("disc");
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);
panel2.add(label4);
panel2.add(label5);
panel2.add(label6);
panel2.add(jImage);
panel.add(panel2);
frame.revalidate();
});
frame.add(newCardButton, BorderLayout.NORTH);
}
}
I'm trying to create a JPanel (non-resizable) showing a grid of buttons but when I try to add the JPanel to a JFrame it won't show.
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
frame.setSize(681,920);
frame.setResizable(true);
JLabel label = new JLabel();
label.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
JButton btn = new JButton();
btn.setContentAreaFilled( false );
btn.setBorder( null );
btn.setBounds(214,210,0,0);
label.add(btn);
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
The output should be a resizable frame with inside a 3x4 grid of buttons.
If I don't use the panel and I put the line frame.setResizable(false) it works as expected but I need to add more stuff to the frame so I need to put the buttons safe in a panel.
Both panel and label are added to your frame, to make sure they are added write
JLabel label = new JLabel("JLABEL");
and
panel.setBackground(Color.BLUE);
I want to increase the height of a custom JPanel in a JFrame. I tried to use pa.setSize(700,200) but it does not change anything.
Here is the code:
JFrame f = new JFrame("Hello");
f.setResizable(true);
JPanel pa = new JPanel();
JButton btn = new JButton("Exit");
pa.setBackground(Color.red);
pa.setSize(700, 200);
f.setUndecorated(true);
f.getContentPane().add(pa, BorderLayout.NORTH);
f.setSize(new Dimension(700,700));
f.setLocation(500, 500);
f.setVisible(true);
its very simple solution instead of pa.setsize() change it into pa.setPreferredSize(new Dimension(width,60));
There are two things that I am trying to figure out. First thing, I want to figure out how to make a Jcomponent background transparent. When placing a JPanel into another JPanel that has a color set as the background, the JPanel that is set within the other JPanel has a white background that I can't seem to get rid of. I tried using the firstpanel.setOpache function and repaint but it doesn't do anything.
And second, I noticed that putting a JPanel within another JPanel thats within another JPanel compresses it size. The images below will show what I am trying to describe. I want to know what to do to avoid compressing the JPanel size and still able to put it within two levels of other JPanels . The code below is what I am practicing with.
import javax.swing.*;
import java.awt.*;
public class LearningFrame extends JFrame {
private JLabel userLabel;
private LearningFrame(){
JPanel backGroundPanel = new JPanel();
JPanel mainPanel = new JPanel();
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
userLabel = new JLabel("User");
userLabel.setFont(new Font("Arial",1 ,24));
backGroundPanel.setBackground(new Color(247,211,53));
// backGroundPanel.setLayout(new BoxLayout(backGroundPanel,BoxLayout.Y_AXIS));
setContentPane(backGroundPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200,800);
setLocationRelativeTo(null);
//backGroundPanel.setLayout(null);
mainPanel.setLayout(new GridLayout(1,2));
firstPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
secondPanel.setLayout(new BoxLayout(secondPanel,BoxLayout.Y_AXIS));
// firstPanel.setBackground(new Color(211,43,185));
secondPanel.setBackground(new Color(34,233,44));
JButton button = new JButton("Click");
JButton button2 = new JButton("Click");
JButton button3 = new JButton("Click");
JButton button4 = new JButton("Click");
firstPanel.add(button);
firstPanel.add(button2);
firstPanel.add(userLabel);
secondPanel.add(button3);
secondPanel.add(button4);
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
backGroundPanel.add(mainPanel);
setVisible(true);
}
public JPanel logPanel() {
JPanel logInPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField userTextField = new JTextField(14);
JTextField passTextField = new JTextField(14);
userLabel = new JLabel("Username: ");
JLabel passLabel = new JLabel("Password: ");
passLabel.setFont(new Font("Arial", Font.BOLD, 24));
userLabel.setFont(new Font("Arial", Font.BOLD, 24));
logInPanel.add(userLabel);
logInPanel.add(userTextField);
logInPanel.add(passLabel);
logInPanel.add(passTextField);
logInPanel.setOpaque(true);
logInPanel.repaint();
return logInPanel;
}
public static void main(String[] args){
LearningFrame x = new LearningFrame();
}
}
Just use
firstPanel.setOpaque(false);
This is will make the background of the component invisible, but you will still be able to see any components that are positioned inside it.
So I'm trying to create a gui, I've tinkered with gui's before in java but I'm still new to them. So my issued here is that my JLabels (butLabel & cbLabel) are filled with buttons and checkboxes. Sadly my JFrame will only show whichever is set to the BorderLayout.CENTER. NORTH & SOUTH don't ever show, even if I only set the butLabel to SOUTH and don't even use the cbLabel. What am I overlooking?? It's much appreciated, thanks!
public class mainWindow
{
JFrame frame = new JFrame("Main Window");
JLabel butLabel = new JLabel();
JLabel cbLabel = new JLabel();
JButton showBut = new JButton("Show");
JButton exitBut = new JButton("Exit");
JButton addBut = new JButton("Add");
JButton remBut = new JButton("Remove");
JCheckBox aCB = new JCheckBox("Airplane");
JCheckBox bCB = new JCheckBox("Boat");
JCheckBox cCB = new JCheckBox("Clock");
public mainWindow()
{
frame.setLayout(new BorderLayout()); //I know this is set by default to BorderLayout but I just did it when I was out of options to try.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(360, 480));
butLabel.setLayout(new GridLayout(1,4));
cbLabel.setLayout(new GridLayout(2, 2));
butLabel.add(showBut);
butLabel.add(exitBut);
butLabel.add(addBut);
butLabel.add(remBut);
cbLabel.add(aCB);
cbLabel.add(bCB);
cbLabel.add(cCB);
frame.add(butLabel, BorderLayout.CENTER);
frame.add(cbLabel, BorderLayout.NORTH);
}
public void setVisible()
{
butLabel.setVisible(true);//Didn't think I needed butLabel.setVisible or the cbLabel.setVisible but
cbLabel.setVisible(true);//again I was trying things that I thought might make sense.
frame.setVisible(true);
}
}
do not use Label for grouping elements, use JPanel instead
I have tried replace all
Label
with
Panel
it works