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);
}
Related
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();
}
});
}
}
I have a card layout where I switch panels with a button. However, the code (switching panels) works only when lines:
JScrollPane scrPane = new JScrollPane(card1);
frame.add(scrPane);
are removed. In other case, clicking button achieves nothing. Is there an option to keep the scrolling (I need this, since the main application will have a lot of wrapped text) without disabling an option to switch cards?
package com.code;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.event.*;
public class Card {
public static void main(String[] args) {
JFrame frame = new JFrame("App");
frame.setVisible(true);
frame.setSize(1200, 800);//Give it a size
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel mainPanel = new JPanel(new CardLayout());
frame.add(mainPanel);
JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel card1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel card2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
mainPanel.add(menu, "menu");
mainPanel.add(card1, "card1");
mainPanel.add(card2, "card2");
JLabel l1 = new JLabel("label 1");
JLabel l2 = new JLabel("label 2");
card1.add(l1);
card2.add(l2);
JButton click = new JButton("Click!");
menu.add(click);
JScrollPane scrPane = new JScrollPane(card1);
frame.add(scrPane);
click.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, "card1");
}
});
}
}
A JFrame (its content pane) uses BorderLayout by default. That means you can have only 1 component at BorderLayout.CENTER. When you frame.add(component) the default constraints is BorderLayout.CENTER.
Now, you frame.add(mainPanel); and then frame.add(scrPane);. So main panel is removed, since scrPane is being added after it.
Doing JScrollPane scrPane = new JScrollPane(card1); it means you add a scrollpane to card1, and not in content pane. I guess that you want it to the content pane (the whole frame). So the fix is to delete frame.add(mainPanel); and do the following:
JScrollPane scrPane = new JScrollPane(mainPanel);
frame.add(scrPane);
Now, the main panel is added to scrPane and scrPane is added to the frame.
However, your GUI will be empty after that, because you frame.setVisible(true); before you are finished adding components to it. Take a look at Why shouldn't I call setVisible(true) before adding components?
Eventually, full code is:
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("App");
frame.setSize(1200, 800);//Give it a size
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel mainPanel = new JPanel(new CardLayout());
JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel card1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel card2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
mainPanel.add(menu, "menu");
mainPanel.add(card1, "card1");
mainPanel.add(card2, "card2");
JLabel l1 = new JLabel("label 1");
JLabel l2 = new JLabel("label 2");
card1.add(l1);
card2.add(l2);
JButton click = new JButton("Click!");
menu.add(click);
JScrollPane scrPane = new JScrollPane(mainPanel);
frame.add(scrPane);
click.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, "card1");
}
});
frame.setVisible(true);
});
}
Some good links I suggest you to read are the Initial Threads and What does .pack() do?
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.
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.
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 !