How to position items relative to each other in a JFrame? - java

I am trying to figure out how to position the button relative to the text.
package windows;
import javax.swing.*;
public class window {
public static void main(String[] args) {
JFrame frame = new JFrame("Window");
JPanel panel = new JPanel();
JLabel label = new JLabel("Some Text");
JButton button = new JButton("Button");
panel.add(button);
panel.add(label);
frame.add(panel);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
In other words I want to position the button below wherever the text is.

Related

How do I add other swing components in a specific swing tabbed pane?

I have just started learning Java Swing and I am making a application form sort of project and I want to add more components like buttons,text areas and other in specific tab but I am not able to.
The code is given below:
import javax.swing.*;
public class TabbedPaneExample {
JFrame f;
TabbedPaneExample(){
f=new JFrame("Hotel Appication Form");
JTextArea ta=new JTextArea(400,400);
JPanel p1=new JPanel();
p1.add(ta);
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JTabbedPane tp=new JTabbedPane();
tp.setBounds(39,20,500,500);
tp.add("form",p1);
tp.add("preferences",p2);
tp.add("FaQ's",p3);
f.add(tp);
f.setSize(600,600);
f.setVisible(true);
//JButton
JButton b = new JButton("Submit");
b.setBounds(50,50,30,20);
f.add(b);
//JLabel
}
public static void main(String[] args) {
new TabbedPaneExample();
}
}
The screenshot of the output is attached here
In this code example simple frame with tabbed panel and simple components in each tab.
Your problem was incorrect adding components to JPanel.
Hope that helps you!
output1 output2
import java.awt.GridLayout;
import javax.swing.*;
public class test {
private static void createAndShowGUI() {
// Create and set up the window.
final JFrame frame = new JFrame("Split Pane Example");
// Display the window.
frame.setSize(500, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set grid layout for the frame
frame.getContentPane().setLayout(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JButton button = new JButton("Button");
JLabel label = new JLabel("Label");
JTextField textField = new JTextField("TextField");
panel.add(button);
panel.add(label);
panel2.add(textField);
tabbedPane.addTab("Tab1", panel);
tabbedPane.addTab("Tab2", panel2);
frame.getContentPane().add(tabbedPane);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

PictureCount does not change

the code is used to create an application which shows picture and if your anwser is correct your supposed to see the next picture but the pictureCount does not go up. all of the variables ar declared after the main class and i created an Actionlistener to check if the awnser is correct.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {new Main().test();}
public int pictureCount = 1;
JFrame frame = new JFrame();
JButton button1 = new JButton("Submit");
JTextField text = new JTextField();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JLabel label = new JLabel(new ImageIcon("C:\\Users\\Admin\\Desktop\\practicum 3\\" + pictureCount + ".jpg"));
void test(){
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(text.getText().equals("5")){
pictureCount++;
new Main().test();
}
}
});
panel1.add(button1);
panel2.add(text);
panel3.add(label);
text.setPreferredSize(new Dimension(100,50));
panel1.setPreferredSize(new Dimension(1000, 200));
panel2.setPreferredSize(new Dimension(1000, 100));
panel3.setPreferredSize(new Dimension(1000, 450));
frame.getContentPane().add(BorderLayout.SOUTH, panel1);
frame.getContentPane().add(BorderLayout.CENTER, panel2);
frame.getContentPane().add(BorderLayout.NORTH, panel3);
frame.setSize(1000,750);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Operation Screen");
frame.setLocationRelativeTo(null);
}
}
You need to read in all the pictures as ImageIcons into an array or ArrayList, say called imageIconArray and then display imageIconArray[0] in your JLabel when you start.
When the button is pressed, increment pictureCount, and then reset the JLabel's icon via its setIcon(...) method:
// in the ActionListener code:
pictureCount++;
label.setIcon(imageIconArray[pictureCount];
Whatever you do, don't create a new Main object, despite what others might say. Why create a new GUI when all you need to do is swap displayed images?

I want 2 JLabels to show on the same JFrame in Java

I can't get the JLabel pieces to both show up on the same JFrame. I've tried resizeing; but it didn't work. Do I need to place the JLabel's in a JPanel? I want both JLabels' to show on the same JFrame. Any help is appreciated. Thanks.
public class HelloWorldFrame extends JFrame
{
private TitledBorder title;
private TitledBorder title2;
public HelloWorldFrame()
{
super("Hello World! ");
JFrame helloWorld = new JFrame();
JLabel label = new JLabel();
title = BorderFactory.createTitledBorder("Language");
title.setTitleJustification(TitledBorder.LEFT);
label.setBorder(title);
add(label);
setSize(300, 200);
JRadioButton button1 = new JRadioButton("English");
JRadioButton button2 = new JRadioButton("French");
JRadioButton button3 = new JRadioButton("Spanish");
label.setLayout(new FlowLayout());
label.add(button1);
label.add(button2);
label.add(button3);
JLabel label2 = new JLabel();
title2 = BorderFactory.createTitledBorder("Greeting");
title2.setTitleJustification(TitledBorder.LEFT);
label2.setBorder(title2);
label2.setSize(100, 100);
add(label2);
helloWorld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//The main begins below
import javax.swing.JFrame;
public class HelloWorldApp
{
public static void main(String[] args)
{
JFrame helloWorld = new HelloWorldFrame();
helloWorld.setVisible(true);
}
}
You need layout manager like in this example.
The structure you should have is : JFrame > JPanel > JLabel.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label_1 = new JLabel("some text");
JLabel label_2 = new JLabel("other text");
panel.add(label_1);
panel.add(label_2);
frame.add(panel);
frame.pack();
frame.setVisible(true);
In your code you have some JButton: add them to the JPanel, not to the JLabel !

In a basic GUI, I can only see the frame

This is the information for what I think is a correct basic GUI, but only the frame is showing up. I don't know why this is. I have everything initialized and set to visible and added, but it acts like nothing is added. Thanks for the help!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class basicButtonPress
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton();
JLabel label = new JLabel();
frame = new JFrame("Test Pop - Up");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel = new JPanel();
panel.setBackground(Color.YELLOW);
panel.add(button);
panel.add(label);
button = new JButton("Test");
label = new JLabel("This is test label");
}
}
You're calling new JFrame() twice; same with JPanel, JButton and JLabel. Remove the duplicates and you'll be closer to fixing the problem.

Adding a label to a JComboBox and JTextField

Hi I am having trouble adding labels to my combo box and textfield.It compiles fine but only shows the boxes but without labels.
import javax.swing. *;
import java.awt.event. *;
import java.awt.FlowLayout;
public class AreaFrame2
{
public static void main(String[]args)
{
//Create array containing shapes
String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};
//Use combobox to create drop down menu
JComboBox comboBox=new JComboBox(shapes);
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("Select shape:");
panel1.add(label1);
comboBox.add(panel1);
JButton button = new JButton("GO");
JTextField text = new JTextField(20);
//Create a JFrame that will be use to put JComboBox into it
JFrame frame=new JFrame("Area Calculator Window");
frame.setLayout(new FlowLayout()); //set layout
frame.add(comboBox);//add combobox to JFrame
text.setLocation(100,100);
frame.add(text);
frame.add(button);
//set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set JFrame ssize
frame.setSize(250,250);
//make JFrame visible. So we can see it
frame.setVisible(true);
}
}
I think the following code will produce more or less what you expect.
public static void main(String[]args)
{
//Create array containing shapes
String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};
//Use combobox to create drop down menu
JComboBox comboBox=new JComboBox(shapes);
JPanel panel1 = new JPanel(new FlowLayout());
JLabel label1 = new JLabel("Select shape:");
panel1.add(label1);
panel1.add(comboBox);
JButton button = new JButton("GO");
JTextField text = new JTextField(20);
//Create a JFrame that will be use to put JComboBox into it
JFrame frame=new JFrame("Area Calculator Window");
frame.setLayout(new FlowLayout()); //set layout
frame.add(panel1);
frame.add(text);
frame.add(button);
//set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set JFrame ssize
frame.setSize(250,250);
//make JFrame visible. So we can see it
frame.setVisible(true);
}

Categories