What do I do wrong and the second button acts weird? - java

I know the code is bad and easy. I am a starter and need some help. I want to add a second button to my code. I feel like I changed the coordinates of the button so it doesn't "fall" on the first one. Any suggestions? (Also, .this is a bit unknown to me so I just copy - pasted it hoping it doesnt affect the second button. I feel like it has to do with .this and the coordinates of the button I entered).
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI extends Frame implements ActionListener
{
static Operand op;
TextField display;
Button button0;
Button button1;
public CalculatorGUI(String title, Operand op)
{
super(title);
CalculatorGUI.op = op;
this.setLayout(null);
this.setFont(new Font("TimesRoman", Font.PLAIN, 14));
this.setBackground(Color.blue);
button0 = new Button("0");
button0.setBounds(64, 265, 35, 28);
button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button0.setForeground(Color.blue);
button0.setFocusable(false);
button0.addActionListener(this);
this.add(button0);
this.setSize(283,320);
this.setLocation(40,30);
this.setVisible(true);
this.setResizable(false);
button1 = new Button("1");
button1.setBounds(64, 230, 35, 28);
button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button1.setForeground(Color.blue);
button1.setFocusable(false);
button1.addActionListener(this);
this.add(button1);
this.setSize(283,320);
this.setLocation(40,30);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new CloseWindowAndExit());
display = new TextField("");
display.setEditable(false);
display.setBounds(13, 55, 257, 30);
this.add(display);
}
class CloseWindowAndExit extends WindowAdapter
{
public void windowClosing(WindowEvent closeWindowAndExit)
{
System.exit(0);
}
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button0)
{
display.setText("Button0 is pressed");
CalculatorGUI.op.addDigit('0');
}
}
}

Do not add buttons directly to the main frame. Instead, create another panel and add the buttons to it:
Panel newPanel = new Panel(new GridBagLayout());
Button button0 = new Button("BUTTON_0");
button0.setBounds(10, 265, 30, 30);
button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button0.setForeground(Color.blue);
button0.setFocusable(false);
Button button1 = new Button("BUTTON_1");
button1.setBounds(10, 265, 30, 30);
button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button1.setForeground(Color.blue);
button1.setFocusable(false);
newPanel.add(button0);
newPanel.add(button1);
this.add(newPanel);

Related

How to pass user input from one frame to another?

I'm currently a beginner in programming and I'm kind of having a hard time right now. Currently I'm making a simple shopping lister using java. Before I go to the problem, I would like to share my code first:
This is the main page of my list:
package com.main;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainPage extends JFrame implements ActionListener{
JTextPane txtpnListName, txtpnTotal, txtpnSubtotal, txtpnShoppingLists;
JTextField txtfNewItem, txtfPrice, txtItemAmount;
JTextArea textItemList, textAmountList, textPriceList;
JButton btnNewButton, btnNewButton_1, btnLogout, btnMakeNewList;
JScrollPane scrollPane1, scrollPane2,scrollPane3;
JTabbedPane tabPane;
public MainPage(){
super("List n\' Go");
setResizable(false);
getContentPane().setBackground(Color.WHITE);
setBounds(100, 100, 600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
tabPane = new JTabbedPane(JTabbedPane.LEFT);
add(tabPane);
txtpnListName = new JTextPane();
txtpnListName.setBackground(Color.WHITE);
txtpnListName.setEditable(false);
txtpnListName.setFont(new Font("Tahoma", Font.BOLD, 30));
txtpnListName.setText("LIST NAME");
txtpnListName.setBounds(175, 25, 175, 38);
getContentPane().add(txtpnListName);
//Buttons
btnNewButton = new JButton("Save");
btnNewButton.setBounds(375, 25, 75, 21);
getContentPane().add(btnNewButton);
btnNewButton_1 = new JButton("Add Item");
btnNewButton_1.setBounds(375, 59, 75, 21);
getContentPane().add(btnNewButton_1);
btnNewButton_1.addActionListener(this);
btnLogout = new JButton("Logout");
btnLogout.setBounds(463, 25, 75, 21);
getContentPane().add(btnLogout);
btnLogout.addActionListener(this);
txtfNewItem = new JTextField();
txtfNewItem.setBackground(Color.WHITE);
txtfNewItem.setEditable(false);
txtfNewItem.setFont(new Font("Tahoma", Font.PLAIN, 16));
txtfNewItem.setText("New Item");
txtfNewItem.setBounds(185, 99, 200, 28);
getContentPane().add(txtfNewItem);
txtfPrice = new JTextField();
txtfPrice.setBackground(Color.WHITE);
txtfPrice.setEditable(false);
txtfPrice.setFont(new Font("Tahoma", Font.PLAIN, 16));
txtfPrice.setText("Price");
txtfPrice.setBounds(490, 99, 75, 28);
getContentPane().add(txtfPrice);
txtItemAmount = new JTextField();
txtItemAmount.setBackground(Color.WHITE);
txtItemAmount.setEditable(false);
txtItemAmount.setFont(new Font("Tahoma", Font.PLAIN, 16));
txtItemAmount.setText("Amount");
txtItemAmount.setBounds(395, 99, 75, 28);
getContentPane().add(txtItemAmount);
//ListBox
scrollPane1 = new JScrollPane();
scrollPane1.setBounds(185, 133, 210, 330);
getContentPane().add(scrollPane1);
scrollPane2 = new JScrollPane();
scrollPane2.setBounds(400, 133, 65, 330);
getContentPane().add(scrollPane2);
scrollPane3 = new JScrollPane();
scrollPane3.setBounds(490, 133, 70, 330);
getContentPane().add(scrollPane3);
textItemList = new JTextArea();
textItemList.setEditable(false);
scrollPane1.setViewportView(textItemList);
textItemList.setBackground(Color.GRAY);
textAmountList = new JTextArea();
textAmountList.setEditable(false);
scrollPane2.setViewportView(textAmountList);
textAmountList.setBackground(Color.GRAY);
textPriceList = new JTextArea();
textPriceList.setEditable(false);
scrollPane3.setViewportView(textPriceList);
textPriceList.setBackground(Color.GRAY);
//Summation
txtpnTotal = new JTextPane();
txtpnTotal.setEditable(false);
txtpnTotal.setFont(new Font("Tahoma", Font.PLAIN, 14));
txtpnTotal.setText("Total no. of Items:");
getContentPane().add(txtpnTotal);
txtpnTotal.setBounds(195, 473, 150, 38);
txtpnSubtotal = new JTextPane();
txtpnSubtotal.setEditable(false);
txtpnSubtotal.setFont(new Font("Tahoma", Font.PLAIN, 14));
txtpnSubtotal.setText("Subtotal:");
txtpnSubtotal.setBounds(360, 473, 85, 67);
getContentPane().add(txtpnSubtotal);
txtpnShoppingLists = new JTextPane();
txtpnShoppingLists.setEditable(false);
txtpnShoppingLists.setFont(new Font("Tahoma", Font.BOLD, 15));
txtpnShoppingLists.setText("Shopping Lists");
txtpnShoppingLists.setBounds(10, 10, 120, 31);
getContentPane().add(txtpnShoppingLists);
//For new List
btnMakeNewList = new JButton("Make New List");
btnMakeNewList.setBounds(10, 519, 120, 21);
getContentPane().add(btnMakeNewList);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent exit){
int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the app?", "Confirm Exit",0);
if(reply == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Thank your for using List n' Go!", "Goodbye!",1);
System.exit(0);
}
else{
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
});
setSize(600,600);
setVisible(true);
setResizable(false);
}
//for testiong purposes
public static void main (String []args){
MainPage mainpage = new MainPage();
}
//Action Listeners
public void actionPerformed(ActionEvent e) {
//logout Button
if(e.getSource() == btnLogout){
int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to Logout?", "Confirm Logout?",2);
if( reply == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "You have logged out.", "Logout Success",1);
LoginScreen login = new LoginScreen();
dispose();
}
else{
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
// Make new List Button
}
else if(e.getSource() == btnMakeNewList){
}
// Add item to list Button
else if(e.getSource() == btnNewButton_1){
}
else if (e.getSource() == btnNewButton){
}
}
}
And here is the GUI where I want to ask the user for input:
package com.main;
import javax.swing.*;
import java.awt.event.*;
public class AddToList extends JFrame implements ActionListener{
JLabel itemName, itemAmount, itemPrice, itemTotalPrice;
JTextField jtfItemName, jtfItemAmount, jtfItemPrice, jtfTotalPrice;
JButton addItem, goBack;
public AddToList(){
super("What do you want to add?");
setLayout(null);
itemName = new JLabel("Item Name");
itemAmount = new JLabel("Item Amount");
itemPrice = new JLabel("Item Price");
itemTotalPrice = new JLabel();
jtfItemName = new JTextField();
jtfItemAmount = new JTextField();
jtfItemPrice = new JTextField();
jtfTotalPrice = new JTextField();
addItem = new JButton("Add Item");
goBack = new JButton("Back");
itemName.setBounds(10,10, 70,15);
jtfItemName.setBounds(75,10, 270,20);
itemAmount.setBounds(350,10, 70,15);
jtfItemAmount.setBounds(425,10, 120,20);
itemPrice.setBounds(550,10, 70,15);
jtfItemPrice.setBounds(610,10, 120,20);
addItem.setBounds(250,35, 100,25);
goBack.setBounds(400,35, 100,25);
addItem.addActionListener(this);
goBack.addActionListener(this);
add(itemName);add(jtfItemName);add(itemAmount);
add(jtfItemAmount);add(itemPrice);add(jtfItemPrice);
add(addItem);add(goBack);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent exit){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
});
setSize(750, 100);
setResizable(false);
setVisible(true);
}
// test Program
public static void main(String []args){
AddToList test = new AddToList();
}
// Action Trigger
public void actionPerformed(ActionEvent e) {
//Add item Confirmation
if(e.getSource() == addItem){
}
// Cancel Add Item
else if(e.getSource() == goBack){
}
}
}
My goal here is after the user clicked the "Add Item" button from the Main page, it will prompt the user to input the product details such as the name, amount, and price on the add Item GUI. it will then take the user input and list it on the main page. After that it will calculate the total amount of items on the list, it's amount, and total price. The problem is that I am currently stuck on how can I pass the user input from the Add Item GUI to the main page. I've been researching but I still couldn't find my way around it. I hope you can help me and any help is much appreciated.
P.S: I'm sorry if you find my code a bit too long because this is what currently I could do with the best of my knowledge and abilities. But don't worry, I know that I shouldn't use setLayout(null) since swing has layout managers that I can use. I am still currently learning more about it so bear with me please.
Usually I have a JFrame holding most of the data. If additional information is required by the application, it will show an input dialog. Most of the time this input is not just a string but some more complex stuff. So here is the pattern I follow:
Create a form derived from JPanel. Add the UI elements you need, and use the bean pattern so the necessary data can be injected/retrieved. Very likely you want to show such input when a button in a pane is pressed, so the ActionListener code can look like this:
FormBean fb = new FormBean();
fb.setData(...); (use setters to display data if required/feasible)
if (JOptionPane.showOptionDialog(this, fb, ...)==JOptionPane.OK_OPTION) {
// user pressed ok, so process the data he entered
fb.getData();
}
The advantage of using JOptionPane.showOptionDialog is that you get a standard modal dialog window without threading issues or the question when to activate your code again.

JTextField and JComboBox make my JPanel disappear from the screen

whenever I uncomment my JTextField or JComboBox they make all my swing elements disappear, I've tried resizing them and manipulating them in any way but I have no clue as to why the stuff is disappearing. They weren't on a previous program I wrote and they're wrote the exact same way. They elements even disappear if the two objects aren't even added to the panel.
Here's the code I have for my program. I don't really want to separate the 2 panels into their own Classes.
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
// Create window
JFrame window = new Window();
String[] optionsToChoose = {"Car Parts", "Fast Food", "Groceries", "Clothing", "Gas", "Store", "Entertainment"};
// Create first screen
JPanel startPanel = new JPanel();
startPanel.setLayout(null);
startPanel.setBounds(230, 230, 500, 250);
startPanel.setBackground(new Color(177, 177, 177));
JLabel header = new JLabel("CHOOSE AN ACTION");
header.setForeground(Color.white);
header.setFont(new Font("Verdana", Font.BOLD, 20));
header.setHorizontalAlignment(JLabel.CENTER);
header.setBounds(0, -25, 500, 150);
JButton add = new JButton("ADD");
add.setBackground(new Color(0, 109, 91));
add.setForeground(Color.white);
add.setFont(new Font("Verdana", Font.BOLD, 15));
add.setBounds(90, 125, 150, 50);
JButton view = new JButton("VIEW REPORT");
view.setBackground(new Color(167, 199, 231));
view.setForeground(Color.white);
view.setFont(new Font("Verdana", Font.BOLD, 15));
view.setBounds(270, 125, 150, 50);
startPanel.add(view);
startPanel.add(add);
startPanel.add(header);
startPanel.setVisible(false);
// Create second screen
JPanel addPanel = new JPanel();
addPanel.setBounds(230, 220, 500, 300);
addPanel.setLayout(null);
addPanel.setBackground(Color.black);
JLabel header2 = new JLabel("INPUT DATA BELOW:");
header2.setBounds(0, -25, 500, 150);
header2.setFont(new Font("Verdana", Font.BOLD, 20));
header2.setHorizontalAlignment(JLabel.CENTER);
header2.setForeground(Color.white);
JLabel costHeader = new JLabel("COST:");
costHeader.setForeground(Color.white);
costHeader.setFont(new Font("Verdana", Font.BOLD, 20));
costHeader.setBounds(90, 75, 150, 50);
JLabel typeHeader = new JLabel("TYPE:");
typeHeader.setBounds(300, 75, 150, 50);
typeHeader.setForeground(Color.WHITE);
typeHeader.setFont(new Font("Verdana", Font.BOLD, 20));
JTextField costInput = new JTextField("$0", 10);
costInput.setFont(new Font("Verdana", Font.PLAIN, 20));
costInput.setBounds(90, 150, 150, 50);
costInput.setForeground(Color.white);
JComboBox<String> jComboBox = new JComboBox<>(optionsToChoose);
jComboBox.setFont(new Font("Verdana", Font.PLAIN, 20));
jComboBox.setBounds(0, 0, 150, 50);
addPanel.add(jComboBox);
addPanel.add(costInput);
addPanel.add(typeHeader);
addPanel.add(header2);
addPanel.add(costHeader);
addPanel.setVisible(true);
// Add components
window.add(startPanel);
window.add(addPanel);
}
}
Here's the Window class as well:
import java.awt.Color;
import javax.swing.JFrame;
public class Window extends JFrame {
public Window() {
this.setTitle("Finance Tracker");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
//this.setLocationRelativeTo(null);
this.setSize(1000, 800);
this.setVisible(true);
this.getContentPane().setBackground(new Color(177, 177, 177));
}
}
If anyone has any idea as to why this could be happening, any help is appreciated. Thanks!

Program Terminates without running in eclipse

When I run this code ( having simple buttons and a LOGIN button with an action listener), it terminates without running and without showing screen.
I have tried System.exit(0); in main function to overcome this terminating issue but all in vain
public class HOme extends JFrame{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
Color cardinal = new Color(194, 35, 38);
int w=155;
int h=50;
public HOme(String title) {
super(title);
getContentPane().setSize(width,height);
getContentPane().setBackground(Color.WHITE);
getContentPane().setLayout(null);
final JPanel panel2 = new JPanel();
panel2.setBounds(364, 33, 664, 344);
getContentPane().add(panel2);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.WHITE);
panel3.setBounds(81, 382, 947, 243);
getContentPane().add(panel3);
panel3.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnHome.setForeground(Color.WHITE);
btnHome.setBackground(cardinal);
btnHome.setBounds(517, 33, w, h);
btnHome.setContentAreaFilled(false);
btnHome.setOpaque(true);
panel3.add(btnHome);
JButton btnClients = new JButton("Clients");
btnClients.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnClients.setForeground(Color.WHITE);
btnClients.setBounds(690, 33, w, h);
btnClients.setBackground(cardinal);
btnClients.setContentAreaFilled(false);
btnClients.setOpaque(true);
panel3.add(btnClients);
JButton btnClose = new JButton("Close");
btnClose.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnClose.setForeground(Color.WHITE);
btnClose.setBounds(690, 198, w, h);
btnClose.setBackground(cardinal);
btnClose.setContentAreaFilled(false);
btnClose.setOpaque(true);
panel3.add(btnClose);
JButton btnLogin = new JButton("Admin Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Login l=new Login();
panel2.add(l);
}
});
btnLogin.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnLogin.setForeground(Color.WHITE);
btnLogin.setBounds(517, 116, w, h);
btnLogin.setBackground(cardinal);
btnLogin.setContentAreaFilled(false);
btnLogin.setOpaque(true);
panel3.add(btnLogin);
JPanel panel1 = new JPanel();
panel1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(204, 51, 0), null));
panel1.setBackground(Color.WHITE);
panel1.setBounds(81, 33, 263, 344);
getContentPane().add(panel1);
panel1.setLayout(null);
JButton btnStartMonitoring = new JButton("");
btnStartMonitoring.setIcon(new ImageIcon(path1));
btnStartMonitoring.setBackground(cardinal);
btnStartMonitoring.setForeground(Color.WHITE);
btnStartMonitoring.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnStartMonitoring.setBounds(10, 274, 239, 59);
panel1.add(btnStartMonitoring);
JLabel lblLogo = new JLabel("New label");
lblLogo.setIcon(new ImageIcon(path2));
lblLogo.setBounds(0, 11, 263, 253);
panel1.add(lblLogo);
}
public static void main(String args[]) {
new HOme("HOme");
//System.exit(0);
}
}
Edited
I have a login class extended from JPanel. When I click on Login Button from Home. It is not showing Login panel
Login.class
public class Login extends JPanel {
private JTextField txtPassword;
private JTextField txtID;
Color cardinal = new Color(194, 35, 38);
int w=155;
int h=50;
public Login() {
setBackground(Color.WHITE);
setLayout(null);
JLabel lblLogin = new JLabel("Login ");
lblLogin.setBackground(Color.ORANGE);
lblLogin.setHorizontalAlignment(SwingConstants.RIGHT);
lblLogin.setFont(new Font("Trajan Pro", Font.BOLD, 36));
lblLogin.setBounds(125, 0, 424, 59);
lblLogin.setBackground(cardinal);
//lblLogin.setContentAreaFilled(false);
lblLogin.setOpaque(true);
lblLogin.setForeground(Color.white);
add(lblLogin);
JLabel lblId = new JLabel("ID");
lblId.setHorizontalAlignment(SwingConstants.RIGHT);
lblId.setFont(new Font("Tekton Pro", Font.PLAIN, 23));
lblId.setBounds(181, 127, 66, 28);
add(lblId);
JLabel lblPassword = new JLabel("Password");
lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
lblPassword.setFont(new Font("Tekton Pro", Font.PLAIN, 23));
lblPassword.setBounds(136, 188, 111, 28);
add(lblPassword);
txtPassword = new JTextField();
lblPassword.setLabelFor(txtPassword);
txtPassword.setBounds(266, 183, 256, 41);
lblPassword.setForeground(cardinal);
add(txtPassword);
txtPassword.setColumns(10);
txtID = new JTextField();
lblId.setLabelFor(txtID);
txtID.setBounds(266, 123, 256, 39);
lblId.setForeground(cardinal);
add(txtID);
txtID.setColumns(10);
JButton btnLogin = new JButton("Login");
btnLogin.setForeground(Color.WHITE);
btnLogin.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnLogin.setBounds(324, 294, w, h);
btnLogin.setBackground(cardinal);
btnLogin.setContentAreaFilled(false);
btnLogin.setOpaque(true);
add(btnLogin);
setVisible(true);
}
You are not making your JFrame visible.
You can do either -
In your constructor, make it visible by adding the following line at the end -
setVisible(true);
Or in your main() function you can do -
HOme h = new HOme("HOme");
h.setVisible(true);
Answer to second part:
Import MouseListener, import java.awt.event.MouseListener;
Construct a MouseListener somewhere, include action of the button
Where you define btnLogin, add a line btnLogin.addMouseListener(<name of MouseListener>);
An example: http://www.java2s.com/Code/Java/Swing-JFC/ButtonActionSample.htm
Add something like setVisible(true); at the end of the HOme method.

RE-enable a button after closing a frame java

so im making a program for my project.
and when i clicked a button it must open anohter frame and make the button unclickable. and when you closed the popup frame the button must re enable. so this is
my main frame
package Option2;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainMenu {
int intCtr1 = 0;
JFrame frame1 = new JFrame("EXD LAN PARTY");
JButton Button1 = new JButton();
JButton Button2 = new JButton();
JButton Button3 = new JButton();
JButton Button4 = new JButton();
JLabel Label1 = new JLabel();
public void MainMenu(){
//BUTTON1
Button1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI2.jpg")));
Button1.setBackground(Color.white);
Button1.setBounds(50, 350, 150, 150);
Button1.setToolTipText("Personal Info");
//BUTTON1 END
//BUTTON2
Button2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PC.jpg")));
Button2.setBackground(Color.white);
Button2.setBounds(250, 350, 150, 150);
Button2.setToolTipText("PC INFO");
//BUTTON2 END
//BUTTON3
Button3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Games.jpg")));
Button3.setBackground(Color.white);
Button3.setBounds(450, 350, 150, 150);
Button3.setToolTipText("Games");
//BUTTON3 END
//BUTTON4 END
Button4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Players.jpg")));
Button4.setBackground(Color.white);
Button4.setBounds(650, 350, 150, 150);
Button3.setToolTipText("Players");
//BUTTON4 END
//LABEL1
Label1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/EXD.jpg")));
Label1.setBounds(50, 50, 800, 250);
//LABEL1 END
//Frame1
frame1.getContentPane().setBackground(Color.black);
frame1.setResizable(false);
frame1.setLayout(null);
frame1.setSize(870,650);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(Label1);
frame1.add(Button1);
frame1.add(Button2);
frame1.add(Button3);
frame1.add(Button4);
//Frame1 END
Button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PersonalInfo objPI = new PersonalInfo();
objPI.Menu1();
Button1.setEnabled(false);
}
});
Button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PCInfo objPCI = new PCInfo();
objPCI.Menu2();
}
});
Button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Games objGames = new Games();
objGames.Menu3();
}
});
Button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Players objPlayers = new Players();
objPlayers.Menu4();
}
});
}
public void dim1(){
if(intCtr1 == 1){
MainMenu objMM = new MainMenu();
objMM.Button1.setEnabled(true);
System.out.println("SD");
}
}
}
**and this is my sub frame**
package Option2;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PersonalInfo {
String[] arrSex = {"Male","Female"};
JFrame frame1 = new JFrame("Personal Info");
JLabel label1 = new JLabel("ID");
JLabel label2 = new JLabel("Last Name");
JLabel label3 = new JLabel("First Name");
JLabel label4 = new JLabel("Middle Name");
JLabel label5 = new JLabel("SEX");
JLabel label6 = new JLabel();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JTextField tf4 = new JTextField();
JComboBox CB1 = new JComboBox(arrSex);
JButton Button1 = new JButton("NEW");
JButton Button2 = new JButton("SAVE");
JButton Button3 = new JButton("EDIT");
JButton Button4 = new JButton("CANCEL");
JButton Button5 = new JButton();
JButton Button6 = new JButton();
JButton Button7 = new JButton();
JButton Button8 = new JButton();
public void Menu1(){
//Frame1
frame1.add(label6);
frame1.add(label1);
frame1.add(tf1);
frame1.add(label2);
frame1.add(tf2);
frame1.add(label3);
frame1.add(tf3);
frame1.add(label4);
frame1.add(tf4);
frame1.add(label5);
frame1.add(CB1);
frame1.add(Button1);
frame1.add(Button2);
frame1.add(Button3);
frame1.add(Button4);
frame1.add(Button5);
frame1.add(Button6);
frame1.add(Button7);
frame1.add(Button8);
frame1.setVisible(true);
frame1.getContentPane().setBackground(Color.black);
frame1.setSize(600,600);
frame1.setResizable(false);
frame1.setLayout(null);
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
MainMenu objMM = new MainMenu();
objMM.intCtr1=1;
objMM.dim1();
}
});
//Frame1 End
//LABEL6
label6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI.jpg")));
label6.setBounds(30, 5, 800, 250);
//LABEL6 END
//LABEl1
label1.setBounds(100, 220, 50,50);
label1.setForeground(Color.white);
label1.setFont(new Font("Serif", Font.BOLD, 18));
//Label1 end
//Tf
tf1.setBounds(130, 230, 400,30);
tf1.setEnabled(false);
SmartC objSMC = new SmartC();
tf1.setText(objSMC.SmartCounter("ABC123415XYZS"));
//tf end
//label2
label2.setBounds(35, 255, 120,50);
label2.setForeground(Color.white);
label2.setFont(new Font("Serif", Font.BOLD, 18));
//label2 end
//Tf2
tf2.setBounds(130, 270, 400,30);
//tf2 end
//label3
label3.setBounds(35, 295, 120,50);
label3.setForeground(Color.white);
label3.setFont(new Font("Serif", Font.BOLD, 18));
//label3 end
//Tf3
tf3.setBounds(130, 310 , 400, 30);
//tf3 end
//label4
label4.setBounds(15, 335, 120,50);
label4.setForeground(Color.white);
label4.setFont(new Font("Serif", Font.BOLD, 18));
//label4 end
//Tf4
tf4.setBounds(130, 350 , 400, 30);
//tf4 end
//label4
label5.setBounds(85, 375, 120,50);
label5.setForeground(Color.white);
label5.setFont(new Font("Serif", Font.BOLD, 18));
//label4 end
//cb1
CB1.setBounds(130, 390, 100, 30);
CB1.setBackground(Color.white);
//cb1 end
//button1
Button1.setBounds(35, 450, 100, 30);
Button1.setBackground(Color.white);
//
//
Button2.setBounds(150, 450, 100, 30);
Button2.setBackground(Color.white);
//
//
Button3.setBounds(335, 450, 100, 30);
Button3.setBackground(Color.white);
//
//
Button4.setBounds(450, 450, 100, 30);
Button4.setBackground(Color.white);
//
//
Button5.setBounds(35, 500, 100, 50);
Button5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/First.jpg")));
Button5.setBackground(Color.white);
//
//
Button6.setBounds(150, 500, 100, 50);
Button6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Previous.jpg")));
Button6.setBackground(Color.white);
//
//
Button7.setBounds(335, 500, 100, 50);
Button7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Next.jpg")));
Button7.setBackground(Color.white);
//
//
Button8.setBounds(450, 500, 100, 50);
Button8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Last.jpg")));
Button8.setBackground(Color.white);
//
//
}
}
Any Suggestions about my coding is accepted. Sorry Still learning about Java :)
and when i clicked a button it must open anohter frame
You should NOT be creating another JFrame.
Instead you should be creating a modal JDialog. The dialog will not allow you to click on the frame until the dialog is closed.
Any Suggestions about my coding is accepted
Follow Java naming conventions. Variable names should NOT start with an upper case character. Sometimes you follow this guideline and sometimes you don't. Be consistent!
Don't use setBounds(...). Swing was designed to be used with layout managers!
First, please read Java naming conventions.
You need to pass the reference of MainMenu to PersonalInfo in order to achieve what you need, here:
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PersonalInfo objPI = new PersonalInfo(this);
objPI.menu1();
button1.setEnabled(false);
}
});
And you need to add a constructor to PersonalInfo:
private MainMenu m;
public PersonalInfo(MainMenu m) {
this.m = m;
}
Add a public method to MainMenu:
public void enableMyButton() {
button1.setEnabled(true);
}
Now you can add an event listener to PersonalInfo to enable the button of the MainMenu frame:
frame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
this.m.enableMyButton();
}
});

How to layout this GUI?

I am creating a GUI in java. Currently i have an empty JFrame and am trying to add a JPanel to it. The JPanel contains buttons, text etc. However none of this is being properly displayed. My code is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class memoDisplayUI {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
memoDisplayUI frame = new memoDisplayUI();
frame.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public memoDisplayUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 270, 400);
frame.setUndecorated(true); //REMOVES MENU BAR
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblMemos = new JLabel("MEMOS");
lblMemos.setForeground(new Color(100, 149, 237));
lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
lblMemos.setBounds(16, 16, 234, 37);
panel.add(lblMemos);
JButton button = new JButton("");
button.setBackground(new Color(100, 149, 237));
button.setBounds(7, 350, 40, 40);
panel.add(button);
button.setIcon(new ImageIcon("back.png"));
JButton button_1 = new JButton("");
button_1.setBackground(new Color(100, 149, 237));
button_1.setBounds(113, 350, 40, 40);
panel.add(button_1);
button_1.setIcon(new ImageIcon("Edit.png"));
JButton button_2 = new JButton("");
button_2.setBackground(new Color(100, 149, 237));
button_2.setBounds(220, 350, 40, 40);
panel.add(button_2);
button_2.setIcon(new ImageIcon("memo.png"));
JButton btnExit = new JButton("");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setBorder(null);
btnExit.setIcon(new ImageIcon("Exit.jpg"));
btnExit.setBounds(216, 19, 40, 40);
panel.add(btnExit);
jTextBox = new JTextArea();
scroll.setViewportView(jTextBox); // add scroll panel
jTextBox.setTabSize(4);
jTextBox.setLineWrap(true);
jTextBox.setBackground(new Color(192, 192, 192));
jTextBox.setBounds(8, 60, 255, 286);
panel.add(jTextBox);
frame.setContentPane(panel);
}
}
My desired layout is something very similar to this:
Could someone advise as to why this is.
Thanks for any help :)

Categories