Jframe is setting everything in the middle of the screen - java

My Jframe is setting every component at the middle of the screen even if i set a specific location...
for exmaple, the button and the searchbar are right next to each other.
Here is the image of the program:
thanks!
It really seems easy but i can't figure out why it doesn't work
private String msg;
private JFrame frame;
private JButton button;
private JPanel panel;
private JTextField searchBar;
private JLabel name;
private JLabel logo;
public GUI() {
this.frame = new JFrame();
this.panel = new JPanel();
this.panel.setBorder(BorderFactory.createEmptyBorder(300, 300, 300, 300));
//this.panel.setLayout(new GridLayout(10, 10));
this.frame.add(panel, BorderLayout.CENTER);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setTitle("emdb (ezon's movie database)");
Icon icon = new ImageIcon("images/search.png");
this.button = new JButton(icon);
this.button.setBounds(100, 20, 25, 25);
this.button.addActionListener(this);
this.searchBar = new JTextField(30);
this.searchBar.setBounds(100, 20, 200, 25);
this.name = new JLabel();
this.name.setBounds(50, 20, 80, 25);
ImageIcon log = new ImageIcon("images/logo.png");
this.logo = new JLabel(log);
this.logo.setBounds(100, 0, 300, 150);
}
public void clearScreen() {
this.panel.removeAll();
this.panel.revalidate();
this.panel.repaint();
}
public void searchScreen() {
this.panel.add(this.searchBar);
this.panel.add(this.button);
this.panel.add(this.logo);
this.frame.pack();
this.frame.setVisible(true);
}
public void searchedScreen() {
this.panel.add(this.name);
}
public String getMsg() {
return(this.msg);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Searched: " + this.searchBar.getText());
this.name.setText(this.searchBar.getText());
this.msg = this.searchBar.getText();
clearScreen();
searchedScreen();
}

Swing was designed to be used with layout managers.
even if i set a specific location.
The job of the layout manager is to set the size and location of the component based on the rules of the layout manager. So your setBounds() statements will be overridden. Don't attempt to use setBounds().
the button and the searchbar are right next to each other
The default layout manager for a JPanel is the FlowLayout, which does exactly that.
this.frame.add(panel, BorderLayout.CENTER);
Change the above statement to:
this.frame.add(panel, BorderLayout.PAGE_START);
to see a difference.
Read the section from the Swing tutorial on Layout Managers for more information and examples. Decide which layout manager (or combination of layout managers on different panels) will achieve your desired layout.

Related

Java - Can't see JRadioButtons

I am a beginner and I was practicing JRadioButtons. I realised that I can't see my JRadioButtons if I'll not set my layout as 'FlowLayout()'. I want to set the location of the buttons by myself.
I posted my code below, can anyone help me what am I doing wrong?
Thanks!
private JFrame frame;
private JPanel panel;
private JRadioButton btn1, btn2;
public JBButtons() {
form();
radioButtons();
frame.add(panel);
frame.setVisible(true);
}
public void form(){
frame = new JFrame("Frame");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
//panel.setLayout(new FlowLayout());
}
public void radioButtons() {
ButtonGroup group = new ButtonGroup();
btn1 = new JRadioButton("btn1");
btn1.setSelected(true);
btn1.setLocation(50, 50);
btn2 = new JRadioButton("btn2");
btn2.setLocation(50, 70);
group.add(btn1);
group.add(btn2);
panel.add(btn1);
panel.add(btn2);
}
public static void main(String[] args) {
new JBButtons();
}
The issue with absolute positioning or null layout is - it requires you to set the sizes of all your components, otherwise they will stay their default zero-size and won't show up. Better use a layout manager - https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Add this part of the code when adding the RadioButton to the panel, use the property .setBounds(x, y, width, heigth).
panel.add(btn1);
btn1.setBounds(90, 59, 93, 23);
panel.add(btn2);
btn2.setBounds(180, 60, 93, 23);

how to make jpanel in jframe visible

I try to write a menue for a little game in Java. I thought it would be a good idea to have a Window class (extending JFrame) and then put a JPanel in it for the different Screens (Menue, Game, GameOver etc)
If I put the buttons and stuff directly in the JFrame everything is shwown correct, but when I try to put a JPanel into the JFrame it doesn't work. Here is the code:
public class Window extends JFrame{
private final int WIDTH = 800;
private final int HEIGTH = 600;
private final int QUADRAT = 50;
JButton startButton;
JButton exitButton;
JButton anleitungButton;
JLabel gameTitle;
public Window() {
super("Study Run");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(WIDTH, HEIGTH);
setResizable(false);
getContentPane().add(new MenuePanel());
setVisible(true);
setLocationRelativeTo(null);
}
And this is my Panel:
public class MenuePanel extends JPanel{
JButton startButton;
JButton exitButton;
JButton anleitungButton;
JLabel gameTitle;
public MenuePanel() {
super();
setBackground(Color.CYAN);
gameTitle = new JLabel("StudyRun", SwingConstants.CENTER);
gameTitle.setBounds(200, 25, 400, 75);
gameTitle.setFont(new Font("Arial", Font.ITALIC, 36));
add(gameTitle);
startButton = new JButton("start");
startButton.setBounds(325, 125, 150, 50);
add(startButton);
anleitungButton = new JButton("anleitung");
anleitungButton.setBounds(325, 200, 150, 50);
add(anleitungButton);
exitButton = new JButton("exit");
exitButton.setBounds(325, 450, 150, 50);
add(exitButton);
CloseListener closeListener = new CloseListener();
StartListener startListener = new StartListener();
AnleitungListener anleitungListener = new AnleitungListener();
startButton.addActionListener(startListener);
anleitungButton.addActionListener(anleitungListener);
exitButton.addActionListener(closeListener);
}
The only help I found online was, that I needed to add the panel before I set the frame visible. That didn't work. Putting pack() or revalidate() anywhere in the code didn't work either. Also setting the Panel on opaque or visible didn't do anything. I don't know what else to try?!
Your problem is here:
setLayout(null);
When you use null layouts, you the coder are completely responsible for the location and size of all added components. Your added component has no size and so defaults to 0, 0.
A (bad) solution: give the MenuePanel a size or bounds
A much better solution: learn and use the layout managers (as all your searches most assuredly already told you).
It's best to remember that Java uses Flowlayout() as a default.
public Window() {
setLayout(new FlowLayout());
}
So your basically overwriting the layout to null as explained in the previous answer.Also if you plan to use different class and panels to add to a JFrame from different classes use a getter
class SomePanel{
public JComponent getPanel(){
return panel;
}
}
Then add to JFrame..
class MyFrame{
add(new SomePanel().getPanel());
}

Java add a JLabel in the middle of a JButton

I would like to simply add a JLabel into a JButton and center it horizontally. I've tried many thing but the text stay left side... Here is my code :
JLabel labeltest = new JLabel("Simple test");
JButton myButton = new JButton();
myButton.setHorizontalTextPosition(SwingConstants.CENTER);
myButton.add(labeltest);
Create an Icon of the text and give the Icon the foreground/background colors that you want.
I tried this but the problem comes when I had to do button.setenabled(false); after this the text becomes almost invisible
You can set the disabled Icon to be the same as the Icon and it will not be painted in the disabled state:
JButton button = new JButton( new ImageIcon(...) );
button.setDisabledIcon( button.getIcon() );
button.setEnabled(false);
Of course the problem with this approach is that the user doesn't know the button is disabled. So in reality you would need 2 icons:
one for the normal state
one for the disabled state
A JButton is also a java.awt.Container. Thus you can set a layout manager. E.g. you can use a GridBagLayout.
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JToggleButton toggleButton = new JToggleButton();
JLabel jLabel = new JLabel("3");
JToggleButton.ToggleButtonModel toggleButtonModel = (JToggleButton.ToggleButtonModel) toggleButton.getModel()
ToggleForegroundAction toggleForegroundAction =
new ToggleForegroundAction(toggleButtonModel, Color.WHITE, Color.RED);
toggleForegroundAction.setComponent(jLabel);
toggleButton.setAction(toggleForegroundAction);
toggleButton.setLayout(new GridBagLayout());
toggleButton.add(jLabel, new GridBagConstraints());
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.setLayout(new BorderLayout());
panel.add(toggleButton, BorderLayout.CENTER);
Container contentPane = frame.getContentPane();
contentPane.add(panel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
An action that toggles the label's foreground color might look like this
public class ToggleForegroundAction extends AbstractAction {
private JComponent component;
private JToggleButton.ToggleButtonModel toggleButtonModel;
private final Color selectedColor;
private final Color unselectedColor;
public ToggleForegroundAction(JToggleButton.ToggleButtonModel toggleButtonModel, Color selectedColor, Color unselectedColor) {
this.toggleButtonModel = toggleButtonModel;
this.selectedColor = selectedColor;
this.unselectedColor = unselectedColor;
}
public void setComponent(JComponent component) {
this.component = component;
setForeground(component, toggleButtonModel.isSelected());
}
#Override
public void actionPerformed(ActionEvent e) {
JComponent targetComponent = this.component;
if (targetComponent == null) {
targetComponent = (JComponent) e.getSource();
}
setForeground(targetComponent, toggleButtonModel.isSelected());
}
private void setForeground(JComponent targetComponent, boolean isSelected) {
Color foreground;
if (isSelected) {
foreground = selectedColor;
} else {
foreground = unselectedColor;
}
targetComponent.setForeground(foreground);
}
}
=>

Set Layout of panel within CardLayout

I am trying to create a UI for an imaginary vehicle that has both Automatic and Manual modes. When the user sets the vehicle into one of the modes, it should only display the controls relevant to that mode, and I've accomplished this using a CardLayout.
However, I'd also like to be able to specify the location of the various elements of the layout for each card manually - for a static layout I'd do something along the lines of mainPanel.setLayout(null), but this simply gives a blank window when used on a CardLayout (hence the two commented-out lines in the code below).
How would I achieve both of these things? My current code is below:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UI extends JFrame implements ActionListener{
public UI() {
initUI();
}
private JPanel cardPanel;
private CardLayout cardLayout = new CardLayout();
public final void initUI() {
cardPanel = new JPanel();
cardPanel.setLayout(cardLayout);
JPanel manualPanel = new JPanel();
getContentPane().add(manualPanel);
//manualPanel.setLayout(null);
cardPanel.add(manualPanel, "manual");
JPanel autoPanel = new JPanel();
//autoPanel.setLayout(null);
cardPanel.add(autoPanel, "auto");
JButton startButton = new JButton("START/STOP");
startButton.setBounds(50, 150, 200, 50);
startButton.addActionListener(new startListener());
manualPanel.add(startButton);
autoPanel.add(startButton);
JButton autoButton = new JButton("SWITCH TO AUTO");
autoButton.setBounds(50, 250, 200, 50);
autoButton.addActionListener(new autoListener());
manualPanel.add(autoButton);
JButton upButton = new JButton("^");
upButton.setBounds(125, 320, 50, 50);
upButton.addActionListener(new returnListener());
manualPanel.add(upButton);
JButton downButton = new JButton("\\/");
downButton.setBounds(125, 380, 50, 50);
downButton.addActionListener(new returnListener());
manualPanel.add(downButton);
JButton ccwButton = new JButton("<-");
ccwButton.setBounds(55, 350, 50, 50);
ccwButton.addActionListener(new returnListener());
manualPanel.add(ccwButton);
JButton cwButton = new JButton("->");
cwButton.setBounds(195, 350, 50, 50);
cwButton.addActionListener(new returnListener());
manualPanel.add(cwButton);
JButton ngzButton = new JButton("SOMETHING ELSE");
ngzButton.setBounds(50, 450, 200, 50);
ngzButton.addActionListener(new returnListener());
manualPanel.add(ngzButton);
JButton manualButton = new JButton("SWITCH TO MANUAL");
manualButton.setBounds(50, 250, 200, 50);
manualButton.addActionListener(new manualListener());
autoPanel.add(manualButton);
JButton returnButton = new JButton("SOMETHING ELSE");
returnButton.setBounds(50, 350, 200, 50);
returnButton.addActionListener(new returnListener());
autoPanel.add(returnButton);
setTitle("UI");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(cardPanel, BorderLayout.NORTH);
}
public static void main(String[] args) {
UI ui = new UI();
ui.setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
private class returnListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
}
}
private class autoListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
cardLayout.show(cardPanel, "auto");
}
}
private class startListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
}
}
private class manualListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
cardLayout.show(cardPanel, "manual");
}
}
}
In your example, you create a startButton, but you then attempt to add the same instance to two different panels. Because a component can only occupy one container, you'll need to create two buttons, one for each panel.
As an aside, instead of using a null layout, give each panel BorderLayout, add the buttons to a JPanel having the default FlowLayout, and add the button panel to the SOUTH. You can then nest your illustrations in the CENTER using whatever layout is appropriate.
Addendum: As #Frakcool comments, using a layout will improve the cross-platform appearance of your buttons. Invoke pack() on the enclosing window, and override getPreferredSize() on the nested illustration panel to give it the needed size. In this related example, the CENTER panel is used for drawing only; having no components, its layout then becomes irrelevant.

Setting radio buttons to a background

I was trying to set Radio Button to a background in order to allow the user to select.
Here is the code ..
public class FirstWindow extends JFrame {
private JTextField search;
private JRadioButton author,title,both;
private ButtonGroup grp;
public FirstWindow() {
super("My App");
setLayout(new BorderLayout());
JLabel backGround = new JLabel(new ImageIcon("C:\\Users\\Kareem Abdo\\Desktop\\3.Jpg"));
backGround.setLayout(null);
add(backGround);
search = new JTextField("Search...");
search.setFont(new Font("Arial",Font.PLAIN,16));
search.setSize(150, 30);
search.setLocation(20, 20);
backGround.add(search);
author = new JRadioButton("Author",true);
author.setLocation(20, 25);
backGround.add(author);
title = new JRadioButton("Title",false);
title.setLocation(25, 25);
backGround.add(title);
both = new JRadioButton("Both",false);
both.setLocation(250, 250);
backGround.add(both);
grp = new ButtonGroup();
grp.add(author);
grp.add(title);
grp.add(both);
But the radio buttons doesn't appear on the screen!
JLabel (JLabel backGround = new JLabel) haven't implemented any LayoutManager, then you have to set the proper one, otherwise any JComponent added to JLabel isn't visible
maybe better could be start with Image (BufferedImage) painted in paintComponent to the JPanel (pre_implemented FlowLayout in API)

Categories

Resources