I am new to programming and still learning. I need help in positioning a JLabel "Welcome back" on top of the GUI interface. The label keeps getting stuck in the middle despite my efforts. Feel free to correct any other mistakes shown in the code that I can do better.
public class MainMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
JPanel panel = new JPanel();
panel.setBounds(30, 80, 400, 570);
panel.setLayout(new GridLayout(3, 2, 15, 15));
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JButton meals = new JButton("Meals");
meals.setFont(new Font("Helvetica", Font.BOLD, 24));
meals.setOpaque(true);
JButton reminder = new JButton ("Reminders");
reminder.setFont(new Font("Helvetica", Font.BOLD, 24));
reminder.setOpaque(true);
JButton shop = new JButton ("Shop");
shop.setFont(new Font("Helvetica", Font.BOLD, 24));
shop.setOpaque(true);
JButton sleep = new JButton ("Sleep Timer");
sleep.setFont(new Font("Helvetica", Font.BOLD, 24));
sleep.setOpaque(true);
JButton account = new JButton ("My Account");
account.setFont(new Font("Helvetica", Font.BOLD, 24));
account.setOpaque(true);
JButton aboutus = new JButton ("About Us");
aboutus.setFont(new Font("Helvetica", Font.BOLD, 24));
aboutus.setOpaque(true);
JLabel label = new JLabel("Welcome back!");
label.setLocation(240, 20);
panel.add(meals);
panel.add(reminder);
panel.add(shop);
panel.add(sleep);
panel.add(account);
panel.add(aboutus);
frame.add(panel);
frame.add(label);
frame.setSize(480, 720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Panels are rearranged on an intermediary Layout-manager(Border)
There are also various solutions based on Layout-used, but the main idea is the same
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
//define a new layout
frame.setLayout(new BorderLayout());
//panel for label
JPanel topPanel = new JPanel();
JPanel panel = new JPanel();
panel.setBounds(30, 80, 400, 570);
panel.setLayout(new GridLayout(3, 2, 15, 15));
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JButton meals = new JButton("Meals");
meals.setFont(new Font("Helvetica", Font.BOLD, 24));
meals.setOpaque(true);
JButton reminder = new JButton ("Reminders");
reminder.setFont(new Font("Helvetica", Font.BOLD, 24));
reminder.setOpaque(true);
JButton shop = new JButton ("Shop");
shop.setFont(new Font("Helvetica", Font.BOLD, 24));
shop.setOpaque(true);
JButton sleep = new JButton ("Sleep Timer");
sleep.setFont(new Font("Helvetica", Font.BOLD, 24));
sleep.setOpaque(true);
JButton account = new JButton ("My Account");
account.setFont(new Font("Helvetica", Font.BOLD, 24));
account.setOpaque(true);
JButton aboutus = new JButton ("About Us");
aboutus.setFont(new Font("Helvetica", Font.BOLD, 24));
aboutus.setOpaque(true);
JLabel label = new JLabel("Welcome back!");
panel.add(meals);
panel.add(reminder);
panel.add(shop);
panel.add(sleep);
panel.add(account);
panel.add(aboutus);
topPanel.add(label);
topPanel.setSize(200, 30);
//rearrange panels on frame
frame.add(topPanel, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(480, 720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output:
Related
Days ago i asked for help to put a JPanel on top of the other CardLayout panels, with the help of one of the users i achieved it using GlassPane, so thanks to him, but now i wanna close it whenever i click outside it(in other windows of the applications or components) because its stuck there until i click settings button, how can i achieve that? I have tried with focus lost and gained but that doesn't work with the panel so what am i supposed to do, here is my piece of code..
JPanel settingsPanel = new JPanel();
settingsPanel.setLayout(null);
settingsPanel.setPreferredSize(
new Dimension(180, 260));
JLabel lblSettingsTitle = new JLabel("Settings");
lblSettingsTitle.setFont(new Font("SansSerif", Font.BOLD |
Font.ITALIC, 18));
lblSettingsTitle.setBounds(5, 8, 200, 35);
settingsPanel.add(lblSettingsTitle);
JSeparator settingsSep = new JSeparator();
settingsSep.setForeground(Color.DARK_GRAY);
settingsSep.setBounds(0, 48, 160, 2);
settingsPanel.add(settingsSep);
JPanel panelLanguage = new JPanel();
panelLanguage.setBounds(0, 61, 200, 30);
settingsPanel.add(panelLanguage);
panelLanguage.setLayout(null);
JPanel pnlLanguage = new JPanel();
pnlLanguage.setBounds(0, 0, 200, 30);
panelLanguage.add(pnlLanguage);
pnlLanguage.setLayout(null);
JLabel lblLanguageIcon = new JLabel("");
lblLanguageIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/Language_20px.png")));
lblLanguageIcon.setBounds(5, 5, 20, 20);
pnlLanguage.add(lblLanguageIcon);
JLabel lblLanguage = new JLabel("Choose Language");
lblLanguage.setFont(new Font("SansSerif", Font.ITALIC, 15));
lblLanguage.setBounds(30, 0, 170, 30);
pnlLanguage.add(lblLanguage);
JPanel pnlAlbanian = new JPanel();
pnlAlbanian.setBounds(0, 30, 200, 30);
panelLanguage.add(pnlAlbanian);
pnlAlbanian.setLayout(null);
JLabel lblAlbIcon = new JLabel("");
lblAlbIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/albanian.png")));
lblAlbIcon.setBounds(20, 0, 30, 30);
pnlAlbanian.add(lblAlbIcon);
JLabel lblAlbanian = new JLabel("Albanian");
lblAlbanian.setBounds(50, 0, 150, 30);
pnlAlbanian.add(lblAlbanian);
JPanel pnlEnglish = new JPanel();
pnlEnglish.setBounds(0, 60, 200, 30);
panelLanguage.add(pnlEnglish);
pnlEnglish.setLayout(null);
JLabel lblEngIcon = new JLabel("");
lblEngIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/britain.png")));
lblEngIcon.setBounds(20, 0, 30, 30);
pnlEnglish.add(lblEngIcon);
JLabel lblEnglish = new JLabel("English");
lblEnglish.setBounds(50, 0, 150, 30);
pnlEnglish.add(lblEnglish);
JPanel pnlAboutUs = new JPanel();
pnlAboutUs.setBounds(0, 120, 200, 30);
settingsPanel.add(pnlAboutUs);
pnlAboutUs.setLayout(null);
JLabel lblAboutIcon = new JLabel("");
lblAboutIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/About_20px.png")));
lblAboutIcon.setBounds(5, 5, 20, 20);
pnlAboutUs.add(lblAboutIcon);
JLabel lblAboutUs = new JLabel("About Us");
lblAboutUs.setFont(new Font("SansSerif", Font.ITALIC, 15));
lblAboutUs.setBounds(35, 0, 165, 30);
pnlAboutUs.add(lblAboutUs);
JPanel pnlHelp = new JPanel();
pnlHelp.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
Help obj=new Help();
getGlassPane().setVisible(false);
obj.setVisible(true);
obj.setLocationRelativeTo(null);
}
});
pnlHelp.setLayout(null);
pnlHelp.setBounds(0, 91, 200, 30);
settingsPanel.add(pnlHelp);
JLabel lblHelpIcon = new JLabel("");
lblHelpIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/Help_20px.png")));
lblHelpIcon.setBounds(5, 5, 20, 20);
pnlHelp.add(lblHelpIcon);
JLabel lblHelp = new JLabel("Help");
lblHelp.setFont(new Font("SansSerif", Font.ITALIC, 15));
lblHelp.setBounds(35, 0, 165, 30);
pnlHelp.add(lblHelp);
((JComponent) getGlassPane()).setLayout(new
FlowLayout(FlowLayout.LEFT, 260, 390));
((JComponent) getGlassPane()).add(settingsPanel, BorderLayout.EAST);
JLabel lblSettings = new JLabel("");
lblSettings.setHorizontalAlignment(SwingConstants.CENTER);
lblSettings.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
getGlassPane().setVisible(!getGlassPane().isVisible());
}
});
lblSettings.setToolTipText("Settings");
lblSettings.setIcon(new
ImageIcon(frmMain.class.getResource("/image/Settings_24px.png")));
lblSettings.setBounds(210, 600, 50, 50);
menuPanel.add(lblSettings);
I've slightly modified my previous example, so the settings panel goes closed on the click outside it.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class RightSidePanel implements Runnable {
#Override
public void run() {
JFrame frm = new JFrame("Right side panel");
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// next two lines are not required
JPanel contentPanel = new JPanel(new BorderLayout());
frm.setContentPane(contentPanel);
JPanel mainPanel = new JPanel(new CardLayout());
mainPanel.add(new JLabel("It's the first card panel"), "first");
mainPanel.add(new JLabel("It's the second card panel"), "second");
// add some components to provide some width and height for the panel.
mainPanel.add(Box.createHorizontalStrut(600));
mainPanel.add(Box.createVerticalStrut(300));
mainPanel.setBackground(Color.CYAN);
JPanel settingsPanel = new JPanel(new GridLayout(1, 1));
settingsPanel.add(new JLabel("Here is the settings panel!"));
settingsPanel.setPreferredSize(new Dimension(settingsPanel.getPreferredSize().width, 300));
JButton settingsButton = new JButton("Show settings"); // move this line up
((JComponent) frm.getGlassPane()).setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
((JComponent) frm.getGlassPane()).add(settingsPanel);
// added code here
frm.getGlassPane().addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
// check whether the click on the glass pane.
Component c = SwingUtilities.getDeepestComponentAt(frm.getGlassPane(), e.getX(), e.getY());
if (e.getComponent().equals(c)) {
updateButton(frm, settingsButton);
}
}
});
// end of the added code
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
settingsButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// move the method content to a separate method
updateButton(frm, settingsButton);
}
});
JButton switchButton = new JButton("Show second");
switchButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) mainPanel.getLayout();
if (mainPanel.getComponent(0).isVisible()) {
cl.show(mainPanel, "second");
switchButton.setText("Show first");
} else {
cl.show(mainPanel, "first");
switchButton.setText("Show second");
}
}
});
buttonPanel.add(switchButton);
buttonPanel.add(settingsButton);
frm.add(mainPanel, BorderLayout.CENTER);
frm.add(buttonPanel, BorderLayout.SOUTH);
frm.pack();
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
private void updateButton(JFrame frm, JButton settingsButton) {
frm.getGlassPane().setVisible(!frm.getGlassPane().isVisible());
if (frm.getGlassPane().isVisible()) {
settingsButton.setText("Hide settings");
} else {
settingsButton.setText("Show settings");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new RightSidePanel());
}
}
Im trying to make a game.
I have a frame and insert jbutton jlabel jtextfield on the frame and the frame has a background.
But now it doesnt appear.
Why isnt buttons etc visible? Can you fix these codes?
public class NewPlayer extends JFrame {
public NewPlayer() {
JLabel backGround = new JLabel(new ImageIcon("images\\fiha2taban2.png"));
setTitle("FiHa");
setSize(750, 550);
getContentPane().add(backGround);
setLocationRelativeTo(null); // Center the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage("images\\iconfh.png"));
Color renk=new Color(255,250,155);
// Create two labels and set their components
JLabel label1= new JLabel("NEW PLAYER !");
label1.setBounds(220,200,300,40);
this.add(label1);
label1.setFont(new Font("Times New Roman", Font.BOLD , 40));
JLabel label2= new JLabel("Please enter your name: ");
label2.setBounds(270,290,249,40);
this.add(label2);
label2.setFont(new Font("Times New Roman", Font.BOLD , 20));
// Create a text field and set its components
JTextField txtField = new JTextField("", 20);
txtField.setBounds(220,335,300,40);
this.add(txtField);
txtField.setFont(new Font("Times New Roman", Font.BOLD , 16));
// Create a button and set its components
JButton button1 = new JButton("OKAY");
button1.setBounds(315,396,110,40);
this.add(button1);
button1.setFont(new Font("Times New Roman", Font.BOLD , 16));
button1.setBackground(renk);
}
}
}
I am currently doing a diploma course in Java and for our project we have to create a Restaurant Calculator(See screenshot), I have the majority of the GUI done but I am having great difficulty with the SQL. I have never used SQL before so I'm not 100% certain what to do. I understand how it works but I can't even run the SQL file that the college supplied us in MySQL. Any help with this would be greatly appreciated as I am really struggling with this. I have had this GUI done the past 2 weeks and have been searching online for help but cannot get my head around it
Code is below...
import java.awt.Container;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RestaurantCalculator {
private static JTextField TotalText;
private static JTextField TaxText;
private static JTextField Table;
private static JTextField Sub;
private static JTextField WName;
private static JComboBox BevMenu;
private static JComboBox AppMenu;
private static JComboBox MainMenu;
private static JComboBox DesMenu;
public static void testFun(String myName){
TotalText.setText(myName);
}
public static void addComponentsToPane(Container pane){
pane.setLayout(null);
Border border = BorderFactory.createLoweredBevelBorder();
Border button = BorderFactory.createRaisedBevelBorder();
JLabel Welcome = new JLabel("Restaurant");
Welcome.setBounds(250, 5, 150, 100);
Welcome.setFont(new Font("Arial Black", Font.BOLD, 20));
JLabel Waiter = new JLabel("Waiter Information");
Waiter.setBounds(100, 100, 200, 100);
Waiter.setFont(new Font("Arial Black", Font.BOLD, 14));
JLabel Tableno = new JLabel("Table number:");
Tableno.setBounds(120, 130, 200, 100);
Tableno.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel WaiterName = new JLabel("Waiter Name:");
WaiterName.setBounds(120, 160, 200, 100);
WaiterName.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel MenuItems = new JLabel("Menu Items");
MenuItems.setBounds(100, 220, 150, 100);
MenuItems.setFont(new Font("Arial Black", Font.BOLD, 14));
JLabel Beverage = new JLabel("Beverage:");
Beverage.setBounds(120, 250, 200, 100);
Beverage.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Appetizer = new JLabel("Appetizer:");
Appetizer.setBounds(120, 280, 200, 100);
Appetizer.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel MainCourse = new JLabel("Main Course:");
MainCourse.setBounds(120, 310, 200, 100);
MainCourse.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Dessert = new JLabel("Dessert:");
Dessert.setBounds(120, 340, 200, 100);
Dessert.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel SubTotal = new JLabel("Sub Total:");
SubTotal.setBounds(120, 440, 200, 100);
SubTotal.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Tax = new JLabel("Tax:");
Tax.setBounds(120, 470, 200, 100);
Tax.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Total = new JLabel("Total:");
Total.setBounds(120, 500, 200, 100);
Total.setFont(new Font("Arial Black", Font.BOLD, 12));
Table = new JTextField(10);
Table.setBounds(290, 171, 150, 20);
Table.setBorder(border);
WName = new JTextField(11);
WName.setBounds(290, 201, 150, 20);
WName.setBorder(border);
Sub = new JTextField(10);
Sub.setBounds(260,482,150, 20);
Sub.setBorder(border);
Sub.setEditable(false);
TotalText = new JTextField(10);
TotalText.setBounds(260,545,150, 20);
TotalText.setBorder(border);
TotalText.setEditable(false);
TaxText = new JTextField(10);
TaxText.setBounds(260,515,150, 20);
TaxText.setBorder(border);
TaxText.setEditable(false);
BevMenu = new JComboBox();
BevMenu.setBounds(290, 295, 180, 20);
AppMenu = new JComboBox();
AppMenu.setBounds(290, 325, 180, 20);
MainMenu = new JComboBox();
MainMenu.setBounds(290, 355, 180, 20);
DesMenu = new JComboBox();
DesMenu.setBounds(290, 385, 180, 20);
JButton calculateJButton;
calculateJButton = new JButton( "Calculate Bill" );
calculateJButton.setBounds(250,425, 170, 30);
calculateJButton.setBorder(button);
pane.add(Welcome);
pane.add(Waiter);
pane.add(Tableno);
pane.add(WaiterName);
pane.add(MenuItems);
pane.add(Beverage);
pane.add(Appetizer);
pane.add(MainCourse);
pane.add(Dessert);
pane.add(SubTotal);
pane.add(Tax);
pane.add(Total);
pane.add(Table);
pane.add(WName);
pane.add(Sub);
pane.add(TaxText);
pane.add(TotalText);
pane.add(calculateJButton);
pane.add(BevMenu);
pane.add(AppMenu);
pane.add(MainMenu);
pane.add(DesMenu);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Restaurant Bill Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Size and display the window.
Insets insets = frame.getInsets();
frame.setSize(600 + insets.left + insets.right,
700 + insets.top + insets.bottom);
frame.setVisible(true);
}
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();
String theBest = "test";
testFun(theBest);
}
});
}
So I am creating a calculator app. When one of the number buttons is clicked, it is supposed to appear on the JLabel. I have added actionListeners to all of my buttons, and when a number button is clicked, the JLabel's text is changed, via the method .setText(). For some reason when i run the program, the JLabel's text doesn't update. So i thought that the text value of the JLbale wasn't being changed, yet when I print the text value of the JLabel on the console, its shows that is has changed. I'm an now stuck. Help would be appreciated
package com.Patel.APSC;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.UIManager;
public class Calculator extends JFrame {
double num1 = 0;
double num2 = 0;
String strNum1;
String strNum2;
String op;
Double answer;
String label = "";
private JButton btnClear;
JLabel lblLabel = new JLabel("");
// constructors
public Calculator() {
// sets up the JFrame
this.setVisible(true);
this.setTitle("Calculator");
this.setResizable(false);
this.setSize(356, 512);
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setBackground(new Color(212, 229, 238));
// sets up all the buttons
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn0 = new JButton("0");
JButton btnAdd = new JButton("+");
JButton btnSubtract = new JButton("-");
JButton btnMultiply = new JButton("*");
JButton btnDivide = new JButton("/");
JButton btnEqual = new JButton("=");
JButton btnClear = new JButton("AC");
btnClear.setBorder(null);
btn1.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn2.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn3.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn4.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn5.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn6.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn7.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn8.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn9.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn0.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btnAdd.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
btnSubtract.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
btnMultiply.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
btnDivide.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
btnEqual.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
btnClear.setFont(new Font("Times New Roman", Font.PLAIN, 32));
btn1.setLocation(28, 159);
btn2.setLocation(98, 159);
btn3.setLocation(168, 159);
btn4.setLocation(28, 229);
btn5.setLocation(98, 229);
btn6.setLocation(168, 229);
btn7.setLocation(28, 299);
btn8.setLocation(98, 299);
btn9.setLocation(168, 299);
btn0.setLocation(98, 369);
btnAdd.setLocation(265, 369);
btnSubtract.setLocation(265, 299);
btnMultiply.setLocation(265, 229);
btnDivide.setLocation(265, 159);
btnEqual.setLocation(265, 93);
btnClear.setBounds(163, 369, 55, 55);
btn1.setSize(55, 55);
btn2.setSize(55, 55);
btn3.setSize(55, 55);
btn4.setSize(55, 55);
btn5.setSize(55, 55);
btn6.setSize(55, 55);
btn7.setSize(55, 55);
btn8.setSize(55, 55);
btn9.setSize(55, 55);
btn0.setSize(55, 55);
btnAdd.setSize(55, 55);
btnSubtract.setSize(55, 55);
btnMultiply.setSize(55, 55);
btnDivide.setSize(55, 55);
btnEqual.setSize(55, 55);
btn0.setActionCommand("0");
btn1.setActionCommand("1");
btn2.setActionCommand("2");
btn3.setActionCommand("3");
btn4.setActionCommand("4");
btn5.setActionCommand("5");
btn6.setActionCommand("6");
btn7.setActionCommand("7");
btn8.setActionCommand("8");
btn9.setActionCommand("9");
btnAdd.setActionCommand("+");
btnSubtract.setActionCommand("-");
btnMultiply.setActionCommand("*");
ButtonListener listener = new ButtonListener();
btn1.addActionListener(listener);
btn2.addActionListener(listener);
btn3.addActionListener(listener);
btn4.addActionListener(listener);
btn5.addActionListener(listener);
btn6.addActionListener(listener);
btn7.addActionListener(listener);
btn8.addActionListener(listener);
btn9.addActionListener(listener);
btn0.addActionListener(listener);
btnAdd.addActionListener(listener);
btnSubtract.addActionListener(listener);
btnMultiply.addActionListener(listener);
btnDivide.addActionListener(listener);
btnClear.addActionListener(listener);
btnEqual.addActionListener(listener);
this.getContentPane().add(btn0);
this.getContentPane().add(btn1);
this.getContentPane().add(btn2);
this.getContentPane().add(btn3);
this.getContentPane().add(btn4);
this.getContentPane().add(btn5);
this.getContentPane().add(btn6);
this.getContentPane().add(btn7);
this.getContentPane().add(btn8);
this.getContentPane().add(btn9);
this.getContentPane().add(btnAdd);
this.getContentPane().add(btnSubtract);
this.getContentPane().add(btnMultiply);
this.getContentPane().add(btnDivide);
this.getContentPane().add(btnEqual);
this.getContentPane().add(btnClear);
JLabel lblLabel = new JLabel("");
lblLabel.setFont(new Font("Times New Roman", Font.PLAIN, 27));
this.getContentPane().add(lblLabel);
lblLabel.setLocation(43, 31);
lblLabel.setSize(277, 51);
lblLabel.setOpaque(true);
}
public static void main(String[] args) {
Calculator gui = new Calculator();
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("1")
|| e.getActionCommand().equals("2")
|| e.getActionCommand().equals("3")
|| e.getActionCommand().equals("4")
|| e.getActionCommand().equals("5")
|| e.getActionCommand().equals("6")
|| e.getActionCommand().equals("7")
|| e.getActionCommand().equals("8")
|| e.getActionCommand().equals("9")
|| e.getActionCommand().equals("0")) {
// if a number is typed
lblLabel.setText(e.getActionCommand());
System.out.println(lblLabel.getText());
}
}
}
}
You're calling text.setText(e.getActionCommand()) -- but what is this textfield? It's not your JLabel which is called, lblLabel, and in fact, I can't find a text field within your program, and so I'm surprised that the class even compiles.
I believe that you'll be much better off calling setText on the correct JLabel field: lblLabel.setText(e.getActionCommand())
Side recommendations:
Needless repetition often leads to hard to find bugs, and so you'll want to use arrays or collections and for loops to consolidate your code.
Your gut instinct may be to use a null layout and calling setBounds(...) on your components to place them with absolute positioning, but I'm going suggest that you not do this as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
Your last large if block can be compressed to if ("1234567890".contains(e.getActionCommand()) {
Edit
Your question's edit changes everything. Please avoid these types of changes as they can be very frustrating to us volunteers.
Now I see that you're shadowing the lblLbl variable by declaring it twice, once in the class and once in the constructor, meaning that the class's field is not the JLabel that is being displayed. Solution: declare the variable only once.
so change
public class Calculator {
JLabel lblLabel = new JLabel("");
public Calculator() {
JLabel lblLabel = new JLabel("");
// ...
}
}
to:
public class Calculator {
JLabel lblLabel = new JLabel("");
public Calculator() {
// JLabel lblLabel = new JLabel(""); // don't re-declare
// ...
}
}
I want my JTabbedpane on top of panel3 and panel4. Rightnow, JTabbedpane is on top of panel1 and panel2 but I want it on top of panel3 and panel4. I try setBounds to bring JTabbedpane down but it doesn't work. Thanks for everyone help.
import java.awt.*;
import static java.awt.Font.BOLD;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import static javax.swing.SwingConstants.CENTER;
import javax.swing.event.*;
public class hotels extends JFrame{
JButton hotel;
JLabel image;
JTabbedPane tabbed,tabbed1;
JPanel panel;
JPanel panel1;
Container pane;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JLabel departure;
JLabel from;
JLabel to;
JLabel searchflight,searchflight1;
JButton back, back1;
public hotels(){
panel=new JPanel();
panel.setBackground(Color.cyan);
hotel=new JButton();
hotel.setText("Hotels");
Font myFont = new Font("Serif", Font.BOLD, 18);
hotel.setFont(myFont);
panel.setLayout(null);
panel.add(hotel);
hotel.setBounds(50, 80, 100, 40);
image=new JLabel();
image.setBounds(50,1,80,80);
image.setBorder(BorderFactory.createLineBorder(Color.yellow));
image.setBackground(Color.white);
image.setIcon(new ImageIcon("2.gif"));
panel.add(image);
panel1=new JPanel();
panel1.setLayout(null);
panel1.setBounds(0, 30, 300, 400);
back=new JButton();
back.setText("<");
back.setBounds(0, 5, 60, 30);
panel1.add(back);
searchflight=new JLabel();
searchflight.setText("Search Flights");
searchflight.setBounds(130,5,100,30);
panel1.add(searchflight);
tabbed=new JTabbedPane();
tabbed.add( "Round Trip",panel1);
panel2=new JPanel();
panel2.setLayout(null);
panel2.setBounds(0, 30, 300, 400);
panel2.setBackground(Color.blue);
searchflight1=new JLabel();
searchflight1.setText("Search Flights");
searchflight1.setBounds(130,5,100,30);
panel2.add(searchflight1);
back1=new JButton();
back1.setText("<");
back1.setBounds(0, 5, 60, 30);
panel2.add(back1);
panel4=new JPanel();
panel4.setLayout(null);
panel4.setBackground(Color.white);
panel4.setBounds(0, 40, 290, 300);
panel4.setBorder(BorderFactory.createMatteBorder(0, 10, 10, 10, Color.blue));
Font f=new Font("Serif", Font.PLAIN,12);
tabbed.add("One Way",panel2);
panel3=new JPanel();
panel3.setBounds(0, 40, 290, 300);
panel3.setLayout(null);
panel3.setBorder(BorderFactory.createMatteBorder(0, 10, 10, 10, Color.blue));
panel3.setBackground(Color.white);
panel1.add(panel3);
panel2.add(panel4);
from=new JLabel();
from.setBackground(Color.blue);
panel1.setBackground(Color.blue);
pane=getContentPane();
pane.add(panel);
image.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e){
if (e.getSource()==image){
pane.removeAll();
pane.add(tabbed);
pane.revalidate();
pane.repaint();
}
}
}
);
}
public static void main(String[] args) {
hotels mw=new hotels();
mw.setVisible(true);
mw.setSize(300, 400);
mw.setResizable(false);
}
}