Hi,
I wrote the following GUI. Its total crap but just need to get the basics working first. Its compiles fine and my Hello World test message comes up but no sign of the UI itself. Any ideas at all ?
Much Thanks,
Sean

import javax.swing.JFrame;
import java.awt.event.WindowEvent;
import java.awt.Label;
import java.awt.Button;
import java.awt.Font;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;


public class TaxiGUI extends JFrame {
/** Creates new form JFrame */
public TaxiGUI() {
initGUI();
pack();
}



public static void main(String arg[])
{
TaxiGUI taxi = new TaxiGUI( ); //new
System.out.println("Hello World!");//TEST


}


/** This method is called from within the constructor to initialize the form. */
private void initGUI() {
label1.setText("ONLINE TAXI SERVICE");
label1.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 24));
label1.setBounds(new java.awt.Rectangle(2, 1, 466, 53));
label1.setForeground(new java.awt.Color(0, 0, 255));
panel1.setLayout(null);
panel1.add(label1);
panel1.add(label2);
panel1.add(textField1);
panel1.add(label3);
panel1.add(label4);
panel1.add(textField2);
panel1.add(button1);
label2.setText("User Name : ");
label2.setBounds(new java.awt.Rectangle(34, 127, 121, 22));
label2.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 20));
label2.setForeground(new java.awt.Color(0, 0, 255));
textField1.setText("");
textField1.setBounds(new java.awt.Rectangle(171, 131, 173, 20));
textField1.setFont(new java.awt.Font("Dialog",java.awt.Font.BOLD,12));
label3.setText("SYSTEM LOGIN");
label3.setBounds(new java.awt.Rectangle(135, 72, 160, 35));
label3.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 18));
label3.setForeground(new java.awt.Color(255,0,0));
label4.setText("Password :");
label4.setBounds(new java.awt.Rectangle(35, 174, 124, 27));
label4.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD,20));
label4.setForeground(new java.awt.Color(0,51,255));
addWindowListener(
new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().add(panel1, java.awt.BorderLayout.CENTER);
textField2.setText("");
textField2.setBounds(new java.awt.Rectangle(170, 180, 176, 22));
textField2.setFont(new java.awt.Font("Dialog",java.awt.Font.BOLD,12));
button1.setLabel("- LOGIN -");
button1.setBounds(new java.awt.Rectangle(169, 232, 122, 33));
button1.setFont(new java.awt.Font("Book Antiqua", java.awt.Font.BOLD, 18));
button1.setForeground(new java.awt.Color(255,0,0));
button1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){button1ActionPerformed(e);}});
}


/** Exit the Application */
private void exitForm(WindowEvent evt) {
System.exit(0);
}


public void button1ActionPerformed(ActionEvent e)
{
while(textField1 == null)
{
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
}
//need to check text from testField1/2
//if(textField1 == "John" && textField2 == "john123")
if(1 == 1)
{
//payGUI = new PaymentGUI(); //if user name and password is valid
selectionGUI = new MainGUI(); //if user name and password is valid
}
else
{
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
}
}


private Panel panel1 = new Panel();
private Label label1 = new Label();
private Label label2 = new Label();
private TextField textField1 = new TextField();
private Label label3 = new Label();
private Label label4 = new Label();


private TextField textField2 = new TextField();
private Button button1 = new Button();
// private PaymentGUI payGUI;
private MainGUI selectionGUI;
}

Dani AI

Generated

As pointed out, the immediate reason the window never appeared was that the frame was never made visible. Beyond that, the posted class has several common issues that will cause layout, focus and responsiveness problems. The notes below give concrete, long-term fixes so the GUI behaves correctly across platforms and look‑and‑feels.

Build and show the UI on the Event Dispatch Thread (EDT). Use Swing components consistently (JLabel, JTextField, JPasswordField, JButton, JPanel) instead of mixing AWT and Swing; mixing heavyweight and lightweight components produces painting and z-order bugs. Prefer layout managers to null layouts and setBounds() so the UI adapts to font/LAF changes; call pack() after components are added so preferred sizes are respected. For official guidance see the Swing concurrency tutorial: Swing concurrency tutorial.

Fix input handling and event logic. Replace the busy loop while(textField1 == null) with a simple validation of the field contents; that loop will never help (the field reference is not null) and would block the UI thread. Compare strings with equals, not ==, and use JPasswordField#getPassword() to avoid keeping passwords as immutable Strings. Example validation pattern:

String user = usernameField.getText().trim();
String pass = new String(passwordField.getPassword());

if (user.isEmpty()) {
  JOptionPane.showMessageDialog(this, "Enter a username", "Error",
    JOptionPane.ERROR_MESSAGE);
  return;
}
if ("John".equals(user) && "john123".equals(pass)) {
  // transition to main UI (dispose login frame or use CardLayout)
} else {
  JOptionPane.showMessageDialog(this, "Invalid login", "Error",
    JOptionPane.ERROR_MESSAGE);
}

Small additional tips: prefer setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) for simple apps; switch screens with a CardLayout or modal dialog instead of piling up frames; clear password arrays after use; and avoid creating or blocking long tasks on the EDT. These changes will make the UI reliable and maintainable for future development.

Recommended Answers

All 2 Replies

You simply forgot to set your frame visible.

try:

taxi.setVisible(true);

This isn't related to your question, but you're mixing AWT and Swing visual components. That is a bad idea in general. Please read the following artical for an explanation.

http://java.sun.com/products/jfc/tsc/articles/mixing/

Brilliant. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.