I am trying to change the positioning of a JLabel and a JButton on my GUI. Even though I try to use .setBounds to change their locations; they both just appear in the top centre of the screen.
import java.awt.color.*;
import java.awt.font.*;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.UIManager.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class yo implements MouseListener {
Image image;
JButton button = new JButton("Wassup");
JFrame frame = new JFrame();
JLabel heloo = new JLabel("yo");
JPanel panel = new JPanel()
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
ImageIcon i = new ImageIcon("hi.jpg");
image = i.getImage();
g.drawImage(image,150,150,null);
g.drawString("Hello",100,100);
g.drawString("Hi",50,50);
}
};
public yo()
{
frame.add(panel);
frame.setTitle("Hello");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
panel.add(heloo);
panel.add(button);
button.setBounds(200,100,200,100);
heloo.setBounds(100,100,100,100);
button.addMouseListener(this);
}
public void mouseClicked (MouseEvent event)
{
heloo.setText(String.format("Clicked at %d,%d", event.getX(), event.getY()));
}
public void mouseEntered (MouseEvent Event){}
public void mouseExited (MouseEvent Event){}
public void mousePressed (MouseEvent Event){}
public void mouseReleased (MouseEvent Event){}
public static void main(String[] args)
{
new yo();
}
}
I apologise about all of the imports, I don't really know which ones I need and which ones are just pointless.
Basically I would like some help on how to change the positioning of my components.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…