How to exactly make JComponents background transparent? - java

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.

Related

How to set position to JComponent in BoxLayout?

I want to use 2 JPanel as panel and panel_1.
I want to add image automatically to panel using JLabel
and also add JButton to panel_1.
How can I resize button according to the image which is above the button?
public class Testing extends JFrame {
public Testing() {
this.setSize(590, 327);
this.setTitle("JFrame");
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(118, 136, 321, 89);
getContentPane().add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel lblImage = new JLabel("image for button1");
panel.add(lblImage);
JLabel lblImage_1 = new JLabel("image for button2");
panel.add(lblImage_1);
JLabel lblImage_2 = new JLabel("image for button3");
panel.add(lblImage_2);
JPanel panel_1 = new JPanel();
panel_1.setBounds(118, 30, 321, 77);
getContentPane().add(panel_1);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
JButton btnNewButton = new JButton("New button 1");
panel_1.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button 2");
panel_1.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button 3");
panel_1.add(btnNewButton_2);
}
public static void main(String[] args) throws Exception {
Testing frame = new Testing();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
If your goal is to have the button above the image, and have the button's width expand with the image, then:
Get rid of your use of null layouts and .setBounds(...) (this is just good general advice)
Put the JLabel with the image into a JPanel that uses BorderLayout with the JLabel in the BorderLayout.CENTER position
Put the button above the JLabel in the same JPanel using the BorderLayout.PAGE_START position.
Then put that JPanel wherever it is needed in the GUI, nesting JPanels, each using their own layout manager.
The BorderLayout will allow the center component to fill that position, and will expand the PAGE_START and PAGE_END positions to fill the width necessary. If the top and bottom components are wider, then this will also expand the width of the container.

Java Button Placement using BorderLayout

This code displays nothing, I have exhausted many avenues but it does not display anything on the GUI (I have a main class that calls this as well already). Please help. I am trying to put the two JButtons horizontally at the bottom of the page and the JTextField and JLabel at the center of the screen.
package test;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("");
textfield = new JTextField("enter text here");
JPanel bottom = new JPanel(new BorderLayout());
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel (new BorderLayout());
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom, BorderLayout.PAGE_END);
centre.add(subCentre, BorderLayout.CENTER);
}
}
Your code is a bit over-complicated. You only need two panels, centre, and buttons. There are two reasons your UI is not showing up:
You never added the panels to the frame
You never set visible to true(Achieve this by using setVisible(true)), unless you did this in the class you ran it in.
One simple way to achieve your desired UI is like so(I added a main method to show the window):
import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
public Test() {
super("test");
JPanel buttons = new JPanel();
JPanel centre = new JPanel();
add(buttons, BorderLayout.SOUTH); //these lines add the
add(centre, BorderLayout.CENTER); //panels to the frame
JButton clear = new JButton("Clear"); // No need
JButton copy = new JButton("Copy"); // to declare
JLabel label = new JLabel("Label"); // these
JTextField textfield = new JTextField("enter text here"); // privately
buttons.add(copy);
buttons.add(clear);
centre.add(label);
centre.add(textfield);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} //end constructor
//added main method to run the UI
public static void main(String[] args) {
new Experiments();
} //end main
} //end class
And it shows the window:
I got closer but its not pretty code, the JFrame is 500x500 so this works based on that... any better suggestions than what I have?
package lab6;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("label");
textfield = new JTextField("enter text here");
JPanel masterPanel = new JPanel(new BorderLayout());
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(100, 200));
JPanel bottom = new JPanel();
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel ();
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom);
centre.add(subCentre);
masterPanel.add(bottom, BorderLayout.PAGE_END);
masterPanel.add(top, BorderLayout.PAGE_START);
masterPanel.add(centre, BorderLayout.CENTER);
add(masterPanel);
}
}

JPanel with FlowLayout not Appearing

I am using the following code for placing labels inside a JPanel, but the JPanel is not appearing (instead, only a blank JFrame is appearing).
Below is the class creating a JFrame instance.
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
WindowContents window = new WindowContents();
window.setSize(600, 400);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Below is the class for setting the contents of the JFrame.
import javax.swing.*;
import java.awt.*;
public class WindowContents extends JFrame {
JLabel label1;
JLabel label2;
JLabel label3;
JPanel panel1;
public WindowContents(){
super("Label Display 2.0");
panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 40));
panel1.setVisible(true);
label1 = new JLabel("Label1");
panel1.add(label1);
label2 = new JLabel("Label2");
panel1.add(label2);
label3 = new JLabel("Label3");
panel1.add(label3);
}
}
Are there any ideas how to make the JPanel contents appear? Thank you.
how to make the JPanel contents appear?
Add panel in the frame.
public WindowContents(){
super("Label Display 2.0");
panel1 = new JPanel();
...
add(panel1);
}
Note:
Use frame.pack() instead of frame.setSize() that fits the components as per component's preferred size.
There is no need to call panel1.setVisible(true);
Just call frame.setVisible(true) in the end after adding all the component.
Favor Composition over inheritance. Instead of extending JFrame make it as member of WindowContents class.
Use SwingUtilities.invokeLater() or EventQueue.invokeLater() to make sure that EDT is initialized properly.
you haven't added the panel to the frame, here's how your class WindowsContents end:
class WindowContents extends JFrame {
JLabel label1;
JLabel label2;
JLabel label3;
JPanel panel1;
public WindowContents(){
super("Label Display 2.0");
panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 40, 40));
label1 = new JLabel("Label1");
panel1.add(label1);
label2 = new JLabel("Label2");
panel1.add(label2);
label3 = new JLabel("Label3");
panel1.add(label3);
add(panel1);
}
}
Cheers

My GUI window doesn't show anything

I'm trying to use a grid layout to make a GUI window. I add all my components and it compiles but when it runs it doesn't show anything. I'm trying to make a simple layout grouped and stacked like this.
{introduction message}
{time label
time input text}
{gravity label
gravity input text}
{answer label
answer text box}
{calculate button clear button}
Here is my code
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
setTitle("Falling Distance Calculator");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 5));
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setVisible(true);
}
public static void main(String[] args)
{
new TurnerRandyFallingGUI();
}
}
You've added nothing to the JFrame that your class above extends. You need to add your components to containers whose hierarchy eventually leads to the top level window, to the this if you will. In other words, you have no add(someComponent) or the functionally similar this.add(someComponent)method call in your code above.
Consider adding all of your JPanels to a single JPanel
Consider adding that JPanel to the JFrame instance that is your class by calling add(thatJPanel).
Even better would be to not extend JFrame and just to create one when needed, but that will likely be the subject of another discussion at another time.
Before setVisible (true) statement add following statements:
add (introP);
add (timeP);
add (gravityP);
add (answerP);
add (buttonsP);
There is nothing in your JFrame. That is the reason
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setLayout(new GridLayout(5, 1));
this.add(introP);
this.add(timeP);
this.add(gravityP);
this.add(answerP);
this.add(buttonsP);
setTitle("Falling Distance Calculator");
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.validate();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TurnerRandyFallingGUI();
}
});
}
}
Consider the following
In GridLayout, the first parameter is Rows, Second is columns
Never set the size of JFrame manually. Use pack() method to decide
the size
Use SwingUtilities.InvokeLater() to run the GUI in another thread.

Not all components showing

When I run this program, the window blocks out the buttons in panel2 when I use setSize to determine window size.
In addition, if I use frame.pack() instead of setSize(), all components are on one horizontal line but I'm trying to get them so that panel1 components are on one line and panel2 components are on a line below them.
Could someone explain in detail the answers to both of these problems?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise16_4 extends JFrame{
// FlowLayout components of top portion of calculator
private JLabel jlbNum1 = new JLabel("Number 1");
private JTextField jtfNum1 = new JTextField(4);
private JLabel jlNum2 = new JLabel("Number 2");
private JTextField jtfNum2 = new JTextField(4);
private JLabel jlbResult = new JLabel("Result");
private JTextField jtfResult = new JTextField(8);
// FlowLayout Components of bottom portion of calculator
private JButton jbtAdd = new JButton("Add");
private JButton jbtSubtract = new JButton("Subtract");
private JButton jbtMultiply = new JButton("Multiply");
private JButton jbtDivide = new JButton("Divide");
public Exercise16_4(){
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
panel1.add(jlbNum1);
panel1.add(jtfNum1);
panel1.add(jlNum2);
panel1.add(jtfNum2);
panel1.add(jlbResult);
panel1.add(jtfResult);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 10));
panel1.add(jbtAdd);
panel1.add(jbtSubtract);
panel1.add(jbtMultiply);
panel1.add(jbtDivide);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
}
public static void main(String[] args){
Exercise16_4 frame = new Exercise16_4();
frame.setTitle("Caculator");
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setResizable(false);
frame.setVisible(true);
}
}
You're problem is likely a typographical error in that you're adding all components to panel1 and none to panel2:
// you create panel2 just fine
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 10));
// but you don't use it! Change below to panel2.
panel1.add(jbtAdd);
panel1.add(jbtSubtract);
panel1.add(jbtMultiply);
panel1.add(jbtDivide);
Add the buttons to panel2, and then call pack() before setVisible(true). Do not set the size of the GUI.

Categories