Can't seem to be able to move my objects using java - java

I seem to be unable to move any objects I bring into my class, my other classes seem to be functional when I set bounds, but not this one.
I'm wondering why it isn't working and functionally like my other classes.
Some of the bounds are extreme as that's me making sure I can notice if any of the objects are moving; which they are not.
package student.information.in.java;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
public class CreatedBy extends JFrame {
JButton btnJoelHogg, btnAndrewCameron, btnConorElliman, btnTiarnanByrne, btnproject;
JLabel lblJoel, lblAndrew, lblConor, lblTiarnan, lbl_title, lbl_Info;
public CreatedBy() {
super("Project team and information");
setLayout(null);
lbl_title = new JLabel("Information about the system & its creators", JLabel.CENTER);
add(lbl_title);
lbl_title.setBounds(200, 200, 200, 200);
lbl_title.setFont(new Font("Calibri", Font.PLAIN, 30));
lbl_title.setForeground(Color.BLACK);
lblJoel = new JLabel("Joel Hogg");
add(lblJoel);
lblJoel.setBounds(30, 140, 200, 30);
lblJoel.setFont(new Font("Calibri", Font.PLAIN, 14));
lblJoel.setForeground(Color.BLACK);
btnJoelHogg = new JButton("About Joel");
btnJoelHogg.setFont(new Font("Calibri", Font.PLAIN, 14));
btnJoelHogg.setBackground(Color.LIGHT_GRAY);
btnJoelHogg.setForeground(Color.BLACK);
lblAndrew = new JLabel("Andrew Cameron");
add(lblAndrew);
lblAndrew.setBounds(80, 50, 400, 30);
lblAndrew.setFont(new Font("Calibri", Font.PLAIN, 14));
lblAndrew.setForeground(Color.BLACK);
btnAndrewCameron = new JButton("About Andrew");
btnAndrewCameron.setFont(new Font("Calibri", Font.PLAIN, 14));
btnAndrewCameron.setBackground(Color.LIGHT_GRAY);
btnAndrewCameron.setForeground(Color.BLACK);
lblJoel = new JLabel("Conor Elliman");
add(lblJoel);
lblJoel.setBounds(20, 540, 170, 380);
lblJoel.setFont(new Font("Calibri", Font.PLAIN, 14));
lblJoel.setForeground(Color.BLACK);
btnConorElliman = new JButton("About Conor");
btnConorElliman.setFont(new Font("Calibri", Font.PLAIN, 14));
btnConorElliman.setBackground(Color.LIGHT_GRAY);
btnConorElliman.setForeground(Color.BLACK);
lblTiarnan = new JLabel("Tiarnán");
add(lblTiarnan);
lblTiarnan.setBounds(202, 506, 170, 30);
lblTiarnan.setFont(new Font("Calibri", Font.PLAIN, 14));
lblTiarnan.setForeground(Color.BLACK);
btnTiarnanByrne = new JButton("About Tiarnán");
btnTiarnanByrne.setFont(new Font("Calibri", Font.PLAIN, 14));
btnTiarnanByrne.setBackground(Color.LIGHT_GRAY);
btnTiarnanByrne.setForeground(Color.BLACK);
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add(btnJoelHogg);
c.add(btnAndrewCameron);
c.add(btnConorElliman);
c.add(btnTiarnanByrne);
ButtonHandler handler = new ButtonHandler();
btnJoelHogg.addActionListener(handler);
btnAndrewCameron.addActionListener(handler);
btnConorElliman.addActionListener(handler);
btnTiarnanByrne.addActionListener(handler);
setSize(600, 400);
show();
}
public static void main(String[] args) {
// Make frame
CreatedBy f = new CreatedBy();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Exit via windowClosing.");
System.exit(0);
}
}
);
} // end of main
// inner class for button event handling
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnJoelHogg) {
System.out.println(
"Joel Hogg button pressed");
}
if (e.getSource() == btnAndrewCameron) {
System.out.println(
"Andrew Cameron button pressed");
}
if (e.getSource() == btnTiarnanByrne) {
System.out.println("Tiarnan Byrne button pressed");
}
if (e.getSource() == btnConorElliman) {
System.out.println("Conor Elliman button pressed");
}
}
} // end of inner class
} // end of outer class

Related

Java Swing Application Window - Second Form showing empty

The first class or JFrame that displays just fine
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Window;
import java.awt.Color;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
public class ATM_Display extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frame;
private JPasswordField jpass_passfield;
char[] password = new char[5]; // field for our password
private StringBuilder tempPin = new StringBuilder(); // temporary pin placeholder
private static List<Account> listOfAccounts = new ArrayList<Account>(); // list of the banks accounts
private static List<JButton> btnList = new ArrayList<JButton>();
private static OptionsMenu frame2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ATM_Display window = new ATM_Display();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ATM_Display() {
initialize();
frame2 = new OptionsMenu();
InstantiateAccList(listOfAccounts); // instantiate our list of accounts including clients names/pin/balance
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 508, 481);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
jpass_passfield = new JPasswordField();
jpass_passfield.setHorizontalAlignment(SwingConstants.CENTER);
jpass_passfield.setFont(new Font("Tahoma", Font.PLAIN, 30));
jpass_passfield.setToolTipText("please enter a valid 4 digit pin number using the numeric buttons below");
jpass_passfield.setEditable(false);
jpass_passfield.setBounds(114, 113, 233, 41);
frame.getContentPane().add(jpass_passfield);
//button 1
JButton btn_1 = new JButton("1");
btn_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("1"); // add to temporary placeholder
jpass_passfield.setText(GetPin()); // set the actual text of the password field
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_1.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_1.setBounds(22, 256, 89, 23);
frame.getContentPane().add(btn_1);
//button 2
JButton btn_2 = new JButton("2");
btn_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("2");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_2.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_2.setBounds(128, 256, 89, 23);
frame.getContentPane().add(btn_2);
//button 3
JButton btn_3 = new JButton("3");
btn_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("3");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_3.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_3.setBounds(229, 256, 89, 23);
frame.getContentPane().add(btn_3);
//button 4
JButton btn_4 = new JButton("4");
btn_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("4");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_4.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_4.setBounds(22, 301, 89, 23);
frame.getContentPane().add(btn_4);
//button 5
JButton btn_5 = new JButton("5");
btn_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("5");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_5.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_5.setBounds(128, 301, 89, 23);
frame.getContentPane().add(btn_5);
//button 6
JButton btn_6 = new JButton("6");
btn_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("6");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_6.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_6.setBounds(229, 301, 89, 23);
frame.getContentPane().add(btn_6);
//button 7
JButton btn_7 = new JButton("7");
btn_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("7");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_7.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_7.setBounds(22, 347, 89, 23);
frame.getContentPane().add(btn_7);
//button 8
JButton btn_8 = new JButton("8");
btn_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("8");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_8.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_8.setBounds(128, 347, 89, 23);
frame.getContentPane().add(btn_8);
//button 9
JButton btn_9 = new JButton("9");
btn_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("9");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_9.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_9.setBounds(229, 347, 89, 23);
frame.getContentPane().add(btn_9);
//button 0
JButton btn_0 = new JButton("0");
btn_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("1");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_0.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_0.setBounds(128, 389, 89, 23);
frame.getContentPane().add(btn_0);
JButton btn_cancel = new JButton("CANCEL");
btn_cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
btn_cancel.setBackground(new Color(255, 0, 0));
btn_cancel.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_cancel.setBounds(328, 256, 127, 23);
frame.getContentPane().add(btn_cancel);
JButton btn_clear = new JButton("CLEAR");
btn_clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClearTempPin(); // clear our temporary pin
jpass_passfield.setText(""); // clear the password field
EnableKeypad(btnList);
}
});
btn_clear.setBackground(Color.YELLOW);
btn_clear.setForeground(Color.BLACK);
btn_clear.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_clear.setBounds(328, 301, 127, 23);
frame.getContentPane().add(btn_clear);
JButton btn_enter = new JButton("ENTER");
btn_enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(isPinValid()) {
frame.setVisible(false);
frame2.setVisible(true);
}
}
});
btn_enter.setBackground(Color.GREEN);
btn_enter.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_enter.setBounds(328, 347, 127, 23);
frame.getContentPane().add(btn_enter);
JLabel lblNewLabel = new JLabel("Enter Pin");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 21));
lblNewLabel.setBounds(179, 79, 114, 23);
frame.getContentPane().add(lblNewLabel);
JLabel lblWelcomePlease = new JLabel("INVESCO ATM");
lblWelcomePlease.setForeground(new Color(0, 128, 0));
lblWelcomePlease.setFont(new Font("Tahoma", Font.BOLD, 25));
lblWelcomePlease.setBounds(144, 11, 189, 23);
frame.getContentPane().add(lblWelcomePlease);
//add buttons to our list
AddBtnsToList(btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_0);
}
// method for accessing private jpass_passfield
/*
* public void AddValToPassField(int i) { this.jpass_passfield. }
*/
// method to instantiate list of accounts
public static void InstantiateAccList(List<Account> x) {
final Account a = new Account("Henry", "Ford", 10000, 12345);
final Account b = new Account("Martha", "Stewart", 3454453, 22345);
final Account c = new Account("Cletus", "Shines", 199, 32345);
final Account d = new Account("Warren", "Scruffet", 28488, 42345);
x.add(a);
x.add(b);
x.add(c);
x.add(d);
}
//method to validate client PIN
// if pin is correct return true
// otherwise return false
public boolean isPinValid() {
for(Account y : listOfAccounts) {
if(y.getPin() == Integer.parseInt(GetPin())) {
return true;
}
}
return false;
}
// Method for adding buttons to the list of buttons
public static void AddBtnsToList(JButton a, JButton b, JButton c, JButton d, JButton e,
JButton f, JButton g, JButton h, JButton i, JButton j) {
btnList.add(a);
btnList.add(b);
btnList.add(c);
btnList.add(d);
btnList.add(e);
btnList.add(f);
btnList.add(g);
btnList.add(h);
btnList.add(i);
btnList.add(j);
}
// method for adding string to the temporary pin string we will be comparing to
public void AddToPassword(String x) {
StringBuilder s = new StringBuilder(x);
this.tempPin.append(s);
}
// gets the current pin string
public String GetPin() {
return this.tempPin.toString();
}
// clears the temporary pin placeholder
public void ClearTempPin() {
this.tempPin.setLength(0);
}
// checks to see if pin is at 5 characters
public boolean CheckPinLength() {
String temp = this.tempPin.toString();
if (temp.length() == 5) {
return true;
}
else { return false;}
}
//method to disable keypad if length is at 5
public void DisableKeypad(List<JButton> x) {
for(JButton y : x) {
y.setEnabled(false);
}
}
//method to Enable under a particular condition
public void EnableKeypad(List<JButton> x) {
for(JButton y : x) {
y.setEnabled(true);
}
}
}
And now my second class.. I'm sorry I'm a complete noob, when I run the debugger it looks like Options Menu is constructing correctly, but then it just displays as the minimize, full screen and exit button and nothing else. It will also run perfectly fine independently if I run it on it's own page. I'm really confused at this point and would appreciate any help. I have seen a lot of examples on here where the JFrame isn't set to isVisible or instantiated, but that isn't the case here. Thank you so much for any help!
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class OptionsMenu extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frame;
private JTextField tb_deposit;
/**
* Launch the application.
*/
/*
* public static void main(String[] args) { EventQueue.invokeLater(new
* Runnable() { public void run() { try { OptionsMenu window = new
* OptionsMenu(); window.frame.setVisible(true); } catch (Exception e) {
* e.printStackTrace(); } } }); }
*/
/**
* Create the application.
*/
public OptionsMenu() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 490, 461);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btn_balance = new JButton("1");
btn_balance.setFont(new Font("Tahoma", Font.BOLD, 25));
btn_balance.setBounds(207, 267, 64, 31);
frame.getContentPane().add(btn_balance);
JButton btn_Withdrawal = new JButton("2");
btn_Withdrawal.setFont(new Font("Tahoma", Font.BOLD, 25));
btn_Withdrawal.setBounds(207, 301, 64, 31);
frame.getContentPane().add(btn_Withdrawal);
JButton btn_deposit = new JButton("3");
btn_deposit.setFont(new Font("Tahoma", Font.BOLD, 25));
btn_deposit.setBounds(207, 335, 64, 31);
frame.getContentPane().add(btn_deposit);
/// exit button
JButton btn_exit = new JButton("4");
btn_exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(frame,
"Do you want to Exit ?", "Exit Confirmation : ",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
frame.dispose();
else if (result == JOptionPane.NO_OPTION)
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
});
btn_exit.setFont(new Font("Tahoma", Font.BOLD, 25));
btn_exit.setBounds(207, 369, 64, 31);
frame.getContentPane().add(btn_exit);
JLabel lblNewLabel = new JLabel("BALANCE INQUIRY:");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel.setBounds(37, 267, 160, 31);
frame.getContentPane().add(lblNewLabel);
JLabel lblWithdrawal = new JLabel("WITHDRAWAL:");
lblWithdrawal.setFont(new Font("Tahoma", Font.BOLD, 15));
lblWithdrawal.setBounds(72, 309, 116, 19);
frame.getContentPane().add(lblWithdrawal);
JLabel lblDeposit = new JLabel("DEPOSIT:");
lblDeposit.setFont(new Font("Tahoma", Font.BOLD, 15));
lblDeposit.setBounds(114, 339, 74, 19);
frame.getContentPane().add(lblDeposit);
JLabel lblExit = new JLabel("EXIT:");
lblExit.setFont(new Font("Tahoma", Font.BOLD, 15));
lblExit.setBounds(140, 369, 48, 14);
frame.getContentPane().add(lblExit);
JButton btn_get20 = new JButton("$20");
btn_get20.setBounds(10, 34, 89, 23);
frame.getContentPane().add(btn_get20);
JButton btn_get40 = new JButton("$40");
btn_get40.setBounds(10, 80, 89, 23);
frame.getContentPane().add(btn_get40);
JButton btn_get60 = new JButton("$60");
btn_get60.setBounds(10, 127, 89, 23);
frame.getContentPane().add(btn_get60);
JButton btn_get100 = new JButton("$100");
btn_get100.setBounds(338, 34, 89, 23);
frame.getContentPane().add(btn_get100);
JButton btn_get200 = new JButton("$200");
btn_get200.setBounds(338, 80, 89, 23);
frame.getContentPane().add(btn_get200);
JButton btn_cancel = new JButton("CANCEL");
btn_cancel.setBounds(338, 127, 89, 23);
frame.getContentPane().add(btn_cancel);
tb_deposit = new JTextField();
tb_deposit.setBounds(157, 128, 96, 20);
frame.getContentPane().add(tb_deposit);
tb_deposit.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Enter Deposit Amount");
lblNewLabel_1.setBounds(157, 103, 114, 14);
frame.getContentPane().add(lblNewLabel_1);
}
public JFrame GetFrame2() {
return this.frame;
}
}
The problem is that your classes (ATM_Display and OptionsMenu) extend JFrame but are never really used as JFrames (you create a separate JFrame instance in each).
That means that when you make the OptionsMenu visible there is nothing to show (because you add all elements to the OptionsMenu.frame).
You should drop the extends clause:
public class ATM_Display {
// ...
}
and
public class OptionsMenu {
// ...
}
Now you will have a compile error in ATM_Display on the following line because the OptionsMenu doesn't have a setVisible() method:
frame2.setVisible(true);
To make the code work again the OptionsMenu needs this setVisible() method to make its frame visible:
public class OptionsMenu {
// ...
public void setVisible(boolean b) {
frame.setVisible(b);
}
}

Making calculator using Swing

I have developed the calc using swing but i have a problem in equal to button.
I have written the code for addition,subtraction,multiplication and division in the equal to button but the problem is that when i subtract two numbers I get the o/p but when I add or multiply or divide I don't get the o/p. So help me in
the code of equal to button:
package com.dss;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;
import javax.swing.SwingConstants;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
public class JCalcEx extends JFrame {
private JPanel contentPane;
private JTextField value1;
private JTextField value2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JCalcEx frame = new JCalcEx();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JCalcEx() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 395, 680);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
value1 = new JTextField();
value1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value1.setHorizontalAlignment(SwingConstants.RIGHT);
value1.setFont(new Font("Tahoma", Font.PLAIN, 20));
value1.setBounds(12, 69, 350, 41);
contentPane.add(value1);
value1.setColumns(10);
JTextField value3 = new JTextField();
value3.setHorizontalAlignment(SwingConstants.RIGHT);
value3.setFont(new Font("Tahoma", Font.PLAIN, 20));
value3.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value3.setBounds(12, 111, 353, 41);
contentPane.add(value3);
value3.setColumns(10);
JButton button9 = new JButton("9");
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+9);
}
});
button9.setFont(new Font("Tahoma", Font.PLAIN, 20));
button9.setBounds(12, 192, 69, 70);
contentPane.add(button9);
JButton button8 = new JButton("8");
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0){
value1.setText(value1.getText()+8);
}
});
button8.setFont(new Font("Tahoma", Font.PLAIN, 20));
button8.setBounds(105, 192, 69, 70);
contentPane.add(button8);
JButton button7 = new JButton("7");
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+7);
}
});
button7.setFont(new Font("Tahoma", Font.PLAIN, 20));
button7.setBounds(199, 192, 69, 70);
contentPane.add(button7);
JButton button6 = new JButton("6");
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+6);
}
});
button6.setFont(new Font("Tahoma", Font.PLAIN, 20));
button6.setBounds(293, 192, 69, 70);
contentPane.add(button6);
JButton button5 = new JButton("5");
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+5);
}
});
button5.setFont(new Font("Tahoma", Font.PLAIN, 20));
button5.setBounds(12, 293, 69, 70);
contentPane.add(button5);
JButton button4 = new JButton("4");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+4);
}
});
button4.setFont(new Font("Tahoma", Font.PLAIN, 20));
button4.setBounds(105, 293, 69, 70);
contentPane.add(button4);
JButton button3 = new JButton("3");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+3);
}
});
button3.setFont(new Font("Tahoma", Font.PLAIN, 20));
button3.setBounds(199, 293, 69, 70);
contentPane.add(button3);
JButton button2 = new JButton("2");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+2);
}
});
button2.setFont(new Font("Tahoma", Font.PLAIN, 20));
button2.setBounds(293, 293, 69, 70);
contentPane.add(button2);
JButton button1 = new JButton("1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+1);
}
});
button1.setFont(new Font("Tahoma", Font.PLAIN, 20));
button1.setBounds(12, 392, 69, 70);
contentPane.add(button1);
JButton buttonc = new JButton("C");
buttonc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText("");
value1.setText("");
actionRecieved.setText("");
value3.setText("");
}
});
buttonc.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonc.setBounds(293, 392, 69, 70);
contentPane.add(buttonc);
JButton button0 = new JButton("0");
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+0);
}
});
button0.setFont(new Font("Tahoma", Font.PLAIN, 20));
button0.setBounds(105, 392, 69, 70);
contentPane.add(button0);
JButton buttoneql = new JButton("=");
buttoneql.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int one = Integer.parseInt(value2.getText());
int two = Integer.parseInt(value1.getText());
if(actionRecieved.getText().equals("sub"))
{
int sub = one - two;
value3.setText(String.valueOf(sub));
}
if(actionRecieved.getText().equals("add"))
{
int add = one + two;
value3.setText(String.valueOf(add));
}
if(actionRecieved.getText().equals("mul"))
{
int mul = one * two;
value1.setText(String.valueOf(mul));
}
if(actionRecieved.getText().equals("div"))
{
int div = one / two;
value1.setText(String.valueOf(div));
}
}
});
buttoneql.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttoneql.setBounds(199, 392, 69, 70);
contentPane.add(buttoneql);
JButton buttonplus = new JButton("+");
buttonplus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("plus");
value1.setText(null);
}
});
buttonplus.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonplus.setBounds(12, 489, 69, 70);
contentPane.add(buttonplus);
JButton buttonminus = new JButton("-");
buttonminus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("sub");
value1.setText(null);
}
});
buttonminus.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonminus.setBounds(105, 489, 69, 70);
contentPane.add(buttonminus);
JButton buttonmultiply = new JButton("x");
buttonmultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("mul");
value1.setText(null);
}
});
buttonmultiply.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonmultiply.setBounds(199, 489, 69, 70);
contentPane.add(buttonmultiply);
JButton buttondivide = new JButton("/");
buttondivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("div");
value1.setText(null);
}
});
buttondivide.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttondivide.setBounds(293, 489, 69, 70);
contentPane.add(buttondivide);
value2 = new JTextField();
value2.setFont(new Font("Tahoma", Font.PLAIN, 20));
value2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value2.setHorizontalAlignment(SwingConstants.RIGHT);
value2.setBounds(229, 29, 133, 41);
contentPane.add(value2);
value2.setColumns(10);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setBounds(12, 29, 350, 81);
contentPane.add(editorPane);
actionRecieved = new JLabel("");
actionRecieved.setBounds(12, 572, 118, 48);
contentPane.add(actionRecieved);
}
private JLabel actionRecieved;
}
You have the following problems
For div int div = one - two; - should be /
For 1. you will have problem with Integer division
Add will never be entered into because you do actionRecieved.setText("plus"); but expect add
Not sure why in some cases you are setting value1 and in others value3
???

The method setVisible(boolean) is undefined for the type orbital_app

This is the code. My main frame is orbital_app and I would like it to when I click on JButton (button), data is being saved, the current window closes and another window orbital_app opens.
public class signup_try {
private JFrame frame;
private JTextField txtname;
private JTextField textusername;
private JTextField txtpass;
private JTextField textmail;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
signup_try window = new signup_try();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public signup_try() {
initialize();
}
Connection connection=null;
/**
* Initialize the contents of the frame.
*/
private void initialize() {
connection=dbase.dBase();
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("Orbital");
label.setForeground(SystemColor.activeCaption);
label.setFont(new Font("Arial Black", Font.BOLD, 17));
label.setBounds(170, 11, 71, 25);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("Name:");
label_1.setForeground(SystemColor.activeCaption);
label_1.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_1.setBounds(21, 66, 46, 14);
frame.getContentPane().add(label_1);
txtname = new JTextField();
txtname.setColumns(10);
txtname.setBounds(102, 63, 200, 22);
frame.getContentPane().add(txtname);
textusername = new JTextField();
textusername.setColumns(10);
textusername.setBounds(102, 108, 200, 22);
frame.getContentPane().add(textusername);
txtpass = new JTextField();
txtpass.setColumns(10);
txtpass.setBounds(102, 150, 200, 22);
frame.getContentPane().add(txtpass);
textmail = new JTextField();
textmail.setColumns(10);
textmail.setBounds(102, 192, 200, 22);
frame.getContentPane().add(textmail);
JLabel label_2 = new JLabel("Username:");
label_2.setForeground(SystemColor.activeCaption);
label_2.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_2.setBounds(21, 111, 71, 14);
frame.getContentPane().add(label_2);
JLabel label_3 = new JLabel("Password:");
label_3.setForeground(SystemColor.activeCaption);
label_3.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_3.setBounds(21, 154, 71, 14);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("Email:");
label_4.setForeground(SystemColor.activeCaption);
label_4.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_4.setBounds(21, 196, 46, 14);
frame.getContentPane().add(label_4);
JButton button = new JButton("Sign Up");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query="insert into Users(Name, Username, Password, Email) values(?,?,?,?)";
PreparedStatement prepstat=connection.prepareStatement(query);
prepstat.setString(1, txtname.getText());
prepstat.setString(2, textusername.getText());
prepstat.setString(3, txtpass.getText());
prepstat.setString(4, textmail.getText());
prepstat.execute();
JOptionPane.showMessageDialog(null, "Data saved");
prepstat.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
I get the error saying that The method setVisible(boolean) is undefined for the type orbital_app. what should I do to fix this? Here I want to close this present frame(signup_try) and go to other frame (orbital_app).. setVisible is underlined red and says "The method setVisible(boolean) is undefined for the type orbital_app".
frame.dispose();
orbital_app orb=new orbital_app();
orb.setVisible(true);
}
});
button.setBounds(170, 239, 91, 23);
frame.getContentPane().add(button);
}
}
Orbital_app code(in this code setVisible works properly without any error):
package project;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.UIManager;
public class orbital_app{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
orbital_app window = new orbital_app();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public orbital_app() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(UIManager.getColor("Button.background"));
frame.setResizable(false);
frame.setBounds(100, 100, 450, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel toplbl = new JLabel("Orbital");
toplbl.setForeground(SystemColor.activeCaption);
toplbl.setFont(new Font("Arial Black", Font.BOLD, 17));
toplbl.setVerticalAlignment(SwingConstants.TOP);
toplbl.setBounds(182, 11, 71, 25);
frame.getContentPane().add(toplbl);
JLabel infolbl = new JLabel("Multipurpose app == orbital 1.0\r\n");
infolbl.setFont(new Font("Arial", Font.PLAIN, 11));
infolbl.setForeground(SystemColor.activeCaption);
infolbl.setBounds(138, 47, 165, 25);
frame.getContentPane().add(infolbl);
JButton signup_btn = new JButton("Sign Up");
signup_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
signup_form sup_for=new signup_form();
3.setVisible works here
sup_for.setVisible(true);
}
});
signup_btn.setFont(new Font("Arial", Font.PLAIN, 11));
signup_btn.setForeground(SystemColor.activeCaption);
signup_btn.setBounds(61, 133, 91, 23);
frame.getContentPane().add(signup_btn);
JButton signin_btn = new JButton("Sign In");
signin_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
signin_form log_for=new signin_form();
4.and here too
log_for.setVisible(true);
}
});
signin_btn.setFont(new Font("Arial", Font.PLAIN, 11));
signin_btn.setForeground(SystemColor.activeCaption);
signin_btn.setBounds(285, 133, 91, 23);
frame.getContentPane().add(signin_btn);
JLabel notelbl = new JLabel("note: click Sign Up for new account or Sign In for existing account.");
notelbl.setHorizontalAlignment(SwingConstants.CENTER);
notelbl.setBounds(10, 199, 406, 25);
frame.getContentPane().add(notelbl);
}
}
I faced the same problem, it is solved by exteding the JFrame class.
Your class should extends JFrame, because setVisible() method belongs to JFrameclass.
There seems to be a few issues with your code.
Most importantly: You have multiple entry points in your program.
You should only have one public static void main(String[] args) ... function in your application, that is where the 'program starts'.
The error message you get (The method setVisible(boolean) is undefined for the type orbital_app) comes from the fact that the class orbital_app don´t have a setVisible function, one of its members does, but that doesn't matter.
Your orbital_app have a private member that is a JFrame, which makes it possible for you to call the JFrames methods from inside the orbital_app by accessing the frame, but you cant reach it from outside.
It seems you have mixed up inheritance and ownership.
If you wish your orbital_app class to be a JFrame, you need to inherit from the JFrame. Else you could just implement the methods you wish to make public for your other classes.
Or you could just create a getter for the private JFrame object so that you can access it from outside.

Calling JPanel from another JPanel

I know this is ill advised but I am using absolute layout and trying to go from panel to panel and was curious how to do this.
public class Mainscreen extends JFrame {
private JPanel Home;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mainscreen frame = new Mainscreen();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mainscreen() {
final Dataentrylog DEL = new Dataentrylog();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 618, 373);
Home = new JPanel();
Home.setBackground(new Color(255, 250, 250));
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setContentPane(Home);
Home.setLayout(null);
JButton btnNewButton = new JButton("Data Entry login");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnNewButton.setForeground(new Color(0, 0, 0));
btnNewButton.setBackground(UIManager.getColor("Menu.selectionBackground"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Home.setVisible(false);
setContentPane(DEL);
setLayout(null);
}
});
btnNewButton.setBounds(44, 214, 204, 61);
Home.add(btnNewButton);
}
}
Calling this JPanel which works
import java.awt.EventQueue;
public class Dataentrylog extends JPanel {
public Dataentrylog() {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setLayout(null);
DEadmin DEA = new Deadmin();
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}
While this works if I try an call another JPanel from Dataentry log the JFrame is blank. What can I do to call another JPanel? Any help would be great. Also, I know that layoutmanagers are the norm but for what I had to do I wasn't able to find anything that worked so I choose to use null despite my best judgement. THANKS!
Intialization of Dataentrylog should be like this
final Dataentrylog DEL = new Dataentrylog(this);
we have to pass the parent jframe.
public class Dataentrylog extends JPanel {
public Dataentrylog(final JFrame parent ) {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
JPanel panel = new JPanel();
panel.add(new JButton("Solved"));
parent.setContentPane(panel);
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}

How to get instantly update from the textfield?

I want to change the input in the textfield which will instantly update to the display, instead of press ENTER button to update it.
Here is my code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyProgram01 extends JFrame
{
private JTextField text1;
private JCheckBox check1;
private JCheckBox check2;
private String message;
private JLabel label1;
private JLabel label2;
private Font font;
public MyProgram01(String title)
{
super(title);
check1 = new JCheckBox("Bold");
check2 = new JCheckBox("Italics");
label1 = new JLabel("Text : ");
label2 = new JLabel("Style : ");
message = "Good Morning...";
text1 = new JTextField(message, 100);
font = new Font("Times New Roman", Font.PLAIN, 36);
setBounds(0, 0, 600, 300);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 600, 120);
panel.setBackground(Color.ORANGE);
label1.setFont(new Font("Times New Roman", Font.PLAIN, 36));
label1.setBounds(15, 15, 100, 36);
panel.add(label1);
text1.setBounds(120, 15, 400, 36);
panel.add(text1);
label2.setFont(new Font("Times New Roman", Font.PLAIN, 36));
label2.setBounds(15, 65, 100, 36);
panel.add(label2);
check1.setBounds(120, 65, 100, 36);
check2.setBounds(220, 65, 100, 36);
panel.add(check1);
panel.add(check2);
check1.addActionListener(new CheckBoxListener());
check2.addActionListener(new CheckBoxListener());
text1.addActionListener(new TextFieldListener());
setLayout(null);
add(panel);
}
public void paint(Graphics g)
{
super.paint(g);
g.setFont(font);
g.drawString(message, 15, 255);
}
private class CheckBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(check1.isSelected() && check2.isSelected())
{
font = new Font("Times New Roman", Font.BOLD + Font.ITALIC, 36);
repaint();
}
else if(check1.isSelected())
{
font = new Font("Times New Roman", Font.BOLD, 36);
repaint();
}
else if(check2.isSelected())
{
font = new Font("Times New Roman", Font.ITALIC, 36);
repaint();
}
else
{
font = new Font("Times New Roman", Font.PLAIN, 36);
repaint();
}
}
}
private class TextFieldListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
message = text1.getText();
repaint();
}
}
public static void main(String[] args)
{
JFrame frame = new MyProgram01("My Program 01");
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
How can I change the code to instantly update to the display?
EDIT :
It's work with the keyListener, but my program will only start display after second key is pressed.
For example, I key in abc, the program will start show a when I press b, and when I pressed c, it displays ab and c is missing, unless I press ENTER.
Here the part of the code :
text1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
message = text1.getText();
repaint();
}
});
Add a KeyListener to your textfield.
You can do that like this:
textField.addKeyListener(new KeyAdapter(){
#Override
public void keyPressed(KeyEvent e){
message = textField.getText();
repaint();
}
});
OR
Add a DocumentListener to your textfield's Document.
You can do that like this:
private JFrame getFrame(){
return this;
}
...
textField.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
message = textField.getText();
getFrame().repaint();
}
#Override
public void removeUpdate(DocumentEvent e) {
message = textField.getText();
getFrame().repaint();
}
#Override
public void changedUpdate(DocumentEvent e) {
// on change
}
});
Instead of using ActionListener for the class TextFieldListener, use KeyListener interface and use the keyTyped(KeyEvent e) method. When ever the event arises you can use getText() of texfield and repaint it.

Categories

Resources