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 !
Related
I made a JPanel, which has 6 string JLabels, and one image JLabel. What I need is to be able to position the Jlabel strings somewhere inside of my image JLabel's bounds. I'm pretty sure my problem would be solved if I knew how to resize JLabels.
Some Code:
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("rainbow seizure");
frame.setResizable(false);
frame.setSize(500, 500);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.SOUTH);
JButton newCardButton = new JButton("New Card");
newCardButton.addActionListener(actionEvent -> {
JPanel panel2 = new JPanel();
panel2.setLayout(new OverlayLayout(panel2));
JLabel jImage = new JLabel(new ImageIcon("C:\\Users\\unkno\\java\\Card1.PNG"));
Dimension dimension = jImage.getPreferredSize(); // dimensions for me are 72x122
System.out.println(dimension.width);
System.out.println(dimension.height);
JLabel label1 = new JLabel("name");
label1.setBounds(new Rectangle(dimension.width, dimension.height)); // doesn't do anything :(
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label1.setAlignmentY(Component.TOP_ALIGNMENT);
JLabel label2 = new JLabel("cost");
JLabel label3 = new JLabel ("atk");
JLabel label4 = new JLabel("type");
JLabel label5 = new JLabel ("hp");
JLabel label6 = new JLabel("disc");
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);
panel2.add(label4);
panel2.add(label5);
panel2.add(label6);
panel2.add(jImage);
panel.add(panel2);
frame.revalidate();
});
frame.add(newCardButton, BorderLayout.NORTH);
}
}
I can't seem to figure out why my JFrame is empty. Where am I going wrong?
import javax.swing.*;
import java.awt.FlowLayout;
public class GUIExample extends JFrame {
JCheckBox box1 = new JCheckBox("Satellite Radio");
JCheckBox box2 = new JCheckBox("Air Conditioning");
JCheckBox box3 = new JCheckBox("Manual Tranmission");
JCheckBox box4 = new JCheckBox("Leather Seats");
JRadioButton radio1 = new JRadioButton("Car");
JRadioButton radio2 = new JRadioButton("Pickup Truck");
JRadioButton radio3 = new JRadioButton("Minivan");
JTextField text = new JTextField();
ButtonGroup group = new ButtonGroup();
public void newGUI() {
setLayout(new FlowLayout());
JPanel panel = new JPanel();
JPanel textPanel = new JPanel();
add(textPanel);
add(panel);
panel.add(box1);
panel.add(box2);
panel.add(box3);
panel.add(radio1);
panel.add(radio2);
panel.add(radio3);
group.add(radio1);
group.add(radio2);
group.add(radio3);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GUI Example");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
You forgot to add the contentPane in your jFrame, something like this
frame.setContentPane(panel);
I notice you're using inheritance to build your jFrame, so in this case you need to instantiate your own class. I've refactored your code with the minimun to run an jFrame.
public class GUIExample extends JFrame {
JCheckBox box1 = new JCheckBox("Satellite Radio");
public static void main(String[] args) {
JFrame frame = new GUIExample("GUI Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(box1);
frame.setContentPane(panel);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Basically you create an JFrame, create an JPanel, add your components to this panel and set the panel to your frame with setContentPane(panel).
I'm sorry I can't test this right now, so if someone could and fix if needed, would really appreciate, but is something like this.
First of all this is quite a good try to what you are trying to do. However there seem to be some basic misunderstandings as to how you are coding the GUI.
There are two ways of making a GUI in java. Either you create frames and panel objects then add your components to them, or you can create and extend your own classes by extends JFrame. But in this case you have tried to do both. You have extended JFrame and you have created the Objects.
I actually quite like how you've tried to extended the JFrame, so I've continued this and made code that add stuff to your screen!
See below - GUIExample.java:
import javax.swing.*;
import java.awt.FlowLayout;
public class GUIExample extends JFrame {
JCheckBox box1 = new JCheckBox("Satellite Radio");
JCheckBox box2 = new JCheckBox("Air Conditioning");
JCheckBox box3 = new JCheckBox("Manual Tranmission");
JCheckBox box4 = new JCheckBox("Leather Seats");
JRadioButton radio1 = new JRadioButton("Car");
JRadioButton radio2 = new JRadioButton("Pickup Truck");
JRadioButton radio3 = new JRadioButton("Minivan");
JTextField text = new JTextField();
ButtonGroup group = new ButtonGroup();
GUIExample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
newGUI();
}
public void newGUI() {
setLayout(new FlowLayout());
JPanel panel = new JPanel();
JPanel textPanel = new JPanel();
add(textPanel);
add(panel);
panel.add(box1);
panel.add(box2);
panel.add(box3);
panel.add(radio1);
panel.add(radio2);
panel.add(radio3);
group.add(radio1);
group.add(radio2);
group.add(radio3);
}
}
test.java:
public class test {
public static void main(String[] args) {
GUIExample e = new GUIExample();
}
}
To get this working. Compile both of the files and then run your program through test.java.
Moreover your original solution, nowhere did you add a newGUI() call so all that code becomes redundant. However as the main method is static, you wouldn't be able to call it anyways.
I hope this helps!
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.
This code displays nothing, I have exhausted many avenues but it does not display anything on the GUI (I have a main class that calls this as well already). Please help. I am trying to put the two JButtons horizontally at the bottom of the page and the JTextField and JLabel at the center of the screen.
package test;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("");
textfield = new JTextField("enter text here");
JPanel bottom = new JPanel(new BorderLayout());
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel (new BorderLayout());
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom, BorderLayout.PAGE_END);
centre.add(subCentre, BorderLayout.CENTER);
}
}
Your code is a bit over-complicated. You only need two panels, centre, and buttons. There are two reasons your UI is not showing up:
You never added the panels to the frame
You never set visible to true(Achieve this by using setVisible(true)), unless you did this in the class you ran it in.
One simple way to achieve your desired UI is like so(I added a main method to show the window):
import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
public Test() {
super("test");
JPanel buttons = new JPanel();
JPanel centre = new JPanel();
add(buttons, BorderLayout.SOUTH); //these lines add the
add(centre, BorderLayout.CENTER); //panels to the frame
JButton clear = new JButton("Clear"); // No need
JButton copy = new JButton("Copy"); // to declare
JLabel label = new JLabel("Label"); // these
JTextField textfield = new JTextField("enter text here"); // privately
buttons.add(copy);
buttons.add(clear);
centre.add(label);
centre.add(textfield);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} //end constructor
//added main method to run the UI
public static void main(String[] args) {
new Experiments();
} //end main
} //end class
And it shows the window:
I got closer but its not pretty code, the JFrame is 500x500 so this works based on that... any better suggestions than what I have?
package lab6;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("label");
textfield = new JTextField("enter text here");
JPanel masterPanel = new JPanel(new BorderLayout());
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(100, 200));
JPanel bottom = new JPanel();
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel ();
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom);
centre.add(subCentre);
masterPanel.add(bottom, BorderLayout.PAGE_END);
masterPanel.add(top, BorderLayout.PAGE_START);
masterPanel.add(centre, BorderLayout.CENTER);
add(masterPanel);
}
}
UPDATE UPDATE UPDATE
thank you :))) I did What u told me
I put frame.add(FirstScreen) first
they appeared .....
but now the events are not working , why???????
Can u help me again???
I'm sorry ........
..................
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class InterFace extends JFrame implements ActionListener,ItemListener
{
JFrame frame = new JFrame("Al-murshed Dictionary");
JPanel FirstScreen = new JPanel();
JPanel SecondScreen = new JPanel();
JPanel ThirdScreen = new JPanel();
JPanel ForthScreen = new JPanel();
JButton Translate = new JButton ("Translate");
JButton About = new JButton ("About");
JButton Help= new JButton ("Help");
JButton Quit= new JButton ("Quit");
JButton Quit1= new JButton ("Quit");
JButton Quit2= new JButton ("Quit");
JButton Back= new JButton ("Back");
JButton Back1= new JButton ("Back");
JTextField WordField = new JTextField("Write Your Word Here",50);
JTextArea ArbField = new JTextArea(40,40);
JTextArea EngField = new JTextArea(40,40);
CardLayout c1 = new CardLayout ();
public InterFace()
{
FirstScreen.setLayout(c1);
SecondScreen.add(WordField);
SecondScreen.add(Translate);
ThirdScreen.add(Back);
ForthScreen.add(Back1);
ThirdScreen.add(Quit1);
ForthScreen.add(Quit2);
FirstScreen.add(SecondScreen,"1");
FirstScreen.add(ThirdScreen,"2");
FirstScreen.add(ForthScreen,"3");
JPanel controlButtons = new JPanel();
controlButtons.add(Help);
controlButtons.add(About);
controlButtons.add(Quit);
JPanel wordTranslate = new JPanel();
wordTranslate.add(WordField);
wordTranslate.add(Translate);
JPanel controlTextArea = new JPanel();
controlTextArea.add(EngField);
controlTextArea.add(ArbField);
c1.show(FirstScreen,"1");
About.addActionListener(this);
Back.addActionListener(this);
Help.addActionListener(this);
Back1.addActionListener(this);
Quit.addActionListener(this);
Quit1.addActionListener(this);
Quit2.addActionListener(this);
frame.add(FirstScreen);
Container pane = frame.getContentPane();
pane.add(wordTranslate, BorderLayout.NORTH);
pane.add(controlTextArea, BorderLayout.CENTER);
pane.add(controlButtons, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
//EventHandler
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==About)
c1.show(FirstScreen,"2");
if(e.getSource()==Help)
c1.show(FirstScreen,"3");
if(e.getSource()==Quit)
System.exit(0);
if(e.getSource()==Quit1)
System.exit(0);
if(e.getSource()==Quit2)
System.exit(0);
if(e.getSource()==Back)
c1.show(FirstScreen,"1");
if(e.getSource()==Back1)
c1.show(FirstScreen,"1");
}
public static void main (String args[])
{
InterFace d = new InterFace ();
}
}
pane.add(controlTextArea, BorderLayout.CENTER);
...
frame.add(FirstScreen);
First you add the text area panel to the content pane.
Then you add the "FirstScreen" to the frame.
The problem is that when you add the "FirstScreen" to the frame you are really adding it to the content pane of the frame. So basically you are replacing the text area panel with the first screen.
Also, follow Java naming conventions. Variable names should NOT start with an upper case character.