I want to be able to position my two JLabels but when I change the values in the position lines it does nothing. Also when I run it only the second label is displayed.
My code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class cubeTimerClass {
public static void main(String[] args) {
window(); //Runs the window method
}
public static void window() {
//Create a window
JFrame window = new JFrame(); //Create the window object
window.setSize(900, 600); //Set the size of the window
window.setTitle("Cube Timer"); //Set the title of the window
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the program to quit when user closes the window
window.setVisible(true); //Make the window visible
//Create a label
JLabel label1 = new JLabel(""); //Create the label1 object
label1.setText("Message 1"); //Set the text for label1
label1.setAlignmentX(0);
label1.setAlignmentY(0);
window.add(label1); //Place the label on the window
//Create a label
JLabel label2 = new JLabel(""); //Create the label2 object
label2.setText("Message 2"); //Set the text for label2
label2.setAlignmentX(0);
label2.setAlignmentY(50);
window.add(label2); //Place the label on the window
}
}
Check with this -
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel) frame.getContentPane();
panel.setLayout(null);
JLabel label = new JLabel("Java");
panel.add(label);
Dimension size = label.getPreferredSize();
label.setBounds(90, 100, size.width, size.height);
frame.setSize(300, 200);
frame.setVisible(true);
Related
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.
I am trying to set the width of the JTextField but non of the methods to change it's size are working I have tried to change the amount of columns to change the width of the JTextField but it's not working?
What is causing this?
And how will I be able to change the width?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class main {
public static void main(String[] args) {
//Create and set up the window.
final JFrame frame = new JFrame("Layout 1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 1500, 1000);
frame.setBackground(new Color(255,255,255));
frame.setLayout(null);
Container pane = frame.getContentPane();
pane.setLayout(null);
JLayeredPane jlp = new JLayeredPane();
frame.setContentPane(jlp);
JPanel container = new JPanel();
//container.setBackground(new Color(241,241,241));
container.setBackground(Color.RED);
container.setBounds(0,0,frame.getWidth(), 75);
container.setLayout(null);
JPanel containerA = new JPanel();
containerA.setBackground(Color.blue);
containerA.setBounds(90 ,10,50,50);
JPanel containerB = new JPanel();
containerB.setBackground(Color.RED);
containerB.setBounds(95 ,20,50,50);
//This is the top textfield
JTextField textField = new JTextField(1000);
textField.setLayout(null);
textField.setPreferredSize(new Dimension(1000,1000));
textField.setBounds(0,0,2300,50);
textField.setColumns(1000);
textField.setSize(1000,10);
textField.invalidate();
textField.setMinimumSize(new Dimension(1000,100));
container.add(textField);
jlp.add(container, Integer.valueOf(2));
jlp.add(containerA, Integer.valueOf(1));
jlp.add(containerB, Integer.valueOf(3));
container.setVisible(true);
frame.invalidate();
frame.setVisible(true);
}
}
You need to change just this line, and will get width of text field,
textField.setSize(1000,25);
I need to make a scrolling background for this platformer. It needs to scroll a 2400x500 image while the frame size is about 1440x900. If it could just gradually change over time that would be great. This is my code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class Main extends JFrame {
public Main() {
//Creates Title Image
JLabel title = new JLabel(" ");
ImageIcon tl = new ImageIcon("title.gif");
title.setIcon(tl);
//Creates Start Image
final JButton start = new JButton("");
ImageIcon st = new ImageIcon("start.gif");
start.setIcon(st);
//Creates Options Image
JButton options = new JButton("");
ImageIcon opt = new ImageIcon("options.gif");
options.setIcon(opt);
options.setBackground(Color.BLACK);
//Creates label for level 0 background image
JLabel background = new JLabel(" ");
ImageIcon back = new ImageIcon("level0.gif");
background.setIcon(back);
//Creates a panel for level 0
final JPanel p5 = new JPanel();
p5.setLayout (new BorderLayout(1, 1));
p5.add(background);
//Create first frame for "Start" button
final JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1));
p1.add(start, BorderLayout.CENTER);
//Create second panel for title label
final JPanel p2 = new JPanel(new BorderLayout());
p2.setLayout(new GridLayout(1, 3));
p2.add(title, BorderLayout.WEST);
//Create third panel for "Options" button
final JPanel p3 = new JPanel(new BorderLayout());
p3.setLayout(new GridLayout(1, 1));
p3.add(options, BorderLayout.SOUTH);
//Creates fourth panel to organize all other primary
final JPanel p4 = new JPanel(new BorderLayout());
p4.setLayout(new GridLayout(1, 3));
p4.add(p1, BorderLayout.WEST);
p4.add(p2, BorderLayout.CENTER);
p4.add(p3, BorderLayout.EAST);
//When button is clicked, it changes the level
start.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(start.isEnabled()) {
remove(p4);
add(p5, BorderLayout.CENTER);
invalidate();
validate();
}
else {
return;
}
}
});
//Adds fourth panel to frame
add(p4, BorderLayout.CENTER);
}
public static void main(String args[]) {
Main frame = new Main();
//Finds screen size of monitor
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//Creates the frame
frame.setTitle("Cockadoodle Duty: Awakening");
frame.setSize(screenSize);
frame.setLocale(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
String background = "#000000";
frame.setBackground(Color.decode(background));
}
}
Add the label containing the icon to a scroll panel
Use a Swing Timer to schedule the scrolling
When the Timer fires you can scroll background.
The scrolling code might be something like:
JViewport viewport = scrollPane.getViewport();
Point position = viewport.getViewPosition();
position.x += 2;
viewport.setViiewPosition( position );
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 !
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);
}