I'm creating a gui for my project. When the gui is first loaded only background is visible, so buttons are not visible, but when mouse over them, they are visible. What is the solve this problem?
public class Home extends JFrame{
//New JPanel
private JPanel home;
//Creating image url. You must be change url
ImageIcon icon = new ImageIcon("img//home1.jpeg");
//Home Class
public Home(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 960, 640);
setTitle("LoneyTunes Crush");
home = new JPanel();
home.setBorder(new EmptyBorder(5, 5, 5, 5));
home.setLayout(new BorderLayout(0, 0));
setContentPane(home);
getContentPane().setLayout(null);
JLabel background = new JLabel(new ImageIcon("img//giphy."));
getContentPane().add(background);
background.setLayout(new FlowLayout());
//Creating Buttons
JButton play = new JButton("Play");
play.setBounds(20, 20, 200, 30);
JButton setting = new JButton("Settings");
setting.setBounds(20, 60, 200, 30);
JButton exit = new JButton("Exit");
exit.setBounds(20, 100, 200, 30);
//Adding Buttons
home.add(play);
home.add(setting);
home.add(exit);
//ActionListeners
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
home.setVisible(false);
difficulty.setVisible(true);
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(1);
}
});
validate();
}
//Background paint method
public void paint(Graphics g){
g.drawImage(icon.getImage(), 0, 0, getWidth(), getHeight(), null);
}
}
Main Class
public class MainClass {
public static Home pencere;
public static void main(String args[]){
pencere=new Home();
pencere.setVisible(true);
}
}
Don't paint on top-level containers like JFrame as they already carry the burden of painting all of it's components.
Instead paint on JPanel or JComponent and Override it's paintComponent method.
On top of overriding paintComponent (or in your case paint), you need to also call super.paintComponent (in your case super.paint) inside the the method (first call under the method signature), as to not break the paint chain. Failing to do so may and probably will leave you with undesired paint artifacts.
Avoid using null layouts for a number of reason. Different platform will treat them differently. They are difficult to maintain, among many other reasons. Instead use layout managers, and let them do the laying out and sizing of the components, as they were designed to do with Swing apps. Learn more at Laying out components Within a Container
Setting Home pancere as a static class member of MainClass is completely pointless. Just declare and instantiate both in the main method.
Swing apps should be run on the Event Dispatch Thread (EDT). You can do so by wrapping your the code inside your main method with a SwingUtilities.invokeLater.... See more at Initial Threads
Instead of trying to make panels visible and not visible or adding an removing panel, consider using a CardLayout which will "layer" panels, and you can navigate through them with CardLayout's methods like show(), next(), previous(). See more at How to Use CardLayout
By time of deployments, the images you are using will need to become embedded resources, and should be loaded from the class path, and not from the file system. When you pass a String to ImageIcon, you are telling the program to look in the file system, which may work in your development environment, but that's it. See the wiki tag on embedded-resource an pay close attention to the very last link that will provide you will some resources on how to use and load embedded resources if the info doesn't provide enough detail.
Problem is with
getContentPane().setLayout(null);
remove it as you have already set the layout to a Border Layout and you will see all these buttons.
Just make sure that the setvisibility of all other panels except the one which you wish to display is set to false.I too had a similar problem but i had forgotten to set visibility of one of the 10 panels to false.Problem resolved once i set it to false.
I don't know how this worked for me I just typed jf.setVisible(true); at the end after adding all the GUI codes.
public Calculator(){
jf = new JFrame("Basic Calculator");
jf.setLayout(GridBagLayout);
jf.setSize(306, 550);
jf.setLocation(530, 109);
//all the GUI things like JButton, JLabel, etc...
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Try putting validate(); method on your main frame. I think it would help you.
Related
In the following code, in the constructor the label gets correctly created and displayed on the screen. Note that it is added to the GridBagLayout() layout manager. Then, in the paintComponent() method that we override from the JPanel extention, we reset the contents of the JPanel and add the label again. However, this time the label is not displayed on the screen. I would expect it to be added normally, but it is not. Why is this the case?
public class MyPanel extends JPanel {
private final GridBagConstraints grid = new GridBagConstraints();
public MyPanel () {
setBounds(200, 200, 1000, 1000);
setLayout(new GridBagLayout());
setOpaque(false);
setVisible(false);
grid.anchor = GridBagConstraints.PAGE_END;
JLabel oldLabel = new JLabel("This is an old Label");
add(oldLabel, grid);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
removeAll();
JLabel newLabel = new JLabel("This is a new Label");
add(newLabel, grid);
revalidate();
repaint();
}
}
In this example the component is known, but in my situation I have a variable amount of components that is not known beforehand and changes during the program.
Like the comments to my question kindly say, my approach is not correct.
Under no circumstances should a paintComponent create, add, or remove components. Painting is triggered by the system, for many many reasons, including seemingly trivial events like moving the mouse over the window. Also, never call repaint from a paintComponent method; that forces Swing to eventually called paintComponent again, which means you have created an infinite loop.
The solution is to add the components to the panel and invoke revalidate() on the panel instead.
Recently, I've met an issue that the paintComponent function is not invoked in the function, and I found that when I use splitpane function, it will disable the paint function, and gives error:
cannot add to layout: unknown constraint: null
I think the paint function may not be added to the right way, below is my code(partly):
Class: test
public class Test extends JFrame{
public Test() throws IOException{
//JFrame jf = new JFrame("my frame");
this.add(new NewPanel(this));
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setBounds(300,200,1050,600);
this.setVisible (true);
}
public static void main (String[] args) throws IOException{
Test test = new Test ();
test.setTitle("Hello");
//frame.pack ();
}
}
Class: NewPanel
public class NewPanel extends JPanel{
public NewPanel(JFrame frame) throws IOException{
JTabbedPane jTabbedpane = new JTabbedPane();
JSplitPane splitPane = new JSplitPane();
JPanel p1 = new JPanel();
p1.setLayout(null);
p2.setLayout(new FlowLayout());
splitPane.setOneTouchExpandable(true);
splitPane.setContinuousLayout(true);
//splitPane.setPreferredSize(new Dimension (250,500));
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane.setLeftComponent(p1);
splitPane.setRightComponent (p2);
splitPane.setDividerSize(3);
splitPane.setDividerLocation(250); //balance two panels width
jTabbedpane.addTab("ABC",p2);
jTabbedpane.addTab("AB",p3);
jTabbedpane.addTab("AC",p4);
jTabbedpane.addTab("BC",p5);
frame.setContentPane(splitPane);
frame.add(jTabbedpane);
}
}
public void paintComponent(Graphics g){
super.paint(g);
g.setColor(Color.BLUE);
g.drawLine(303, 90, 303, 200);
g.drawLine(583, 90, 583, 200);
g.drawLine(863, 90, 863, 200);
}
}
When I comment frame.add(jTabbedpane),the line could be drawn in the panel, BUT it is only available in one panel, I cannot draw it into another split panel, I don't know why.. And when I uncomment frame.add(jTabbedpane), it pops up the above mentioned error.
Your UI assembly doesn't make sense. You're calling 'setContentPane' to the splitpane, which is sort-of OK (but unusual), but then you're calling add() to the frame, which tries to then add something else to the contentPane (the JSplitPane). You should either add the JTabbedPane to the SplitPane before adding the splitPane to the JPanel, or set up your layout differently.
//These don't make sense together.
frame.setContentPane(splitPane);
frame.add(jTabbedpane);
Your second question about drawing the blue line is more complicated.
You're doing a bunch of crazy stuff - you're creating a NewPanel and trying to add it to the JFrame, but then you're setting the contentPane of the JFrame to a different component later. You need to go through the Swing Tutorial and lay out your UI better.
I think the paint function may not be added to the right way,
public void paintComponent(Graphics g){
super.paint(g);
You are overriding paintComponent(...), so why are you calling super.paint(...)?
Start by reading the Swing Tutorial for Swing basics. All sections in the tutorial have working examples you can download and test.
So you might start with:
How to Use Split Panes - it will show you how to add a split pane to a frame
Performing Custom Painting - it will explain how painting works and show how to override the paintComponent(...) method.
public class Create_JFrame extends JFrame{
public Create_JFrame(){
//Create a Frame
JFrame Frame = new JFrame("Bla-Bla");
JPanel Panel_1 = new JPanel();
JPanel Panel_2 = new JPanel();
JButton Option_1 = new JButton("Option-1");
//Layout management for Panels
Frame.getContentPane().add(BorderLayout.WEST, Panel_1);
//Add button to Panel
Panel_1.add(Option_1);
//Registering Listeners for all my buttons
Option_1.addActionListener(new ListenerForRadioButton(Panel_2));
//Make the frame visible
Frame.setSize(500, 300);
Frame.setVisible(true);
}//end of Main
}//end of Class
public class ListenerForRadioButton implements ActionListener{
JPanel Panel_2;
JButton browse = new JButton("Browse");
//Constructor, will be used to get parameters from Parent methods
public ListenerForRadioButton(JPanel Panel){
Panel_2 = Panel;
}
//Overridden function, will be used for calling my 'core code' when user clicks on button.
public void actionPerformed(ActionEvent event){
Panel_2.add(browse);
System.out.println("My listener is called");
}//end of method
}//end of class
Problem Statement:
I have 2 JPanel components in a a given JFrame. Panel_1 is having a Option_1 JButton. When user clicks on that I am expecting my code to add a JButton 'browse' in Panel_2 at runtime.
Runtime Output:
System is not adding any JButton in Panel_2. However, I see my debug message in output, indicating that system was successful in identifying user's click action on 'option-1'.
Question:
Why is JPanel not adding any component at Runtime?
Panel_2.add(browse);
Panel_2.revalidate();
adding a 'revalidate' will solve the problem.
There are some reasons. but:
usually it's because of using unsuitable LayoutManager.
sometimes it's because of adding the JPanel to it's root component in worng way. which any operation (add, remove,...) works but is not visible.
you must refresh the view when you make some changes on it, like adding or removing components to/from it.
try to use Panel_2.revalidate() to refresh.
if it doesn't work properly use it with Panel_2.repaint() method.
see Java Swing revalidate() vs repaint()
see Difference between validate(), revalidate() and invalidate() in Swing GUI
using setSize twice for your jframe is another way.
Frame.setSize(498, 300); then Frame.setSize(500, 300);
I started java programming yesterday, and have developed this. I have run into a problem, as the button will not resize. Please help if you can and thank you in advance.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class BgPanel extends JPanel {
Image bg = new ImageIcon("C:\\Users\\********\\Pictures\\tiger.jpg").getImage();
#Override
public void paintComponent(Graphics g) {
g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
}
}
public class FrameTestBase extends JFrame {
public static void main(String args[]) {
JPanel bgPanel = new BgPanel();
bgPanel.setLayout(new BorderLayout());
final FrameTestBase t = new FrameTestBase();
ImageIcon img = new ImageIcon("C:\\Users\\********\\Pictures\\gear-icon.png");
t.setLayout(null);
t.setIconImage(img.getImage());
t.setTitle("Login");
t.setSize(600,600);
t.setLocationRelativeTo(null);
t.setContentPane(bgPanel);
t.setDefaultCloseOperation(EXIT_ON_CLOSE);
t.setVisible(true);
JButton registerButton = new JButton("register");
registerButton.setBounds(80, 80, 80, 80);
t.add(registerButton);
}
}
I have run into a problem, as the button will not resize. Please help
if you can and thank you in advance.
bgPanel.setLayout(new BorderLayout());
// --------- your other code
t.setLayout(null);
//--------------- your other code
t.setContentPane(bgPanel); // you are setting bgPanel which has BorderLayout
JButton registerButton = new JButton("register");
registerButton.setBounds(80, 80, 80, 80);
t.add(registerButton); // t is the JFrame, your main window
Any JFrame.add(component) will essentially add your component to the content pane of the JFrame. After setting layout to null you have added the bgPanel as content pane to the JFrame, which has BorderLayout as its layout manager. Adding your button to the content pane i.e.,bgPanel will add your registerButton with BorderLayout.Center constraint. That is why this button is expanding to the size of the screen.
As you are so eager to see an output do the following:
// registerButton.setBounds(80, 80, 80, 80); comment out this line
registerButton.setPreferedSize(new Dimension(80, 80));
t.add(registerButton, BorderLayout.PAGE_START)
Now, About using NULL Layout:
In your own example you have lost to find the reason why the Button is expanding to the window size. In near future you will see that one of your component has head but lost its tail by going outside of the window border. You will see that one of your component is going to jump over the other without no reason. You will see that you have changed a position of component with relative to another component, but it will make relation with other component. Well you will be able to find the issues wasting lost of time and get fixed by setting xxxSize, setLocation, setBounds etc but....
people can be rich in money, they can't be rich in time.
Start learning LayoutManager: Lesson: Laying Out Components Within a Container
Don't use a null layout!!!
Swing was designed to be used with layout managers. And don't forget to follow Mike's suggestion.
Try to use registerButton.setSize(new Dimension(width, height)) instead of setBounds.
Remember to replace width and height for new values
And I forget to say the same thing guys are telling you:
Don't use null layout.
The sooner you learn, the better.
Layouts are not difficult, they're actually easy.
I'm learning Swing and I have the following code:
public class SimView {
private JFrame frame;
private JLabel background;
private JPanel car_panel;
public SimView() {
frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.getContentPane().setLayout(null);
background = new JLabel("");
background.setIcon(new ImageIcon(
"C:\\Users\\D__\\Desktop\\background.png"));
background.setBounds(0, 0, 384, 462);
frame.getContentPane().add(background);
frame.setVisible(true);
car_panel = new JPanel();
car_panel.setBounds(175, 430, 16, 21);
car_panel.setVisible(true);
car_panel.setBackground(Color.BLACK);
background.add(car_panel);
MoveCarRunnable carMover = new MoveCarRunnable(car_panel);
}
private static class MoveCarRunnable implements Runnable {
private JPanel car;
MoveCarRunnable(final JPanel car) {
this.car = car;
}
#Override
public void run() {
// Should I call rePaint() on the car_panel here then ?
}
}
What I want to do is to move the JLabel called "car" 's y coordinates for every update to get the effect of it moving by itself i.e. no user interaction. I'm not quite sure how to do this, I suppose I need to have some sort of repaint() method redrawing the position of the JLabel for every update. But how do I get this class to know that it needs to update the position?
Any pointers (links/code) would be appreciated. I want to get the concept of how Swing and its components work in this case, rather than just employing a solution but of course I am interested in a solution so I can study it closer. Thanks
EDIT:
Please see my edit code above
You will have to create a separate Thread that would modify the location of your label and then call validate() and repaint() on the container (frame.getContentPane()). Don't forget to put some sleep() value inside the thread.
However, there would be a better approach to create a separate JPanel. Inside it you would override the paintComponent method or the paint method and there you would draw an image instead of moving JLabels around.
You should better add a JPanel instead of the label to your layout. Choose the dimensions of the JPanel so that it can contain your car in every phase.
Then overwrite the paint method of that panel to position the image on it.