I've got 2 errors:
C:\Users\anderson\Documents\FlopstoGFlopsConverter1.java:80: error: cannot find symbol
long flops = Long.parseLong(this.textField1.getText());
^
symbol: variable textField1
C:\Users\anderson\Documents\FlopstoGFlopsConverter1.java:85: error: cannot find symbol
this.textField2.setText(String.valueOf(gFlops));
^
symbol: variable textField2
2 errors
it's just so difficult to learn.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FlopstoGFlopsConverter1 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
FlopstoGFlopsConverter1() {
setSize(500, 350);
setVisible(true);
JButton button1 = new JButton("Convert!");
button1.addActionListener(this);
JPanel panel = new JPanel();
panel.add(button1);
panel.setLayout(null);
this.add(panel);
button1.setBounds(190, 230, 100, 30);
JLabel label1 = new JLabel("Enter Flops");
panel.add(label1);
this.add(panel);
label1.setBounds(89, 52, 150, 50);
JTextField textArea1 = new JTextField(20);
JPanel p = new JPanel();
panel.add(textArea1);
this.add(panel);
textArea1.setBounds(160, 69, 160, 20);
JTextField textArea2 = new JTextField(20);
panel.add(textArea2);
this.add(panel);
textArea2.setBounds(159, 155, 160, 20);
JLabel label2 = new JLabel("Gigaflops ");
panel.add(label2);
this.add(panel);
label2.setBounds(91, 150, 200, 30);
}
public static void main(String[]args) {
new FlopstoGFlopsConverter1();
}
public void actionPerformed(ActionEvent e) {
System.out.println("");
try {
long flops = Long.parseLong(this.textField1.getText());
double gFlops = flops/1000000000;
this.textField2.setText(String.valueOf(gFlops));
} catch(Exception exception) {
}
}
}
You need to declare textArea1 and textArea2 as instance variables and use those in actionPerformed() method.
Currently, there are no textField1 and textField2 declared, instead textArea1 and textArea2, but even these are in the local scope of the constructor.
JTextField textArea1 = null; // Outside the constructor, inside the class
JTextField textArea2 = null; // Outside the constructor, inside the class
...
...
// Inside actionPerformed method
long flops = Long.parseLong(this.textArea1.getText()); // use textArea1
...
this.textArea2.setText(String.valueOf(gFlops));
As it says you do not have any declaration of textField1 and textField2. Declare them first. If you meant to use textArea1 and textArea2, declare them as instance variables after the class declaration so you can use them. Your class would look like this:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FlopstoGFlopsConverter1 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textArea1;
private JTextField textArea2;
FlopstoGFlopsConverter1() {
setSize(500, 350);
setVisible(true);
JButton button1 = new JButton("Convert!");
button1.addActionListener(this);
JPanel panel = new JPanel();
panel.add(button1);
panel.setLayout(null);
this.add(panel);
button1.setBounds(190, 230, 100, 30);
JLabel label1 = new JLabel("Enter Flops");
panel.add(label1);
this.add(panel);
label1.setBounds(89, 52, 150, 50);
textArea1 = new JTextField(20);
JPanel p = new JPanel();
panel.add(textArea1);
this.add(panel);
textArea1.setBounds(160, 69, 160, 20);
textArea2 = new JTextField(20);
panel.add(textArea2);
this.add(panel);
textArea2.setBounds(159, 155, 160, 20);
JLabel label2 = new JLabel("Gigaflops ");
panel.add(label2);
this.add(panel);
label2.setBounds(91, 150, 200, 30);
}
public static void main(String[]args) {
new FlopstoGFlopsConverter1();
}
public void actionPerformed(ActionEvent e) {
System.out.println("");
try {
long flops = Long.parseLong(this.textArea1.getText());
double gFlops = flops/1000000000;
this.textArea2.setText(String.valueOf(gFlops));
} catch(Exception exception) {
}
}
}
Related
I'm trying to make a banking system GUI and I've done well so far but I've run into quite the roadblock. I want to be able to log in, but only if you've previously created an account, but I'm not sure if it's even possible to run my create account class and save the information received for future use since the program will close. I assume it's not possible as I'd need a place to store the username and password for future use, such as a server or something, but I thought I'd ask to see if maybe there was something I could do with an array or something. (P.S. I know I shouldn't use .setLayout(null) but this is just a fun first solo coding project I'm trying to do and I don't understand what people say about it so I'll learn about it later)
Here is my code so far:
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class CreateAccount{
private JPanel panel;
private JFrame frame;
private JLabel username;
private JLabel title;
private JLabel passwordLabel;
private JLabel confirmPasswordLabel;
private JLabel alreadyLoggedIn;
private JTextField usernameText;
private JPasswordField passwordText;
private JPasswordField confirmPasswordText;
private JButton confirmButton;
private JButton alreadyLoggedInButton;
private JLabel failureLabel;
public CreateAccount()
{
panel = new JPanel();
frame = new JFrame();
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(panel);
panel.setLayout(null);
Clicklistener click = new Clicklistener();
title = new JLabel("Create New Account");
title.setFont(new Font("Serif", Font.BOLD, 18));
title.setBounds(150,10,200, 25);
panel.add(title);
username = new JLabel("New Username:");
username.setBounds(40,50,100,25);;
username.setFont(new Font("Serif", Font.BOLD, 14));
panel.add(username);
passwordLabel = new JLabel("Create Password:");
passwordLabel.setBounds(30,90,150,25);
passwordLabel.setFont(new Font("Serif", Font.BOLD, 14));
panel.add(passwordLabel);
confirmPasswordLabel = new JLabel("Confirm Password:");
confirmPasswordLabel.setBounds(18,130,150,25);
confirmPasswordLabel.setFont(new Font("Serif", Font.BOLD, 14));
panel.add(confirmPasswordLabel);
alreadyLoggedIn = new JLabel("Already logged in?");
alreadyLoggedIn.setBounds(28,225,200,25);
panel.add(alreadyLoggedIn);
usernameText = new JTextField();
usernameText.setBounds(160,53,225,25);
panel.add(usernameText);
passwordText = new JPasswordField();
passwordText.setBounds(160,93,225,25);
panel.add(passwordText);
confirmPasswordText = new JPasswordField();
confirmPasswordText.setBounds(160,133,225,25);
panel.add(confirmPasswordText);
confirmButton = new JButton("Confirm Login");
confirmButton.setBounds(205,190,125,25);
confirmButton.addActionListener(click);
panel.add(confirmButton);
alreadyLoggedInButton = new JButton("Login Here");
alreadyLoggedInButton.setBounds(140,225, 125,25);
alreadyLoggedInButton.addActionListener(click);
panel.add(alreadyLoggedInButton);
failureLabel = new JLabel("");
failureLabel.setBounds(175,160,400,25);
panel.add(failureLabel);
frame.setVisible(true);
}
public String getUsername() {
return usernameText.getText();
}
public String getPassword() {
return passwordText.getText();
}
private class Clicklistener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String password = passwordText.getText();
String confirmedPassword = confirmPasswordText.getText();
if ((password.equals(confirmedPassword)) && (e.getSource() == confirmButton))
{
frame.dispose();
//call Home Page
}
else if(!password.equals(confirmedPassword)) {
failureLabel.setText("Passwords don't match. Try Again!");
}
if (e.getSource() == alreadyLoggedInButton)
{
frame.dispose();
new Login();
}
}
}
}
My Login class code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame;
private JPanel panel;
private JLabel userLabel;
private JTextField userText;
private JLabel passwordLabel;
private JPasswordField passwordText;
private JButton button;
private JLabel success;
public Login(){
panel = new JPanel();
frame = new JFrame();
frame.setSize(350,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
userLabel = new JLabel("Username:");
userLabel.setBounds(10, 20, 80, 25); // x , y , width , height
panel.add(userLabel);
userText = new JTextField();
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
button = new JButton("Login");
button.setBounds(135,80,80,25);
button.addActionListener(new Clicklistener());
panel.add(button);
success = new JLabel("");
success.setBounds(10,110,300,25);
panel.add(success);
success.setText("");
frame.setVisible(true);
}
private class Clicklistener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == button))
{
frame.dispose();
}
}
}
}
I've been making an app with java swing and JavaFX and didn't run into a problem until now. Whenever I set the guis visibility to true, the GUI becomes very small, when it should be 1150 px wide, 900 px tall. Does anyone get any ideas?
Main Gui code:
import javax.swing.*;
import java.awt.*;
public class mainGui extends JFrame{
public static JFrame mainScreen = new JFrame("Tamo");
public mainGui() {
JPanel topPanel = new JPanel();
mainScreen.setSize(1150, 900);
mainScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainScreen.setResizable(false);
mainScreen.setLayout(new BorderLayout());
topPanel.setBackground(new Color(226, 230, 204));
topPanel.setPreferredSize(new Dimension(1150, 100));
mainScreen.add(topPanel, BorderLayout.NORTH);
ImageIcon icon = new ImageIcon("C:\\Users\\uname\\Desktop\\TamoDienynas\\pngs\\tamo.png");
mainScreen.setIconImage(icon.getImage());
mainScreen.setVisible(false);
}
public static void main(String[] args) {
new mainGui();
}
}
Class which make's the gui visible:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static me.rockorbonk.tamodienynas.GUI.mainGui.mainScreen;
public class loginGui extends JFrame implements ActionListener {
private static JFrame frame;
private static JLabel userLabel;
private static JLabel passwordLabel;
private static JLabel success;
private static JButton login;
private static JTextField userText;
private static JPasswordField passwordText;
loginGui() {
frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
panel.setLayout(null);
userLabel = new JLabel("User");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
userText = new JTextField();
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
login = new JButton("Login");
login.setBounds(10, 80, 80, 25);
login.addActionListener(this);
panel.add(login);
success = new JLabel("");
success.setBounds(10, 110, 300, 25);
panel.add(success);
ImageIcon icon = new ImageIcon("C:\\Users\\uname\\Desktop\\TamoDienynas\\pngs\\tamo.png");
frame.setIconImage(icon.getImage());
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae) {
String uname = userText.getText();
String pwd = passwordText.getText();
if(uname.equals("rokasbruz") && pwd.equals("Rokas123")) {
success.setText("Sekmingai prisijungėte!");
ActionListener l = evt -> {
userLabel.setVisible(false);
passwordLabel.setVisible(false);
userText.setVisible(false);
passwordText.setVisible(false);
login.setVisible(false);
success.setVisible(false);
frame.setVisible(false);
mainScreen.setVisible(true);
mainScreen.pack();
};
Timer timer = new Timer(2000, l);
timer.setRepeats(false);
timer.start();
}
else{
success.setText("Slapyvardis arba slaptažodis nesutampa!");
}
}
public static void main(String[] args) {
new loginGui();
}
}
Any help would be appreciated!
P.S. I'm using IntelliJ ultimate as the IDE
-Rock
mainScreen is just a JFrame that does not contain anything. Hence when you make it visible, all you see is the title bar. In order to initialize mainScreen you need to call mainGui constructor. Nonetheless, as Andrew Thompson wrote in his comment, your style of coding a Swing application is very unusual. Consider using a dialog as your login window.
Here is your code that displays mainScreen as I believe you intended. I only changed the ActionListener, in class loginGui, that displays mainGui. I added one line which is indicated with the comment ADDED THIS LINE
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Timer;
import static me.rockorbonk.tamodienynas.GUI.mainGui.mainScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class loginGui extends JFrame implements ActionListener {
private static JFrame frame;
private static JLabel userLabel;
private static JLabel passwordLabel;
private static JLabel success;
private static JButton login;
private static JTextField userText;
private static JPasswordField passwordText;
loginGui() {
frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
panel.setLayout(null);
userLabel = new JLabel("User");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
userText = new JTextField();
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
login = new JButton("Login");
login.setBounds(10, 80, 80, 25);
login.addActionListener(this);
panel.add(login);
success = new JLabel("");
success.setBounds(10, 110, 300, 25);
panel.add(success);
ImageIcon icon = new ImageIcon("C:\\Users\\uname\\Desktop\\TamoDienynas\\pngs\\tamo.png");
frame.setIconImage(icon.getImage());
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae) {
String uname = userText.getText();
String pwd = passwordText.getText();
if (uname.equals("rokasbruz") && pwd.equals("Rokas123")) {
success.setText("Sekmingai prisijungėte!");
ActionListener l = evt -> {
userLabel.setVisible(false);
passwordLabel.setVisible(false);
userText.setVisible(false);
passwordText.setVisible(false);
login.setVisible(false);
success.setVisible(false);
frame.setVisible(false);
new mainGui(); // ADDED THIS LINE
mainScreen.setVisible(true);
mainScreen.pack();
};
Timer timer = new Timer(2000, l);
timer.setRepeats(false);
timer.start();
}
else {
success.setText("Slapyvardis arba slaptažodis nesutampa!");
}
}
public static void main(String[] args) {
new loginGui();
}
}
Here is how it appears (after logging in).
I am currently designing a login screen, but I ran into a strange issue. I already designed my GUI with the help of swing, and it was time to make functional buttons. I wanted to test my login button and if it would take me to the frame I want, but it is unable to. I can set a JOptionPane.showMessageDialog for example, which works just fine, but I am unable to open another frame from the button. I tried with New JFrameName().setVisible(true), and also JFrameName test = new JFrameName(); test.setVisible(true);, but the methods show up in red. Here is my code.
package com.edu4java.swing.tutrial3;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView {
public static void main(String[] args) {
JFrame frame = new JFrame("Bus Tour Booking System");
frame.setSize(300, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel titleLabel = new JLabel("Bus Tour Booking System");
titleLabel.setBounds(70,15,150,25);
panel.add(titleLabel);
JLabel userLabel = new JLabel("Username: ");
userLabel.setBounds(30, 50, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(120, 50, 130, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setBounds(30, 80, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(120, 80, 130, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 125, 80, 25);
panel.add(loginButton);
ActionListener myButtonListener = new MyButtonListener();
loginButton.addActionListener(myButtonListener);
}
private static class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
//can't access a new frame from here :(
}
}
}
I would be very grateful if someone could help me out, I read a lot on Stackoverflow and Reddit, but just can't find the solution. I am also new to Java, so that doesn't help a lot either :D. Thanks in advance!
P.S. As far as the actual functionality for the login screen, I am going to do that in a later stage.
This is your login class. I put the JFrame frame in the global scope, so you can manipulate it from the ButtonListener method. I also created a SomeFrame class, just to demonstrate the new JFrame that would be created when you click the button. When an action is performed(the button is clicked) a new object of SomeFrame is created. Since SomeFrame extends JFrame we can use the method setVisible() to a SomeFrame object. The SomeFrame frame appears and the LoginView is no longer visible.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView {
public static JFrame frame = new JFrame("Bus Tour Booking System");
public static void main(String[] args) {
frame.setSize(300, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel titleLabel = new JLabel("Bus Tour Booking System");
titleLabel.setBounds(70,15,150,25);
panel.add(titleLabel);
JLabel userLabel = new JLabel("Username: ");
userLabel.setBounds(30, 50, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(120, 50, 130, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setBounds(30, 80, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(120, 80, 130, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 125, 80, 25);
panel.add(loginButton);
ActionListener myButtonListener = new MyButtonListener();
loginButton.addActionListener(myButtonListener);
}
private static class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
SomeFrame newFrame = new SomeFrame();
newFrame.setVisible(true);
frame.setVisible(false);
}
}
}
This is the SomeFrame class.
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SomeFrame extends JFrame {
public SomeFrame(){
super("something");
this.setSize(300, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.add(panel);
this.setVisible(true);
}
}
Hi I'm trying to display an ArrayList of Objects on a Jlist. While displaying the ArrayList, I'm trying to be able to add or delete objects to the ArrayList through the GUI.
1) How can I display the ArrayList objects on the Jlist? 2)How can I delete an object by selecting it on the JList?
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
public class DriverFrame extends JFrame implements ItemListener{
private JPanel contentPane;
private JList driverlist;
private ArrayList drivers= new ArrayList<Driver>();
JTextField textFieldFirstName;
JTextField textFieldLastName;
JTextField textFieldTruckNumber;
JTextField textFieldTrailerNumber;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DriverFrame frame = new DriverFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DriverFrame() {
Driver guy1= new Driver("Jeff","Blank","4125","1234",1400);
Driver guy2= new Driver("Marcus","Geralds","0093","1203",1800);
Driver guy3= new Driver("Steve","Wemmings","2010","2046",2100);
Driver guy4= new Driver("Kyle","Patricks","8427","5625",900);
Driver guy5= new Driver("Ficel","Metter","9124","4536",5500);
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
setResizable(false);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBackground(Color.WHITE);
tabbedPane.setBounds(5, 5, 495, 480);
contentPane.add(tabbedPane);
//Drivers TabbedPan ********
JPanel Drivers = new JPanel();
Drivers.setBackground(Color.WHITE);
tabbedPane.addTab("Drivers", null, Drivers, null);
Drivers.setLayout(null);
//List to display drivers
JList<Object> driverlist = new JList<>(drivers.toArray(new String[0]));
driverlist.setBackground(Color.WHITE);
driverlist.setVisibleRowCount(8);
driverlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Drivers.add(driverlist);
driverlist.setBounds(221,6,247,234);
JTextPane txtpnFirstName = new JTextPane();
txtpnFirstName.setText("First name");
txtpnFirstName.setBounds(5, 20, 140, 15);
Drivers.add(txtpnFirstName);
txtpnFirstName.setEditable(false);
textFieldFirstName = new JTextField();
textFieldFirstName.setBounds(5, 40, 140, 30);
Drivers.add(textFieldFirstName);
textFieldFirstName.setColumns(10);
JTextPane txtpnLastName = new JTextPane();
txtpnLastName.setText("Last Name");
txtpnLastName.setBounds(5, 75, 140, 15);
Drivers.add(txtpnLastName);
txtpnLastName.setEditable(false);
textFieldLastName = new JTextField();
textFieldLastName.setBounds(5, 95, 140, 30);
Drivers.add(textFieldLastName);
textFieldLastName.setColumns(10);
JTextPane txtpnTruckNumber = new JTextPane();
txtpnTruckNumber.setText("Truck Number");
txtpnTruckNumber.setBounds(5, 130, 140, 15);
Drivers.add(txtpnTruckNumber);
txtpnTruckNumber.setEditable(false);
textFieldTruckNumber = new JTextField();
textFieldTruckNumber.setBounds(5, 150, 140, 30);
Drivers.add(textFieldTruckNumber);
textFieldTruckNumber.setColumns(10);
JTextPane txtpnTrailerNumber = new JTextPane();
txtpnTrailerNumber.setText("Trailer Number");
txtpnTrailerNumber.setBounds(5, 185, 140, 15);
Drivers.add(txtpnTrailerNumber);
txtpnTrailerNumber.setEditable(false);
textFieldTrailerNumber = new JTextField();
textFieldTrailerNumber.setBounds(5, 205, 140, 30);
Drivers.add(textFieldTrailerNumber);
textFieldTrailerNumber.setColumns(10);
JButton btnAddDriver = new JButton("Add Driver");
btnAddDriver.setBounds(5, 285, 117, 29);
Drivers.add(btnAddDriver);
JButton btnNewButton = new JButton("Delete Driver");
btnNewButton.setBounds(302, 285, 117, 29);
Drivers.add(btnNewButton);
btnAddDriver.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Driver blah = new Driver();
blah.setFirst(textFieldFirstName.getText());
blah.setLast(textFieldLastName.getText());
blah.setTnum(textFieldTruckNumber.getText());
blah.setTrailer(textFieldTrailerNumber.getText());
drivers.add(blah);
System.out.println(blah.printDriver());
System.out.println(drivers.size());
//drivers.remove(example);
}
});
//End of DriversPane******
//New Expenses Tab*****
JPanel NewExpense = new JPanel();
tabbedPane.addTab("New Expense", null, NewExpense, null);
JButton buttontest = new JButton();
NewExpense.add(buttontest);
//End of New Expense Tab****
//Income Tab****
JPanel Income = new JPanel();
Income.setBackground(Color.WHITE);
tabbedPane.addTab("Income", null, Income, null);
Income.setLayout(null);
JComboBox comboBox_3 = new JComboBox();
comboBox_3.setBounds(6, 70, 138, 27);
Income.add(comboBox_3);
JTextPane txtpnDriver_3 = new JTextPane();
txtpnDriver_3.setText("Driver:");
txtpnDriver_3.setBounds(6, 42, 42, 16);
Income.add(txtpnDriver_3);
JTextPane txtpnLoadLocationstate = new JTextPane();
txtpnLoadLocationstate.setText("Load Location(State): ");
txtpnLoadLocationstate.setBounds(6, 161, 138, 16);
Income.add(txtpnLoadLocationstate);
textField_6 = new JTextField();
textField_6.setBounds(6, 205, 130, 26);
Income.add(textField_6);
textField_6.setColumns(10);
JTextPane txtpnLoadAmount = new JTextPane();
txtpnLoadAmount.setText("Load Amount $$:");
txtpnLoadAmount.setBounds(6, 299, 108, 16);
Income.add(txtpnLoadAmount);
textField_7 = new JTextField();
textField_7.setBounds(6, 339, 130, 26);
Income.add(textField_7);
textField_7.setColumns(10);
//End of Income Tab****
//Results Tab***
JPanel Results = new JPanel();
Results.setBackground(Color.WHITE);
tabbedPane.addTab("Result", null, Results, null);
Results.setLayout(null);
JComboBox comboBox_4 = new JComboBox();
comboBox_4.setBounds(6, 69, 140, 27);
Results.add(comboBox_4);
JTextPane txtpnDriver_4 = new JTextPane();
txtpnDriver_4.setText("Driver:");
txtpnDriver_4.setBounds(6, 42, 140, 16);
Results.add(txtpnDriver_4);
JTextArea txtrHowMuchTo = new JTextArea();
txtrHowMuchTo.setText("How much to pay driver. How much are the expenses of the driver");
txtrHowMuchTo.setBounds(6, 124, 234, 304);
Results.add(txtrHowMuchTo);
JTextPane txtpnDriverInformation = new JTextPane();
txtpnDriverInformation.setText("Driver Information");
txtpnDriverInformation.setBounds(16, 108, 140, 16);
Results.add(txtpnDriverInformation);
JTextPane txtpnCompanyInfo = new JTextPane();
txtpnCompanyInfo.setText("Company Info:");
txtpnCompanyInfo.setBounds(414, 80, 178, 16);
Results.add(txtpnCompanyInfo);
JTextArea txtrIncome = new JTextArea();
txtrIncome.setText("Income Expenses");
txtrIncome.setBounds(390, 124, 294, 304);
Results.add(txtrIncome);
//End of Results Tab****
}
#Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
}
}
This is my code
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
public class Test extends JFrame{
private JButton by;
private JButton bn;
JLabel startQuestion;
public Test() {
JPanel p = new JPanel();
by = new JButton("Yes");
bn = new JButton("No");
startQuestion = new JLabel("Would you like to start?");
p.setLayout(null);
by.setBounds(180, 250, 70, 45);
bn.setBounds(250, 250, 70, 45);
startQuestion.setLocation(195, 240);
p.add(by);
p.add(bn);
add(startQuestion);
add(p);
setDefaultCloseOperation(3);
setSize(500, 500);
setVisible(true);
}
public static void main(String...args) {
new Test();
}
}
There are no syntax errors, however my Jlabel is not added (the buttons work fine). I do not want to change the layout from null, as I want to be able to define the positions of the buttons and labels. Does anyone know why this isn't working?
This question is similar to JLabel won't show with JPanel.setLayout(null). Why?
To answer it here, add a line like this:
startQuestion.setBounds(150, 150, 200, 20);
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
public class Test extends JFrame{
private JButton by;
private JButton bn;
JLabel startQuestion;
public Test() {
JPanel p = new JPanel();
by = new JButton("Yes");
bn = new JButton("No");
startQuestion = new JLabel("Would you like to start?");
//p.setLayout(null);
by.setBounds(180, 250, 70, 45);
bn.setBounds(250, 250, 70, 45);
startQuestion.setLocation(195, 240);
p.add(by);
p.add(bn);
add(startQuestion);
add(p);
setDefaultCloseOperation(3);
setSize(500, 500);
setVisible(true);
}
public static void main(String...args) {
new Test();
}
}
Note:
you need to comment setlayout(null);