Text box and Label issues - java

I’ve started to create a GUI that consists of a few tabs. Right now I am focusing on two of them. The Pool tab and the Hot Tub tab. When I first started I got everything to work fine on the pool tab. So I figured since all of the label and text box placement would be the same for the Hot Tub tab I would just copy the coding over. Well, I did that and tried naming all the labels and text boxes the same just with the number 2 after them. It’s not working. Now the Hot Tub tab works, but the Pool tab doesn’t, plus the text boxes are gone. I’m also having alignment issues with the text boxes too, but I think that has to do with the naming of the labels and text boxes, I’m not sure.
MAIN CLASS:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
public class test extends JFrame implements ActionListener{
private JTabbedPane jtabbedPane;
private JPanel General;
private JPanel Pools;
private JPanel HotTub;
JTextField lengthText, widthText, depthText, volumeText;
public test(){
setTitle("Volume Calculator");
setSize(300, 200);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
createGeneral();
createPools();
jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("General", General);
jtabbedPane.addTab("Pool", Pools);
jtabbedPane.addTab("Hot Tub", HotTub);
topPanel.add(jtabbedPane, BorderLayout.CENTER);
}
public void createGeneral(){
General = new JPanel();
General.setLayout( null );
JLabel dateLabel = new JLabel("Today's Date");
dateLabel.setBounds(10, 15, 150, 20);
General.add( dateLabel );
JFormattedTextField date = new JFormattedTextField(
java.util.Calendar.getInstance().getTime());
date.setEditable(false);
date.setBounds(90,15,150,20);
General.add(date);
JButton Close = new JButton("Close");
Close.setBounds(20,50,80,20);
Close.addActionListener(this);
Close.setBackground(Color.white);
General.add(Close);
}
/* CREATE POOL */
public void createPools(){
Pools = new JPanel();
Pools.setLayout( null );
JLabel lengthLabel = new JLabel("Length of pool (ft):");
lengthLabel.setBounds(10, 15, 260, 20);
Pools.add( lengthLabel );
lengthText = new JTextField();
lengthText.setBounds(260, 15, 150, 20);
Pools.add( lengthText );
JLabel widthLabel = new JLabel("Width of pool (ft):");
widthLabel.setBounds(10, 60, 260, 20);
Pools.add( widthLabel );
widthText = new JTextField();
widthText.setBounds(260, 60, 150, 20);
Pools.add( widthText );
JLabel depthLabel = new JLabel("Average Depth of pool (ft):");
depthLabel.setBounds( 10, 100, 260, 20 );
Pools.add( depthLabel );
depthText = new JTextField();
depthText.setBounds(260, 100, 150, 20);
Pools.add( depthText );
JLabel volumeLabel = new JLabel("The pool's volume is:(ft ^3");
volumeLabel.setBounds(10, 200, 260, 20);
Pools.add( volumeLabel );
volumeText = new JTextField();
volumeText.setBounds(260, 200, 150, 20);
volumeText.setEditable(false);
Pools.add(volumeText);
JButton calcVolume = new JButton("Calculate Volume");
calcVolume.setBounds(150,250,150,20);
calcVolume.addActionListener(this);
calcVolume.setBackground(Color.white);
Pools.add(calcVolume);
JButton Close = new JButton("Close");
Close.setBounds(350,250,80,20);
Close.addActionListener(this);
Close.setBackground(Color.white);
Pools.add(Close);
}
public void actionPerformed(ActionEvent event){
JButton button = (JButton)event.getSource();
String buttonLabel = button.getText();
if ("Close".equalsIgnoreCase(buttonLabel)){
Exit_pressed(); return;
}
if ("Calculate Volume".equalsIgnoreCase(buttonLabel)){
Calculate_Volume(); return;
}
if ("Calculate Volume".equalsIgnoreCase(buttonLabel)){
Calculate_Volume(); return;
}
}
private void Exit_pressed(){
System.exit(0);
}
private void Calculate_Volume(){
String lengthString, widthString, depthString;
int length=0;
int width=0;
int depth=0;
lengthString = lengthText.getText();
widthString = widthText.getText();
depthString = depthText.getText();
if (lengthString.length() < 1 || widthString.length() < 1 || depthString.length() < 1 ){
volumeText.setText("Enter All 3 Numbers"); return;
}
length = Integer.parseInt(lengthString);
width = Integer.parseInt(widthString);
depth = Integer.parseInt(depthString);
if (length != 0 || width != 0 || depth != 0 ){
volumeText.setText((length * width * depth) + "");
} else{
volumeText.setText("Enter All 3 Numbers"); return;
}
}
public static void main(String[] args){
JFrame frame = new test();
frame.setSize(500, 350);
frame.setVisible(true);
}
}
HOT TUB CLASS:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public abstract class HotTub extends JFrame implements ActionListener{
private JTabbedPane jtabbedPane;
private Component HotTub;
{
jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("Hot Tub", HotTub);
JPanel HotTub;
JTextField lengthText, widthText, depthText, volumeText;
/* CREATE HOT TUB */
HotTub = new JPanel();
HotTub.setLayout( null );
JLabel lengthLabel = new JLabel("Length of hot tub (ft):");
lengthLabel.setBounds(10, 15, 260, 20);
HotTub.add( lengthLabel );
lengthText = new JTextField();
lengthText.setBounds(260, 15, 150, 20);
HotTub.add( lengthText );
JLabel widthLabel = new JLabel("Width of hot tub (ft):");
widthLabel.setBounds(10, 60, 260, 20);
HotTub.add( widthLabel );
widthText = new JTextField();
widthText.setBounds(260, 60, 150, 20);
HotTub.add( widthText );
JLabel depthLabel = new JLabel("Average Depth of hot tub (ft):");
depthLabel.setBounds( 10, 100, 260, 20 );
HotTub.add( depthLabel );
depthText = new JTextField();
depthText.setBounds(260, 100, 150, 20);
HotTub.add( depthText );
JLabel volumeLabel = new JLabel("The hot tub's volume is:(ft ^3");
volumeLabel.setBounds(10, 200, 260, 20);
HotTub.add( volumeLabel );
volumeText = new JTextField();
volumeText.setBounds(260, 200, 150, 20);
volumeText.setEditable(false);
HotTub.add(volumeText);
JButton calcVolume = new JButton("Calculate Volume");
calcVolume.setBounds(150,250,150,20);
calcVolume.addActionListener((ActionListener) this);
calcVolume.setBackground(Color.white);
HotTub.add(calcVolume);
JButton Close = new JButton("Close");
Close.setBounds(350,250,80,20);
Close.addActionListener((ActionListener) this);
Close.setBackground(Color.white);
HotTub.add(Close);
}
}
Right now both the Pool tab and the Hot Tub tab are the same. No matter what tab I'm on, the same results show up on each tab. Is it a naming issue?

This should not be all in one class.
If your pool and hot tub tabs are so similar that you are copying code. Instead create a new class that extends JPanel and sets up the panel based on some parameters. (Or even just a factory method.) Then add two of these classes to the JTabbedPane, one with parameters for a HotTub and the other for a Pool.
Use LayoutManagers. It will be worth the learning curve and will greatly improve your GUI.

in createHotTub() method:
replace HotTub.add( lengthText ); with HotTub.add( lengthText2 );
replace HotTub.add( widthText ); with HotTub.add( widthText2 );
replace HotTub.add( depthText ); with HotTub.add( depthText2 );
replace HotTub.add( volumeText ); with HotTub.add( volumeText2 );

Related

background image hidding the other components like buttons labels and other, and vicce versa

how to solve hidding of components in this code
code is running without errors
but background image not displayed
how to change code to get the background image
when using validation method, its creating error in validation()
public class TEST{
public TEST() {
String[] strm = {"Jan", "Feb", "Mar", "Apr", "May"};
String[] stry = {"2016", "2017", "2018","2019"};
String[] strf={"NEW Delhi", "Bangalore", "Chennai"};
String[] strt={"Goa","Kashmir","Hyderabad"};
JFrame f = new JFrame("test");
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lfr = new JLabel("FROM");
JLabel lto = new JLabel("TO");
JLabel lda = new JLabel("DATE");
JLabel ld = new JLabel("DAY");
JLabel lm = new JLabel("MONTH");
JLabel y = new JLabel("YEAR");
JComboBox cfr = new JComboBox(strf);
JComboBox cto = new JComboBox(strt);
JComboBox cd = new JComboBox();
JComboBox cm = new JComboBox(strm);
JComboBox cy = new JComboBox(stry);
JButton bs = new JButton("Search");
JPanel p1 = new JPanel(null);
p1.setPreferredSize(new Dimension(500,500));
JLabel jimage = new JLabel();
jimage.setIcon(new ImageIcon("air.jpg"));
for(int i = 1; i <= 31; i++)
cd.addItem(i);
lfr.setBounds(20, 40, 100, 20);
cfr.setBounds(100, 40, 100, 20);
lto.setBounds(20, 100, 25, 20);
cto.setBounds(100, 100, 100, 20);
lda.setBounds(20, 160, 50, 20);
cd.setBounds(100, 160, 50, 20);
cm.setBounds(160, 160, 65, 20);
cy.setBounds(240, 160, 75, 20);
ld.setBounds(100, 190, 50, 20);
lm.setBounds(160, 190, 50, 20);
y.setBounds(240, 190, 50, 20);
bs.setBounds(20, 230, 100, 20);
p1.add(lfr);
p1.add(cfr);
p1.add(lto);
p1.add(cto);
p1.add(lda);
p1.add(cd);
p1.add(cm);
p1.add(cy);
p1.add(ld);
p1.add(lm);
p1.add(y);
p1.add(bs);
p1.add(jimage);
// validate();
f.add(p1);
f.setVisible(true);
}
public static void main(String[] args) {
new TEST();
}
}
The best you can do is something like:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
JLabel label;
JComboBox combo;
JButton button;
JPanel pane;
JFrame frame;
JPanel create1stRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
String options[] = {"New Delhi", "Bangalore", "Chennai"};
label = new JLabel("FROM");
combo = new JComboBox(options);
pane.add(label);
pane.add(combo);
return pane;
}
JPanel create2ndRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
String options[] = {"Goa", "Kashmir", "Hyderabad"};
label = new JLabel("TO");
combo = new JComboBox(options);
pane.add(label);
pane.add(combo);
return pane;
}
JPanel create3rdRow() {
JPanel pane = new JPanel();
JPanel dataPane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
dataPane.setOpaque(false); //forgot to add this line when I took the pic
dataPane.setLayout(new GridLayout(2, 3)); //2 rows, 3 cols, so we can have the combos with their text aligned
String days[] = {"1", "2", "3", "4", "5"}; //Too lazy to write 31 days
String months[] = {"Jan", "Feb", "Mar", "Apr", "May"}; //Too lazy to write 12 months
String years[] = {"2016", "2017", "2018", "2019", "2020"};
label = new JLabel("DATE");
combo = new JComboBox(days);
//We add the combos
dataPane.add(combo);
combo = new JComboBox(months); //We're reusing the object, but change the data
dataPane.add(combo);
combo = new JComboBox(years); //The same as above
dataPane.add(combo);
//Now we add the labels
dataPane.add(new JLabel("DAYS"));
dataPane.add(new JLabel("MONTHS"));
dataPane.add(new JLabel("YEARS"));
pane.add(label);
pane.add(dataPane); //We add the whole pane to another one
return pane;
}
JPanel create4thRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
button = new JButton("Search");
pane.add(button);
return pane;
}
public Test() {
frame = new JFrame("Test");
frame.setContentPane(new JLabel(new ImageIcon("C:/Users/Frakcool/workspace/StackOverflowProjects/src/test/Air.jpg")));
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
pane = create1stRow();
frame.add(pane);
pane = create2ndRow();
frame.add(pane);
pane = create3rdRow();
frame.add(pane);
pane = create4thRow();
frame.add(pane);
frame.pack();
//frame.setSize(500, 500); //If your image is too large use this
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[]) {
new Test();
}
}
As you can see in the above code, I'm not using a null layout but a combination of multiple Layout Managers and I suggest you to do it like this in the future.
But if you still want to use that ugly null layout, you were missing this line:
jimage.setBounds(0, 0, 500, 500);
before this one:
lfr.setBounds(20, 40, 100, 20);
The output that my above code gives is:
And the output given by your code with the line I added is:
As you can see, both are really similar, and I could have done them identical but I don't have enough time to do so, but you can by combining the Layout Managers I posted above.
Note: I forgot to mention that to make this program to show the background image, I needed to make every other panels not opaque with pane.setOpaque(false); so, be sure to use this whenever you need to show something that is behind another panel.

Cannot get JLabel to display JTextField Input

I have my ProfileInput class to store a JTextField input from a Dialog box. Then I transfer that to the setter and getter methods. From there I am calling the setter and getter methods in my AppFrame class.
The the problem that I am having is when I want the input to be displayed as a JLabel on the GUI nothing is showing up. I have no errors that are displayed when I run the code either. Any ideas as to what I have done wrong.
Please note that I am new to Java and am trying to learn. Any ideas/help to improve anything is also great.
ProfileInput Class
package GUI;
//Library imports
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ProfileInput extends Dialog {
//array for the active drop down box
String[] activeLabels = {"Select One", "Not Active", "Slightly Active", "Active", "Very Active"};
public String firstNameString;
//intilizing aspects used in the user profile dialog box
JPanel Panel = new JPanel();
JButton saveButton = new JButton("Save");
JLabel firstName = new JLabel("First Name: ");
JLabel lastName = new JLabel("Last Name: ");
JLabel age = new JLabel("Age: ");
JLabel weight = new JLabel("Weight: ");
JLabel height = new JLabel("Height: ");
JLabel weightGoal = new JLabel("Weight Goal: ");
JLabel activeLevel = new JLabel("Active Level: ");
JLabel completion = new JLabel("Completion By: ");
JTextField firstNameInput = new JTextField();
JTextField lastNameInput = new JTextField();
JTextField ageInput = new JTextField();
JTextField weightInput = new JTextField();
JTextField heightInputFeet = new JTextField();
JTextField heightInputInches = new JTextField();
JTextField weightGoalInput = new JTextField();
JComboBox activeCombo = new JComboBox(activeLabels);
JTextField completionInput = new JTextField();
//setup of the dialog panel
public ProfileInput(Frame parent) {
super(parent,true);
userProfileInput();
setSize(315, 380);
setTitle("Profile Creator");
setLocationRelativeTo(null);
}
public void userProfileInput() {
//sets up the main panel for the dialog box (only panel to add to)
Panel.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
Panel.setLayout(null);
//sets the location of the aspects inside the panel
firstName.setBounds(35, 15, 150, 20);
lastName.setBounds(35, 50, 150, 20);
firstNameInput.setBounds(115, 15, 150, 20);
lastNameInput.setBounds(115, 50, 150, 20);
age.setBounds(35, 85, 120, 20);
ageInput.setBounds(115, 85, 150, 20);
weight.setBounds(35, 115, 150, 20 );
weightInput.setBounds(115, 115, 150, 20);
height.setBounds(35, 150, 150, 20);
heightInputFeet.setBounds(115, 150, 72, 20);
heightInputInches.setBounds(193, 150, 72, 20);
weightGoal.setBounds(35, 185, 150, 20);
weightGoalInput.setBounds(115, 185, 150, 20);
activeLevel.setBounds(35, 220, 150, 20);
activeCombo.setBounds(115,220, 150, 20);
completion.setBounds(35, 255, 150, 20);
completionInput.setBounds(130, 255, 120, 20);
saveButton.setBounds(135, 310, 65, 20);
saveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//converts the inputs to a string
firstNameString = firstNameInput.getText();
System.out.println(firstNameString);
}
});
//adds the items to the main panel on the dialog box
Panel.add(firstName, null);
Panel.add(lastName, null);
Panel.add(firstNameInput, null);
Panel.add(lastNameInput, null);
Panel.add(age, null);
Panel.add(ageInput, null);
Panel.add(weight, null);
Panel.add(weightInput, null);
Panel.add(height, null);
Panel.add(heightInputFeet, null);
Panel.add(heightInputInches, null);
Panel.add(weightGoal, null);
Panel.add(weightGoalInput, null);
Panel.add(activeLevel, null);
Panel.add(activeCombo, null);
Panel.add(completion, null);
Panel.add(completionInput, null);
Panel.add(saveButton, null);
//adds the panel to the dialog frame
add(Panel);
}//end of userProfileInput method
public String getFirstName() {
return this.firstNameString;
}
public void setFirstName(String firstNameString) {
this.firstNameString = firstNameString;
}
}
AppFrame Class
public class AppFrame extends JFrame {
private static final long serialVersionUID = 1L;
ProfileInput profileInput = new ProfileInput(null);
String firstNameTest = profileInput.getFirstName();
/**
* Starts the frame from AppFrame method below.
*
* #param args
*/
public static void main(String[] args) {
new AppFrame().setVisible(true);
}//end of main Method
/**
*
*
*/
private AppFrame() {
//Initialization of panels and bars used in the main app
JMenuBar menuBar = new JMenuBar();
JPanel contentPane = new JPanel(new BorderLayout());
JPanel rightPanel = new JPanel();
JPanel profileInfo = new JPanel();
//aspects used in the left toolbar panel
JToolBar toolBarPanel = new JToolBar();
JButton bloodPressureTool = new JButton();
JButton heartRateTool = new JButton();
JButton weightTool = new JButton();
JButton bmiTool = new JButton();
JButton medicationTool = new JButton();
JButton appointmentTool = new JButton();
JButton noteTool = new JButton();
JButton profileTool = new JButton();
Border etched = BorderFactory.createEtchedBorder();
Icon bloodPIcon = new ImageIcon("/Users/BrandonGrow/git/Health-Application/src/Icons/BloodPressure.png");
Icon heartRateIcon = new ImageIcon("/Users/BrandonGrow/git/Health-Application/src/Icons/HeartRate.png");
Icon weightIcon = new ImageIcon("/Users/BrandonGrow/git/Health-Application/src/Icons/Weight.png");
Icon bmiIcon = new ImageIcon("/Users/BrandonGrow/git/Health-Application/src/Icons/BMI.png");
Icon medicationIcon = new ImageIcon("/Users/BrandonGrow/git/Health-Application/src/Icons/Medications.png");
Icon appointmentIcon = new ImageIcon("/Users/BrandonGrow/git/Health-Application/src/Icons/DoctorAppointment.png");
Icon noteIcon = new ImageIcon("/Users/BrandonGrow/git/Health-Application/src/Icons/Notes.png");
Icon profileIcon = new ImageIcon("/Users/BrandonGrow/git/Health-Application/src/Icons/Profile.png");
//aspects of the user profile panel
JLabel firstName = new JLabel("First Name: ");
JLabel lastName = new JLabel("Last Name: ");
JLabel height = new JLabel("Height: ");
JLabel weight = new JLabel("Weight: ");
JLabel age = new JLabel("Age: ");
JLabel weightGoal = new JLabel("Weight Goal: ");
JLabel activeLevel = new JLabel("Active Level: ");
JLabel completion = new JLabel("Completion Date: ");
//Menu Bar Headers
JMenu file = new JMenu("File");
JMenu go = new JMenu("Go");
JMenu help = new JMenu("Help");
//file drop down
JMenuItem newEntry = new JMenuItem("Profile Creator");
JMenuItem exportReport = new JMenuItem("Export Report");
JMenuItem exportNotes = new JMenuItem("Export Notes");
JMenuItem preferences = new JMenuItem("Preferences");
JMenuItem exit = new JMenuItem("Exit");
file.add(newEntry);
file.addSeparator();
file.add(exportReport);
file.addSeparator();
file.add(exportNotes);
file.addSeparator();
file.add(preferences);
file.addSeparator();
file.add(exit);
//action used when the user presses the enter profile input button
newEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
profileInput.setVisible(true);
}
});
//allows for the program to exit when exit is clicked
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
exitDialog();
}
});
//go drop down
JMenuItem bloodPressure = new JMenuItem("Blood Pressure");
JMenuItem heartRate = new JMenuItem("Heart Rate");
JMenuItem medication = new JMenuItem("Medication");
JMenuItem weightDisplay = new JMenuItem("Weight");
JMenuItem bmi = new JMenuItem("BMI");
JMenuItem docAppoints = new JMenuItem("Doctor's Appointments");
JMenuItem notes = new JMenuItem("Notes");
JMenuItem resources = new JMenuItem("Profile");
go.add(bloodPressure);
go.addSeparator();
go.add(heartRate);
go.addSeparator();
go.add(medication);
go.addSeparator();
go.add(weight);
go.addSeparator();
go.add(bmi);
go.addSeparator();
go.add(docAppoints);
go.addSeparator();
go.add(notes);
go.addSeparator();
go.add(resources);
//help drop down
JMenuItem usersGuide = new JMenuItem("Users Guide");
JMenuItem about = new JMenuItem("About Personal Health Application");
help.add(usersGuide);
help.addSeparator();
help.add(about);
//adds Items to Frame
menuBar.add(file);
menuBar.add(go);
menuBar.add(help);
setJMenuBar(menuBar);
//Panel that allows for all GUI to be ad added here
contentPane.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
contentPane.setBackground(Color.WHITE);
contentPane.add(toolBarPanel, BorderLayout.WEST);
contentPane.add(rightPanel);
//stores the buttons for application (left)
toolBarPanel.setOrientation(JToolBar.VERTICAL);
toolBarPanel.setBackground(Color.white);
toolBarPanel.setFloatable(false);;
toolBarPanel.setBorder(etched);
//sets the large panel on the right side of the frame.
rightPanel.setBackground(Color.WHITE);
rightPanel.setBorder(etched);
rightPanel.setLayout(null);
rightPanel.add(profileInfo, null);
//adds the user profile info to the main screen
profileInfo.setBounds(0, 0, 1104, 100);
profileInfo.setBackground(Color.WHITE);
profileInfo.setLayout(null);
profileInfo.setBorder(etched);
firstName.setBounds(80, 10, 80, 20);
firstName.setFont(new java.awt.Font("Dialog", 1, 11));
lastName.setBounds(80, 50, 80, 20);
lastName.setFont(new java.awt.Font("Dialog", 1, 11));
weightDisplay.setBounds(310, 10, 80, 20);
weightDisplay.setFont(new java.awt.Font("Dialog", 1, 11));
height.setBounds(330, 50, 80, 20);
height.setFont(new java.awt.Font("Dialog", 1, 11));
age.setBounds(550, 10, 80, 20);
age.setFont(new java.awt.Font("Dialog", 1, 11));
weightGoal.setBounds(550, 50, 80, 20);
weightGoal.setFont(new java.awt.Font("Dialog", 1, 11));
activeLevel.setBounds(780, 10, 80, 20);
activeLevel.setFont(new java.awt.Font("Dialog", 1, 11));
completion.setBounds(780, 50, 120, 20);
completion.setFont(new java.awt.Font("Dialog", 1, 11));
//test to see if first name displays
JLabel firstNameInputTest = new JLabel(firstNameTest);
firstNameInputTest.setBounds(160, 10, 80, 20);
profileInfo.add(firstName);
profileInfo.add(lastName);
profileInfo.add(weightDisplay);
profileInfo.add(height);
profileInfo.add(age);
profileInfo.add(weightGoal);
profileInfo.add(completion);
profileInfo.add(activeLevel);
//part of test to see of first name displays
profileInfo.add(firstNameInputTest);
//blood pressure button
bloodPressureTool.setMaximumSize(new Dimension(90, 80));
bloodPressureTool.setMinimumSize(new Dimension(30, 30));
bloodPressureTool.setFont(new java.awt.Font("Dialog", 1, 10));
bloodPressureTool.setPreferredSize(new Dimension(90, 50));
bloodPressureTool.setBorderPainted(false);
bloodPressureTool.setContentAreaFilled(false);
bloodPressureTool.setVerticalTextPosition(SwingConstants.BOTTOM);
bloodPressureTool.setHorizontalTextPosition(SwingConstants.CENTER);
bloodPressureTool.setText("Blood Pressure");
bloodPressureTool.setOpaque(false);
bloodPressureTool.setMargin(new Insets(0, 0, 0, 0));
bloodPressureTool.setSelected(true);
bloodPressureTool.setIcon(bloodPIcon);
//heart rate button
heartRateTool.setMaximumSize(new Dimension(90, 80));
heartRateTool.setMinimumSize(new Dimension(30, 30));
heartRateTool.setFont(new java.awt.Font("Dialog", 1, 10));
heartRateTool.setPreferredSize(new Dimension(90, 50));
heartRateTool.setBorderPainted(false);
heartRateTool.setContentAreaFilled(false);
heartRateTool.setVerticalTextPosition(SwingConstants.BOTTOM);
heartRateTool.setHorizontalTextPosition(SwingConstants.CENTER);
heartRateTool.setText("Heart Rate");
heartRateTool.setOpaque(false);
heartRateTool.setMargin(new Insets(0, 0, 0, 0));
heartRateTool.setSelected(true);
heartRateTool.setIcon(heartRateIcon);
//weight button
weightTool.setMaximumSize(new Dimension(90, 80));
weightTool.setMinimumSize(new Dimension(30, 30));
weightTool.setFont(new java.awt.Font("Dialog", 1, 10));
weightTool.setPreferredSize(new Dimension(90, 50));
weightTool.setBorderPainted(false);
weightTool.setContentAreaFilled(false);
weightTool.setVerticalTextPosition(SwingConstants.BOTTOM);
weightTool.setHorizontalTextPosition(SwingConstants.CENTER);
weightTool.setText("Weight");
weightTool.setOpaque(false);
weightTool.setMargin(new Insets(0, 0, 0, 0));
weightTool.setSelected(true);
weightTool.setIcon(weightIcon);
//BMI button
bmiTool.setMaximumSize(new Dimension(90, 80));
bmiTool.setMinimumSize(new Dimension(30, 30));
bmiTool.setFont(new java.awt.Font("Dialog", 1, 10));
bmiTool.setPreferredSize(new Dimension(90, 50));
bmiTool.setBorderPainted(false);
bmiTool.setContentAreaFilled(false);
bmiTool.setVerticalTextPosition(SwingConstants.BOTTOM);
bmiTool.setHorizontalTextPosition(SwingConstants.CENTER);
bmiTool.setText("BMI");
bmiTool.setOpaque(false);
bmiTool.setMargin(new Insets(0, 0, 0, 0));
bmiTool.setSelected(true);
bmiTool.setIcon(bmiIcon);
//medication button
medicationTool.setMaximumSize(new Dimension(90, 80));
medicationTool.setMinimumSize(new Dimension(30, 30));
medicationTool.setFont(new java.awt.Font("Dialog", 1, 10));
medicationTool.setPreferredSize(new Dimension(90, 50));
medicationTool.setBorderPainted(false);
medicationTool.setContentAreaFilled(false);
medicationTool.setVerticalTextPosition(SwingConstants.BOTTOM);
medicationTool.setHorizontalTextPosition(SwingConstants.CENTER);
medicationTool.setText("Medication");
medicationTool.setOpaque(false);
medicationTool.setMargin(new Insets(0, 0, 0, 0));
medicationTool.setSelected(true);
medicationTool.setIcon(medicationIcon);
//appointment button
appointmentTool.setMaximumSize(new Dimension(90, 80));
appointmentTool.setMinimumSize(new Dimension(30, 30));
appointmentTool.setFont(new java.awt.Font("Dialog", 1, 10));
appointmentTool.setPreferredSize(new Dimension(90, 50));
appointmentTool.setBorderPainted(false);
appointmentTool.setContentAreaFilled(false);
appointmentTool.setVerticalTextPosition(SwingConstants.BOTTOM);
appointmentTool.setHorizontalTextPosition(SwingConstants.CENTER);
appointmentTool.setText("Appointments");
appointmentTool.setOpaque(false);
appointmentTool.setMargin(new Insets(0, 0, 0, 0));
appointmentTool.setSelected(true);
appointmentTool.setIcon(appointmentIcon);
//note button
noteTool.setMaximumSize(new Dimension(90, 80));
noteTool.setMinimumSize(new Dimension(30, 30));
noteTool.setFont(new java.awt.Font("Dialog", 1, 10));
noteTool.setPreferredSize(new Dimension(90, 50));
noteTool.setBorderPainted(false);
noteTool.setContentAreaFilled(false);
noteTool.setVerticalTextPosition(SwingConstants.BOTTOM);
noteTool.setHorizontalTextPosition(SwingConstants.CENTER);
noteTool.setText("Notes");
noteTool.setOpaque(false);
noteTool.setMargin(new Insets(0, 0, 0, 0));
noteTool.setSelected(true);
noteTool.setIcon(noteIcon);
//profile button
profileTool.setMaximumSize(new Dimension(90, 80));
profileTool.setMinimumSize(new Dimension(30, 30));
profileTool.setFont(new java.awt.Font("Dialog", 1, 10));
profileTool.setPreferredSize(new Dimension(90, 50));
profileTool.setBorderPainted(false);
profileTool.setContentAreaFilled(false);
profileTool.setVerticalTextPosition(SwingConstants.BOTTOM);
profileTool.setHorizontalTextPosition(SwingConstants.CENTER);
profileTool.setText("Profile");
profileTool.setOpaque(false);
profileTool.setMargin(new Insets(0, 0, 0, 0));
profileTool.setSelected(true);
profileTool.setIcon(profileIcon);
//adding buttons to toolBarPanel
toolBarPanel.add(bloodPressureTool);
toolBarPanel.add(heartRateTool);
toolBarPanel.add(weightTool);
toolBarPanel.add(bmiTool);
toolBarPanel.add(medicationTool);
toolBarPanel.add(appointmentTool);
toolBarPanel.add(noteTool);
toolBarPanel.add(profileTool);
//sets up the actual frame
setSize(1200,800);
setResizable(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
add(contentPane);
//allows for the program to shut down by using x and then using the dialog
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
exitDialog();
}
});
}//end of appFrame Method
You've got several problems with the above code, but most important, you're using a modeless dialog when you absolutely need to use a modal one. Since it is modeless, program flow in the calling code does not halt when the dialog is made visible, and so you're calling getFirstName() on the dialog immediately after it is opened, before it has been closed, and well before the user has had a chance to input any information whatsoever. A modal dialog on the other hand will freeze program flow in the calling code, and program flow will not resume until the dialog is no longer visible.
Problems and suggestions:
First and foremost, make sure the dialog window is a modal dialog.
But even before this, don't use Dialog, Panel and other AWT component classes, but rather use Swing classes -- JDialog, JPanel, etc.
You can set the JDialog to be modal with either the proper constructor, passing in ModalityType.APPLICATION_MODAL as a parameter within the appropriate constructor (see the API), or you can set it via a method.
Either way, make sure that it's set before setting the dialog visible.
Do this, and when you query the state of the dialog, you can be assured that the user has at least had a chance to interact with the dialog before you try to extract information from it.
Be sure to query the dialog and assign the results after setting it visible.
Edit, I see now that you're calling String firstNameTest = profileInput.getFirstName(); even before setting the dialog visible, as if the firstNameTest String, which is obviously null at this stage, will magically update once the dialog has been visualized and dealt with, but sorry, there's no magic in Java, and fields will not update by themselves. Again, do not set the firstNameTest field at that point, but rather only after the dialog has been displayed and then dealt with.
Next we'll need to talk about null layouts and setBounds. You really don't want to go this route, trust me.
For example:
public class AppFrame extends JFrame {
private static final long serialVersionUID = 1L;
// !! the JLabel needs to be a field so it can be set in the ActionListener
private JLabel firstNameInputTest = new JLabel("");
private ProfileInput profileInput = null; //!! let this start out as null
// !! worthless code, get rid of
// String firstNameTest = profileInput.getFirstName();
public static void main(String[] args) {
// .... etc
And the ActionListener where we create/display the dialog:
newEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//!! create JDialog in a lazy fashion
if (profileInput == null) {
// create dialog, passing in the JFrame
profileInput = new ProfileInput(AppFrame.this);
}
profileInput.setVisible(true); // display the *modal* dialog
// program flow is frozen here until JDialog is no longer visible
// query dialog for its contents
String firstNameTxt = profileInput.getFirstName();
// and use in GUI
firstNameInputTest.setText(firstNameTxt);
}
});
We don't want to declare the JLabel within a method or constructor since in doing so, it will not be visible throughout the class. So...
private AppFrame() { // ??? private ???
// .....
// test to see if first name displays
// !! JLabel firstNameInputTest = new JLabel(firstNameTest); // No!!!
Finally, a very simple example JDialog to demonstrate what I'm discussing:
#SuppressWarnings("serial")
public class ProfileInput extends JDialog {
private JTextField firstNameField = new JTextField(10);
public ProfileInput(JFrame frame) {
// make it modal!
super(frame, "Profile Input", ModalityType.APPLICATION_MODAL);
JPanel panel = new JPanel();
panel.add(new JLabel("Enter First Name:"));
panel.add(firstNameField);
panel.add(new JButton(new SubmitAction("Submit", KeyEvent.VK_S)));
add(panel);
pack();
setLocationRelativeTo(frame);
}
public String getFirstName() {
return firstNameField.getText();
}
private class SubmitAction extends AbstractAction {
public SubmitAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
ProfileInput.this.dispose();
}
}
}

Position glCanvas alongside a JPanel containing buttons

I'd like to have the JButton panel consume ~30% of the frames horizontal space.
And glCanvas on the right side taking up the rest of the frames space.
How can I achieve this layout?
Currently:
Main.java
GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);
GLCanvas glcanvas = new GLCanvas(capabilities);
glcanvas.addGLEventListener(new GameRenderer());
glcanvas.setSize(100, 100);
JFrame frame = new JFrame("Tool");
JPanel panel = new JPanel();
JPanel cpanel=new JPanel();
panel.setLayout(null);
cpanel.setLayout(null);
JButton ButtonBR = new JButton("1");
JButton ButtonE = new JButton("2");
JButton ButtonR = new JButton("3");
JButton ButtonC = new JButton("4");
JButton ButtonT = new JButton("5");
JButton ButtonCR = new JButton("6");
ButtonBR.setBounds(10, 30, 150, 40);
ButtonE.setBounds(10, 80, 150, 40);
ButtonR.setBounds(10, 130, 150, 40);
ButtonC.setBounds(10, 180, 150, 40);
ButtonT.setBounds(10, 230, 150, 40);
ButtonCR.setBounds(10, 450, 150, 40);
cpanel.add(glcanvas);
panel.add(ButtonBR);
panel.add(ButtonE);
panel.add(ButtonR);
panel.add(ButtonC);
panel.add(ButtonT);
panel.add(ButtonCR);
frame.add(cpanel);
frame.add(panel);
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
frame.setVisible(true);
Animator animator = new Animator(glcanvas));
animator.start();
Consider using a MigLayout as such:
import javax.swing.JFrame;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public Main() {
getContentPane().setLayout(new MigLayout("", "[grow 30][grow 70]", "[]"));
JLabel label = new JLabel("30%");
getContentPane().add(label, "cell 0 0");
JTextField textField = new JTextField("70%");
getContentPane().add(textField, "cell 1 0,growx");
pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}

Repaint only a specific JPanel inside a JFrame

I am trying to create a paint component for the panel_1 JPanel, but I don't know how to do it. This is my class.
This code works, except that it does not paint the panel_1.
If I try and add it to the panel without painting it, it works just fine.
Also I need that panel to refresh (repaints) automatically at a given interval.
Please let help me. :D
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class GUI extends JFrame implements ActionListener {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JButton btnStop, btnStart;
private JComboBox comboBox_1, comboBox;
private Manager manager = new Manager();
private String arrMin, arrMax, svcMin, svcMax, simTime;
private JTextField textField_4;
private JPanel panel_1 = new JPanel(){
#Override
public void paintComponent(Graphics g){
paintComponents(g);
panel_1.setBackground(Color.orange);
panel_1.setVisible(true);
System.out.println("Just testing");
}
};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI() {
setResizable(false);
setForeground(Color.DARK_GRAY);
setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 500);
contentPane = new JPanel();
contentPane.setForeground(Color.DARK_GRAY);
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(12, 12, 434, 346);
panel.setForeground(Color.LIGHT_GRAY);
contentPane.add(panel);
panel.setLayout(null);
JLabel lblArrivingTime = new JLabel("Arriving time");
lblArrivingTime.setBounds(12, 367, 94, 15);
lblArrivingTime.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblArrivingTime);
JLabel lblMin = new JLabel("Min");
lblMin.setBounds(12, 395, 70, 15);
lblMin.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMin);
JLabel lblMax = new JLabel("Max");
lblMax.setBounds(12, 418, 70, 15);
lblMax.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMax);
textField = new JTextField();
textField.setBounds(58, 394, 48, 16);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(58, 422, 48, 15);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblServiceTime = new JLabel("Service Time");
lblServiceTime.setBounds(152, 367, 105, 15);
lblServiceTime.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblServiceTime);
JLabel lblMin_1 = new JLabel("Min");
lblMin_1.setBounds(152, 395, 51, 15);
lblMin_1.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMin_1);
JLabel lblMax_1 = new JLabel("Max");
lblMax_1.setBounds(152, 418, 70, 15);
lblMax_1.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMax_1);
textField_2 = new JTextField();
textField_2.setBounds(209, 393, 48, 17);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(209, 417, 48, 17);
contentPane.add(textField_3);
textField_3.setColumns(10);
JLabel lblSimSetup = new JLabel("Sim. Setup");
lblSimSetup.setBounds(292, 367, 94, 15);
lblSimSetup.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblSimSetup);
JLabel lblQueue = new JLabel("Queue");
lblQueue.setBounds(292, 395, 70, 15);
lblQueue.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblQueue);
String[] numbers = { "1", "2", "3", "4" };
comboBox = new JComboBox(numbers);
comboBox.setBounds(345, 395, 75, 15);
contentPane.add(comboBox);
JLabel lblMode = new JLabel("Mode");
lblMode.setBounds(292, 418, 70, 15);
lblMode.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMode);
String[] mode = { "Time", "Number" };
comboBox_1 = new JComboBox(mode);
comboBox_1.setBounds(345, 413, 75, 15);
contentPane.add(comboBox_1);
JLabel lblActions = new JLabel("Actions");
lblActions.setBounds(484, 367, 70, 15);
lblActions.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblActions);
btnStart = new JButton("Start");
btnStart.setBounds(482, 392, 81, 20);
contentPane.add(btnStart);
btnStop = new JButton("Stop");
btnStop.setBounds(482, 415, 81, 20);
contentPane.add(btnStop);
JLabel lblTime = new JLabel("Time");
lblTime.setBounds(292, 440, 70, 15);
lblTime.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblTime);
textField_4 = new JTextField();
textField_4.setBounds(345, 438, 75, 17);
contentPane.add(textField_4);
textField_4.setColumns(10);
panel_1.setBounds(484, 12, 202, 346);
contentPane.add(panel_1);
panel_1.setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(158, 12, -126, 322);
panel_1.add(textArea);
btnStop.addActionListener(this);
btnStart.addActionListener(this);
comboBox.addActionListener(this);
comboBox_1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnStop) {
this.dispose();
System.exit(NORMAL);
} else if (e.getSource() == btnStart) {
String nrQueues = (String) comboBox.getSelectedItem();
arrMin = textField.getText();
arrMax = textField_1.getText();
svcMin = textField_2.getText();
svcMax = textField_3.getText();
simTime = textField_4.getText();
int aMin = 0;
int aMax = 0;
int sMin = 0;
int sMax = 0;
int sTime = 0;
int nQueues = 1;
try {
nQueues = Integer.parseInt(nrQueues);
aMin = Integer.parseInt(arrMin);
aMax = Integer.parseInt(arrMax);
sMin = Integer.parseInt(svcMin);
sMax = Integer.parseInt(svcMax);
sTime = Integer.parseInt(simTime);
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String s = (String) comboBox_1.getSelectedItem();
System.out.println("Selected: " + s);
boolean mode = true;
if (s.equals("Time")) {
mode = true;
} else {
mode = false;
}
manager.startSimulation(aMin * 1000, aMax * 1000, sMin * 1000,
sMax * 1000, sTime * 1000, nQueues, mode);
}
}
}
Of course the handler is not relevant in this case since I am only interested in painting that panel.
First of all, this...
private JPanel panel_1 = new JPanel(){
#Override
public void paintComponent(Graphics g){
paintComponents(g);
panel_1.setBackground(Color.orange);
panel_1.setVisible(true);
System.out.println("Just testing");
}
};
Is not what custom painting is for. You should NEVER change the state of the component, or any component for that matter, from inside any paint method.
This could trigger a new repaint request which would put you into a never ending cycle of painting...
Calling paintComponents(g); is going to produce a StackOverflowException. You should not be calling paintComponents any way (note the s at the end) but you should be calling super.paintComponent(g);
JPanel is visible by default.
As it stands, you simply don't need to do anything that you are...
Don't use null layouts. Swing was designed to work with layout managers, you will have no end of issues getting the screen to update without them, besides, pixel perfect layouts are an illusion in modern user interface design, you don't control the fonts, rendering pipelines or other aspects of the target system that might effect how large elements like text are rendered.
Take a look at Laying Out Components Within a Container for more details
You're calling the super.paintComponents method inside of your paintComponent override. Yikes. You are also using null layout and shouldn't be.
You are setting background color but doing nothing with it. You should instead set the background color on your JPanel object, not in its paintComponent method.
i.e., here
panel_1.setBackground(Color.orange); // here
// !! panel_1.setBounds(484, 12, 202, 346); // you shouldn't need this!
contentPane.add(panel_1);
Edit
You state:
Is it enough to simply append a string to the text area, and it will be displayed on the screen automatically?
Yes, as long as you take Swing thread rules into consideration.

java.lang.NoClassDefFoundError:

I have a new error with the code below. Originally I had some classes that were named different. I changed them, but new errors arose. The code below will not run correctly because I had to remove some of it for the post. It had too many characters. Hopefully I didn't get rid of the part that is needed to be fixed.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import javax.swing.ButtonGroup;
import java.awt.FlowLayout;
public class VolCalc extends JFrame implements ActionListener{
private JTabbedPane jtabbedPane;
private JPanel general;
private JPanel pools;
private JPanel tempCalc;
private JPanel options;
private JPanel hotTub;
private JComponent date;
JTextField lengthText, widthText, depthText, volumeText;
public void CalcVolume(){
JPanel customers = new JPanel();
setTitle("Pools");
setSize(300, 200);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
createGeneral();
createPools();
createOptions();
jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("General", general);
jtabbedPane.addTab("Pools", pools);
jtabbedPane.addTab("Temp Calculator", tempCalc);
jtabbedPane.addTab("Options", options);
jtabbedPane.addTab("Hot Tubs", hotTub);
topPanel.add(jtabbedPane, BorderLayout.CENTER);
}
/* CREATE GENERAL */
public void createGeneral(){
general = new JPanel();
general.setLayout(null);
JLabel dateLabel = new JLabel("Todays Date");
dateLabel.setBounds(10, 15, 150, 20);
general.add(dateLabel);
JFormattedTextField date = new JFormattedTextField(
java.util.Calendar.getInstance().getTime());
date.setEditable(false);
date.setBounds(150,15,150,20);
general.add(date);
JButton Exit = new JButton("Exit");
Exit.setBounds(10,80,150,30);
Exit.addActionListener(this);
Exit.setBackground(Color.red);
general.add(Exit);
}
/* CREATE POOLS */
public void createPools(){
pools = new JPanel();
pools.setLayout(null);
JLabel lengthLabel = new JLabel( "Enter the length of swimming pool(ft):");
lengthLabel.setBounds(10, 15, 260, 20);
pools.add(lengthLabel);
lengthText = new JTextField();
lengthText.setBounds( 260, 15, 150, 20 );
pools.add( lengthText );
JLabel widthLabel = new JLabel("Enter the width of the swimming pool(ft):");
widthLabel.setBounds(10, 60, 260, 20);
pools.add(widthLabel);
widthText = new JTextField();
widthText.setBounds(260, 60, 150, 20);
pools.add(widthText);
JLabel depthLabel = new JLabel("Enter the average depth the swimming pool(ft):");
depthLabel.setBounds(10, 100, 260, 20);
pools.add( depthLabel);
depthText = new JTextField();
depthText.setBounds(260, 100, 150, 20);
pools.add(depthText);
JLabel volumeLabel = new JLabel("The pool volume is:(ft ^3");
volumeLabel.setBounds(10, 200, 260, 20);
pools.add(volumeLabel);
volumeText = new JTextField();
volumeText.setBounds(260, 200, 150, 20);
volumeText.setEditable(false);
pools.add(volumeText);
JButton calcVolume = new JButton("Calculate Volume");
calcVolume.setBounds(150,250,150,30);
calcVolume.addActionListener(this);
pools.add(calcVolume);
JButton Exit = new JButton("Exit");
Exit.setBounds(350,250,80,30);
Exit.addActionListener(this);
Exit.setBackground(Color.white);
pools.add(Exit);
}
public void createOptions()
{
options = new JPanel();
options.setLayout( null );
JLabel labelOptions = new JLabel("Change Company Name:");
labelOptions.setBounds( 150, 50, 150, 20 );
options.add( labelOptions );
JTextField newTitle = new JTextField();
newTitle.setBounds( 150, 70, 150, 20 );
options.add( newTitle );
JButton newName = new JButton("Set New Name");
newName.setBounds(100,115,150,30);
newName.addActionListener(this);
newName.setBackground(Color.yellow);
options.add(newName);
JButton Exit = new JButton("Exit");
Exit.setBounds(250,115,80,30);
Exit.addActionListener(this);
Exit.setBackground(Color.red);
options.add(Exit);
}
public void actionPerformed(ActionEvent event){
JButton button = (JButton)event.getSource();
String buttonLabel = button.getText();
if ("Exit".equalsIgnoreCase(buttonLabel)){
Exit_pressed(); return;
}
if ("Set New Name".equalsIgnoreCase(buttonLabel)){
New_Name();
return;
}
if ("Calculate Volume".equalsIgnoreCase(buttonLabel)){
Calculate_Volume(); return;
}
if ("Customers".equalsIgnoreCase(buttonLabel)){
Customers(); return;
}
if ("Calculate Volume".equalsIgnoreCase(buttonLabel)){
Calculate_Volume();
return;
}
if ("Options".equalsIgnoreCase(buttonLabel)){
Options();
return;
}
}
private void Exit_pressed(){
System.exit(0);
}
private void New_Name(){
System.exit(0);
}
private void Calculate_Volume(){
String lengthString, widthString, depthString;
int length=0;
int width=0;
int depth=0;
lengthString = lengthText.getText();
widthString = widthText.getText();
depthString = depthText.getText();
if (lengthString.length() < 1 || widthString.length() < 1 || depthString.length() < 1){
volumeText.setText("Error! Must enter in all three numbers!!");
return;
}
length = Integer.parseInt(lengthString );
width = Integer.parseInt(widthString );
depth = Integer.parseInt(depthString);
if (length != 0 || width != 0 || depth != 0){
volumeText.setText((length * width * depth) + "" );
}
else{
volumeText.setText("Error! Must Enter in all three numbers!!");
return;
}
}
private void Customers(){
}
private void Options(){
}
public static void main(String[] args){
JFrame frame = new VolCalc();
frame.setSize(525, 350);
frame.setVisible(true);
}
}
Errs:
java.lang.NoClassDefFoundError: VolCac Caused by: java.lang.ClassNotFoundException: VolCac at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Exception in thread "main"
My guess is that you tried to use a previous launch configuration, which was using the old name. Launching the class from the context menu (or using the appropriate keyboard shortcut, which I can't remember now - something like Ctrl-Alt-X, J) will use the new name, and may solve the problem.
Alternatively, you could use the dropdown next to the "Run" button and edit the launch configuration to tell it the correct class name to launch.
Don't know how you instantiate this but the stacktrace indicate the "VolCac" class is not found. In the code you pasted here, your class is named "VolCalc"...

Categories

Resources