I'm currently working on a project and whenever I click the jbutton on jframe 2 (the 2nd jframe after the log in) "set appointments"/btn1, it won't show the other jframe which is jframe3.
The program itself works but the button won't show the other jframe.
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Container;
public class me {
public static void main (String [] args) {
JFrame jframe = new JFrame();
jframe.setSize(450,350);
jframe.getContentPane().setBackground(Color.WHITE);
ImageIcon c = new ImageIcon("teethlogo5.png");
JLabel bi = new JLabel("",c,JLabel.RIGHT);
bi.setBounds(25,35,400,40);
jframe.add(bi);
ImageIcon a = new ImageIcon("teethlogo2.png");
JLabel si = new JLabel("",a,JLabel.RIGHT);
si.setBounds(50,90,100,120);
jframe.add(si);
JLabel jl1 = new JLabel("Username:");
jl1.setBounds(190,100,100,50);
jframe.add(jl1);
JTextField uss = new JTextField();
uss.setBounds(270,110,120,30);
jframe.add(uss);
JLabel jl2 = new JLabel("Password:");
jl2.setBounds(190,150,100,50);
jframe.add(jl2);
JPasswordField pss = new JPasswordField();
pss.setBounds(270,160,120,30);
jframe.add(pss);
JButton enter = new JButton("log in");
enter.setBounds(250,210,100,40);
jframe.add(enter);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String userText;
String pwdText;
userText = uss.getText();
pwdText = String.valueOf(pss.getPassword());
if (userText.equals("user") && pwdText.equals("pass")) {
JOptionPane.showMessageDialog(null, "LoginSuccessful","Message",JOptionPane.PLAIN_MESSAGE);
jframe.setVisible(false);
JFrame jframe2 = new JFrame();
jframe2.setSize(850,560);
jframe2.getContentPane().setBackground(Color.WHITE);
ImageIcon b = new ImageIcon("teethlogo4.png");
JLabel sii = new JLabel("",b,JLabel.RIGHT);
sii.setBounds(10,0,600,100);
jframe2.add(sii);
JButton btn1 = new JButton("Set an Appointment");
btn1.setBounds(100,100,150,30);
jframe2.add(btn1);
JButton btn2 = new JButton("View Appointments");
btn2.setBounds(270,100,150,30);
jframe2.add(btn2);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (btn1.isSelected()){
jframe2.setVisible(false);
JFrame jframe3 = new JFrame();
jframe3.setSize(850,560);
jframe3.getContentPane().setBackground(Color.WHITE);
jframe3.setLayout(null);
jframe3.setVisible(true);
jframe3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
});
jframe2.setLayout(null);
jframe2.setVisible(true);
jframe2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else {
JOptionPane.showMessageDialog(null, "Invalid Username or Password","Message",JOptionPane.PLAIN_MESSAGE);
uss.setText(null);
pss.setText(null);
}
}
});
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
jframe.setLayout(null);
jframe.setVisible(true);
}
}
I'm a beginner in java and I really want to know how to fix this problem.
You're shooting your own self in the foot with:
if (btn1.isSelected()){
// ...
}
There is no need for this block of code -- a button isn't "selected" unless it extends from JToggleButton (such as JCheckBox) and has been checked, something that a JButton does not allow, and what is more, it is preventing the listener's code that it holds from running. Solution: just get rid of it.
Related
I'm attempting to create a launch page. I want to open an initial window where the user can either click an 'admin' or 'employee' button which will each open a corresponding window, and close the original window.
So far I'm finding that when clicked the two buttons don't do anything, despite the ActionListener methods in the code below. I'd really appreciate any advice on where I'm going wrong (am a beginner).
Main
package deskBooking;
public class Main {
public static void main(String[] args) {
LaunchWindow launchWindow = new LaunchWindow();
}
}
LaunchWindow
package deskBooking;
import javax.swing.*;
import java.awt.event.*;
public class LaunchWindow implements ActionListener{
// An initial window where users can declare sign in type (Administrator/employee)
// Depending on selection will direct user to an employee or administrator sign in journey
JFrame frame = new JFrame("Desk Booking step 1/4");
JPanel panel = new JPanel();
JButton employeeButton = new JButton("Employee");
JButton adminButton = new JButton("Admin");
LaunchWindow() {
frame.setSize(600,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
JLabel label = new JLabel("Welcome to the desk booking system! "+"\n"+"Please declare if you are an admin or a normal user:");
label.setBounds(50, 20, 80, 25);
panel.add(label);
panel.add(employeeButton);
employeeButton.addActionListener(this);
employeeButton.setBounds(100,160,200,40);
employeeButton.setFocusable(false);
panel.add(adminButton);
adminButton.addActionListener(this);
adminButton.setBounds(100,160,200,40);
adminButton.setFocusable(false);
frame.setVisible(true);
}
#Override
public void actionPerformed (ActionEvent e) {
if(e.getSource()=="employeeButton") {
frame.dispose();
EmployeeWindow employeeWindow = new EmployeeWindow();
}
else if(e.getSource()=="adminButton") {
frame.dispose();
AdminWindow adminWindow = new AdminWindow();
}
}
}
EmployeeWindow
package deskBooking;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class EmployeeWindow {
JFrame frame2 = new JFrame("Employee log-in 2/4");
JPanel employeePanel = new JPanel();
JLabel label = new JLabel("Username: ");
JLabel label2 = new JLabel("Password: ");
EmployeeWindow(){
frame2.setSize(200,200);
frame2.add(employeePanel);
employeePanel.add(label);
employeePanel.add(label2);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
AdminWindow
package deskBooking;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AdminWindow {
JFrame frame3 = new JFrame("Admin log-in 2/4");
JPanel adminPanel = new JPanel();
JLabel label = new JLabel("Username: ");
JLabel label2 = new JLabel("Password: ");
AdminWindow(){
frame3.setSize(200,200);
frame3.add(adminPanel);
adminPanel.add(label);
adminPanel.add(label2);
frame3.setVisible(true);
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
So, I'm brand spankin' new to programming, so thanks in advance for your help. I'm trying to put this base 2 to base 10/base 10 to base 2 calculator I have made into a GUI. For the life of me I can't figure out how to nicely format it. I'm trying to make it look like the following: The two radio buttons on top, the input textfield bellow those, the convert button bellow that, the output field bellow that, and the clear button bellow that. Any ideas on how I can accomplish this?
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
#SuppressWarnings("serial")
public class GUI extends JFrame implements ActionListener
{
private JTextField input;
private JTextField output;
private JRadioButton base2Button;
private JRadioButton base10Button;
private JButton convert;
private JButton clear;
private Container canvas = getContentPane();
private Color GRAY;
public GUI()
{
this.setTitle("Base 10-2 calc");
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//this.setLayout(new GridLayout(2,2));
base2Button = new JRadioButton( "Convert to base 2");
base10Button = new JRadioButton( "Convert to base 10");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(base2Button);
radioGroup.add(base10Button);
JPanel radioButtonsPanel = new JPanel();
radioButtonsPanel.setLayout( new FlowLayout(FlowLayout.LEFT) );
radioButtonsPanel.add(base2Button);
radioButtonsPanel.add(base10Button);
canvas.add(radioButtonsPanel);
base2Button.setSelected( true );
base10Button.setSelected( true );
input = new JTextField(18);
//input = new JFormattedTextField(20);
canvas.add(input);
output = new JTextField(18);
//output = new JFormattedTextField(20);
canvas.add(output);
convert = new JButton("Convert!");
convert.addActionListener(this);
canvas.add(convert);
clear = new JButton("Clear");
clear.addActionListener(this);
canvas.add(clear);
output.setBackground(GRAY);
output.setEditable(false);
this.setSize(300, 200);
this.setVisible(true);
this.setLocation(99, 101);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
GUI app = new GUI();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Convert!"))
{
String numS = input.getText();
int numI = Integer.parseInt(numS);
if(base2Button.isSelected())
{
output.setText(Integer.toBinaryString(Integer.valueOf(numI)));
}
if(base10Button.isSelected())
{
output.setText("" + Integer.valueOf(numS,2));
}
}
if(s.equals("Clear"))
{
input.setText("");
output.setText("");
}
}
}
For a simple layout, you could use a GridLayout with one column and then use a bunch of child panels with FlowLayout which align the components based on the available space in a single row. If you want more control, I'd suggest learning about the GridBagLayout manager which is a more flexible version of GridLayout.
public class ExampleGUI {
public ExampleGUI() {
init();
}
private void init() {
JFrame frame = new JFrame();
// Set the frame's layout to a GridLayout with one column
frame.setLayout(new GridLayout(0, 1));
frame.setPreferredSize(new Dimension(300, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Child panels, each with FlowLayout(), which aligns the components
// in a single row, until there's no more space
JPanel radioButtonPanel = new JPanel(new FlowLayout());
JRadioButton button1 = new JRadioButton("Option 1");
JRadioButton button2 = new JRadioButton("Option 2");
radioButtonPanel.add(button1);
radioButtonPanel.add(button2);
JPanel inputPanel = new JPanel(new FlowLayout());
JLabel inputLabel = new JLabel("Input: ");
JTextField textField1 = new JTextField(15);
inputPanel.add(inputLabel);
inputPanel.add(textField1);
JPanel convertPanel = new JPanel(new FlowLayout());
JButton convertButton = new JButton("Convert");
convertPanel.add(convertButton);
JPanel outputPanel = new JPanel(new FlowLayout());
JLabel outputLabel = new JLabel("Output: ");
JTextField textField2 = new JTextField(15);
outputPanel.add(outputLabel);
outputPanel.add(textField2);
// Add the child panels to the frame, in order, which all get placed
// in a single column
frame.add(radioButtonPanel);
frame.add(inputPanel);
frame.add(convertPanel);
frame.add(outputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ExampleGUI example = new ExampleGUI();
}
}
The end result:
I am learning Java and working on a project. I want to add a new panel as the button is clicked. It is working, but if user clicks button again it adds the panel again. I want to remove the existing panel before adding new one. I have tried remove(westPanel); revalidate(); at the start of my actionPerformed method, but its not working.
Can someone please help?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyJpanel extends JPanel implements ActionListener
{ //declaring some buttons
private JButton newAccountButton, deleteAccountButton, searchButton, showButton, depositButton, withdrawButton;
protected JPanel westPanel, panel;
JLabel message = new JLabel("Please Use Buttons For Interation");
public MyJpanel()
{
newAccountButton = new JButton("Create Account");
deleteAccountButton = new JButton("Delete Account");
searchButton = new JButton("Search Account");
showButton = new JButton("Show Accounts");
depositButton = new JButton("Deposit");
withdrawButton = new JButton("Withdraw");
setBackground(Color.GRAY);
add(newAccountButton);
add(deleteAccountButton);
add(searchButton);
add(showButton);
add(depositButton);
add(withdrawButton);
newAccountButton.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e)
{ westPanel= new JPanel();
if (e.getSource()==newAccountButton)
{ getRootPane().remove(westPanel);
revalidate();
JLabel accountNumber = new JLabel("Account Number");
JTextField tfaccountnumber = new JTextField(10);
JLabel name = new JLabel("Owner Name");
JTextField tfName = new JTextField(10);
JLabel password = new JLabel("Password");
JTextField tfPassword = new JTextField(10);
JLabel deposit = new JLabel("Deposit Amount");
JTextField tfDeposit = new JTextField(10);
westPanel.setLayout(new GridLayout(4,2));
westPanel.setBackground(getBackground());
westPanel.add(accountNumber);
westPanel.add(tfaccountnumber);
westPanel.add(name);
westPanel.add(tfName);
westPanel.add(password);
westPanel.add(tfPassword);
westPanel.add(deposit);
westPanel.add(tfDeposit);
add(westPanel);
revalidate();
}
}
}
I am trying to make a Tic Tac Toe game which is in progress. I have problems loading images onto JButton. I have just programmed a single button right now. I have made a res file which is like res/image/Cross.png and Circle.png . They are in the build path. But even after clicking the button, image is not displayed
My one and only class
package com.arjav.tictactoe ;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TTT implements ActionListener{
JFrame frame ;
int turn = 0 ;
ImageIcon cross = new ImageIcon("Cross.png");
ImageIcon circle = new ImageIcon("Circle.png");
public JButton x1y1 = new JButton("Click me");
public JButton x2y1 = new JButton("Click me");
public JButton x3y1 = new JButton("Click me");
public JButton x1y2 = new JButton("Click me");
public JButton x2y2 = new JButton("Click me");
public JButton x3y2 = new JButton("Click me");
public JButton x1y3 = new JButton("Click me");
public JButton x2y3 = new JButton("Click me");
public JButton x3y3 = new JButton("Click me");
public TTT(){
frame = new JFrame();
frame.setTitle("Let's Play Tic Tac Toe ?? YES!!");
frame.setSize(300 , 300);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
public void init(){
GridLayout layout = new GridLayout(3 , 3);
frame.setLayout(layout);
frame.add(x1y1);
frame.add(x2y1);
frame.add(x3y1);
frame.add(x1y2);
frame.add(x2y2);
frame.add(x3y2);
frame.add(x1y3);
frame.add(x2y3);
frame.add(x3y3);
x1y1.addActionListener(this);
x2y1.addActionListener(this);
x3y1.addActionListener(this);
x1y2.addActionListener(this);
x2y2.addActionListener(this);
x3y2.addActionListener(this);
x1y3.addActionListener(this);
x2y3.addActionListener(this);
x3y3.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == x1y1){
if(turn == 0) {x1y1.setIcon(circle);
turn++ ;
System.out.println(turn);
}
}
}
public static void main(String[] args)
{
TTT main = new TTT();
}
}
Use
ImageIcon circle = new ImageIcon(getClass().getResource("Circle.png"));
instead of
ImageIcon circle = new ImageIcon("Circle.png");. If your icon image is not inside of default package than you have to use path to icon like "/res/image/Circle.png"
So I'm writing a program for my AP Comp Sci class and I need some help with parsing ints and such.
How do I take a random integer and turn it into a string that can be used inside an if/else statement?
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.*;
class LibraryGUI {
// variables and objects
JFrame frame, frame2, frame3;
JPanel panel, panel2, panel3;
JLabel label, label2, label3, label4;
JButton button1;
JButton button2, button3, enter2;
JTextField username, logincred;
Random r = new Random();
public LibraryGUI()
{
frame = new JFrame("LexCorp Library");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("Welcome to LexCorp Library");
button1 = new JButton("Login");
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame3 = new JFrame("Find Account");
panel3 = new JPanel();
enter2 = new JButton("Enter");
frame3.setLayout(new GridLayout(1,1));
panel3.setBackground(Color.white);
panel3.setSize(700,700);
label4 = new JLabel("Enter account number");
logincred = new JTextField("\t\t");
logincred.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent j){
String choose = logincred.getText();
}
});
panel3.add(label4);
panel3.add(logincred);
panel3.add(enter2);
frame3.getContentPane().add (panel3);
frame3.pack();
frame3.setVisible(true);
//internalcode
}
});
button2 = new JButton("Make a new Account");
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent f){
//GUI Section
frame2 = new JFrame("Make a new account");
//frame2.setLayout(new GridLayout(4,4));
panel2 = new JPanel();
panel2.setLayout(new GridLayout(4,3));
//panel2.setLayout(new FlowLayout());
label2 = new JLabel("Account's name");
username = new JTextField();
int acc = r.nextInt(1000);
label3 = new JLabel("Your account number is " +acc);
button3 = new JButton("Enter");
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ent){
JOptionPane.showMessageDialog(frame, "Account Created");
frame2.dispose();
}
});
panel2.add(label2);
panel2.add(username);
panel2.add(button3);
panel2.add(label3);
frame2.pack();
username.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String user = username.getText();
System.out.println(user);
}
});
panel2.setSize(700,700);
panel2.setBackground(Color.LIGHT_GRAY);
frame2.getContentPane().add (panel2);
frame2.pack();
frame2.setVisible(true);
//internalCode
}
});
//panel properties
panel = new JPanel();
panel.setSize(700,700);
panel.setLayout(new GridLayout(10,10));
panel.setBackground(Color.yellow);
//add-ins
panel.add(label);
panel.add(button1);
panel.add(button2);
frame.getContentPane().add (panel);
}
public void display()
{
frame.pack();
frame.setVisible(true);
}
}
(take an..) integer and turn it into a string that can be used inside an IF ElSE
How about instead don't convert it? The conditions of an if statement can work with integers, and do it more easily than strings!
E.G.
int x = random.nextInt(100);
if (x < 50) {
//..
if an int primitive type goes with a string, then the result will be a string.
for example,
int b=3;
String bs = b+"";
then the bs will be "3"
If you are interested in programming, I will suggest you to read "Think in Java." Reading it helps me a lot.
Please use:
Integer.toString(int)
and not:
int + ""
It is the more elegant way and also easier to process by the JVM. It is not a complex operation either way but is the correct way to do it.