I am having an issue that is giving me an error message and a blank applet is appearing. The error message says load:class Myapp.class not found and java.lang.ClassNotFoundException and then a list of lines saying # and then some other jargon.
If I run try to run the program as a Jcreator applet like I expected I get the prior stated error and if I run it in an application it just gives me the main method error.
Here is my program:
package MyPack;
import javax.swing.*;
import java.awt.event.*;
public class Myapp extends JApplet implements ItemListener{
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
JComboBox comboBox;
JComboBox comboBox_1;
JCheckBox chckbxPizza;
JCheckBox chckbxBurger;
JCheckBox chckbxSalad;
JLabel label_3;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
JTextArea txtrCommentsForSpecial;
JTextArea txtrCommentsForSpecial_2;
JTextArea txtrCommentsForSpecial_1;
JInternalFrame internalFrame;
public Myapp() {
}
public void init() {
setSize(700,640);
// setVisible(true);
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
guiInit(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}
// Called second, after init(). Also called
// whenever the applet is restarted.
public void start() {
// Not used by this applet.
}
// Called when the applet is stopped.
public void stop() {
// Not used by this applet.
}
// Called when applet is terminated. This is
// the last method executed.
public void destroy() {
// Not used by this applet.
}
// Setup and initialize the GUI.
private void guiInit() {
// Create two buttons and a label.
// setLayout(null);
String a[] = { "Chiken Tikka", "Fajita", "Vegetable"};
comboBox = new JComboBox(a);
comboBox.setBounds(115, 103, 100, 20);
comboBox.addItemListener(this);
getContentPane().add(comboBox);
String b[] = { "small", "medium","large","extra large"};
comboBox_1 = new JComboBox(b);
// comboBox_1.addItemListener(this);
// comboBox_1.setEnabled(false);
getContentPane().add(comboBox_1);
comboBox_1.setBounds(290, 103, 140, 20);
// setSize(300, 300);
getContentPane().setLayout(null);
JLabel lblSelectMenu = new JLabel("Select Menu");
lblSelectMenu.setForeground(new Color(72, 61, 139));
lblSelectMenu.setFont(new Font("Segoe Script", Font.BOLD, 16));
lblSelectMenu.setBounds(58, 10, 107, 34);
getContentPane().add(lblSelectMenu);
chckbxPizza = new JCheckBox("Pizza");
chckbxPizza.setBounds(37, 64, 97, 23);
getContentPane().add(chckbxPizza);
JLabel lblFlavour = new JLabel("flavour");
lblFlavour.setBounds(60, 103, 45, 20);
getContentPane().add(lblFlavour);
JLabel lblSize = new JLabel("size");
lblSize.setBounds(253, 103, 45, 20);
getContentPane().add(lblSize);
JLabel lblQty = new JLabel("Qty");
lblQty.setBounds(451, 103, 35, 20);
getContentPane().add(lblQty);
textField = new JTextField();
textField.setBounds(485, 103, 86, 20);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblComments = new JLabel("comments");
lblComments.setBounds(139, 154, 76, 20);
getContentPane().add(lblComments);
txtrCommentsForSpecial = new JTextArea();
txtrCommentsForSpecial.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial.setText("comments for special features");
txtrCommentsForSpecial.setBounds(210, 154, 268, 61);
getContentPane().add(txtrCommentsForSpecial);
chckbxBurger = new JCheckBox("Burger");
chckbxBurger.setBounds(37, 236, 97, 23);
getContentPane().add(chckbxBurger);
JLabel label = new JLabel("flavour");
label.setBounds(60, 286, 45, 20);
getContentPane().add(label);
String c[] = { "Fish", "Vegetable", "Chicken"};
JComboBox comboBox_2 = new JComboBox(c);
comboBox_2.setBounds(115, 286, 100, 20);
getContentPane().add(comboBox_2);
JLabel label_1 = new JLabel("size");
label_1.setBounds(253, 286, 45, 20);
getContentPane().add(label_1);
JComboBox comboBox_3 = new JComboBox(b);
comboBox_3.setBounds(290, 286, 110, 20);
getContentPane().add(comboBox_3);
JLabel label_2 = new JLabel("Qty");
label_2.setBounds(451, 286, 35, 20);
getContentPane().add(label_2);
textField_1 = new JTextField();
textField_1.setBounds(485, 286, 86, 20);
getContentPane().add(textField_1);
textField_1.setColumns(10);
label_3 = new JLabel("comments");
label_3.setBounds(139, 340, 76, 20);
getContentPane().add(label_3);
txtrCommentsForSpecial_1 = new JTextArea();
txtrCommentsForSpecial_1.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial_1.setText("comments for special features");
txtrCommentsForSpecial_1.setBounds(210, 340, 268, 61);
getContentPane().add(txtrCommentsForSpecial_1);
chckbxSalad = new JCheckBox("Soft Drink");
chckbxSalad.setBounds(37, 424, 97, 23);
getContentPane().add(chckbxSalad);
JLabel label_4 = new JLabel("flavour");
label_4.setBounds(60, 475, 45, 20);
getContentPane().add(label_4);
String f[] = { "Pepsi", "Coca Cola", "Sprite","7up"};
JComboBox comboBox_4 = new JComboBox(f);
comboBox_4.setBounds(115, 475, 86, 20);
getContentPane().add(comboBox_4);
JLabel label_5 = new JLabel("size");
label_5.setBounds(253, 475, 45, 20);
getContentPane().add(label_5);
JComboBox comboBox_5 = new JComboBox(b);
comboBox_5.setBounds(290, 475, 110, 20);
getContentPane().add(comboBox_5);
JLabel label_6 = new JLabel("Qty");
label_6.setBounds(451, 475, 35, 20);
getContentPane().add(label_6);
textField_2 = new JTextField();
textField_2.setBounds(485, 475, 86, 20);
getContentPane().add(textField_2);
textField_2.setColumns(10);
JLabel label_7 = new JLabel("comments");
label_7.setBounds(139, 546, 76, 20);
getContentPane().add(label_7);
txtrCommentsForSpecial_2 = new JTextArea();
txtrCommentsForSpecial_2.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial_2.setText("comments for special features");
txtrCommentsForSpecial_2.setBounds(210, 546, 268, 61);
getContentPane().add(txtrCommentsForSpecial_2);
JButton btnNewButton = new JButton("Ok");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
internalFrame = new JInternalFrame("Order Details");
internalFrame.setBounds(100, 80, 450, 300);
internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
internalFrame.getContentPane().setLayout(null);
internalFrame.setVisible(true);
JTextArea textArea = new JTextArea();
textArea.setBounds(67, 52, 270, 149);
internalFrame.getContentPane().add(textArea);
JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
internalFrame.setVisible(false);
}
});
btnOk.setBounds(164, 212, 89, 23);
internalFrame.getContentPane().add(btnOk);
JLabel lblOrderYouPlaced = new JLabel("Order You Placed");
lblOrderYouPlaced.setBounds(67, 26, 98, 14);
internalFrame.getContentPane().add(lblOrderYouPlaced);
getContentPane().add(internalFrame);
}
});
btnNewButton.setBounds(162, 616, 89, 23);
getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Reset");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField_1.setText("");
textField_2.setText("");
chckbxPizza.setText("");
chckbxBurger.setText("");
chckbxSalad.setText("");
txtrCommentsForSpecial.setText("");
txtrCommentsForSpecial_2.setText("");
txtrCommentsForSpecial_1.setText("");
}
});
btnNewButton_1.setBounds(279, 616, 89, 23);
getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Cancel");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnNewButton_2.setBounds(389, 616, 89, 23);
getContentPane().add(btnNewButton_2);
}
public void itemStateChanged(ItemEvent e) {
String b[] = { "Chikken Tikka", "Fajita" };
String c[] = { "Fish", "Vegetable", "Chicken"};
String d[] = { "Russian Salad", "Fruit Salad", "Beans Salad"};
String f[] = { "Pepsi", "Coca Cola", "Sprite","7up"};
if (e.getSource() == comboBox) {
if (comboBox.getSelectedItem().equals("Select")) {
comboBox_1.setEnabled(false);
} else if (comboBox.getSelectedItem().equals("Pizza")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < b.length; i++) {
comboBox_1.addItem(b[i]);
}
} else if (comboBox.getSelectedItem().equals("Burger")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < c.length; i++) {
comboBox_1.removeItem(c[i]);
comboBox_1.addItem(c[i]);
}
} else if (comboBox.getSelectedItem().equals("Salad")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < d.length; i++) {
comboBox_1.addItem(d[i]);
}
}else if (comboBox.getSelectedItem().equals("Soft Drink")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < f.length; i++) {
comboBox_1.addItem(f[i]);
}
}
}
}
}
Write the itemListener with a captial letter like this
public class Myapp extends JApplet implements ItemListener{
and import all this packages:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
Related
Im trying to sort my GUI so it will be able to use the methods from the other classes
in the JProject i also want to call it from the main.
The action listeners i had just learnt today so i need feedback on what to do there as well.
Any Ideas??
It would be a great help to get some insight on this!
(I am new to stack so my code posted in the explanation and in the actual script idk
why).
package test;
import javax.swing.JPanel;
import javax.swing.JLabel;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.Container;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.JRadioButton;
import java.awt.Font;
import java.io.File;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.util.ArrayList;
import java.util.Scanner;
//Main Method
public class TextUI{
protected StormTracker var;
public TextUI(StormTracker var)
{
this.var=var;
}
public static void main(String[] args)
{
ShowGUI gui = new SHOWGUI();
while(gui.running)
if(gui.buttonPress == -1)
continue;
StormTracker obj = new StormTracker("config.txt");
WeatherAuthority wa = new WeatherAuthority("stormfile.txt");
int xcoord=0;
int ycoord=0;
TextUI uiobj = new TextUI(obj);
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("---------------------------------------------------------------");
System.out.println("Welcome to the StormTracker Application.");
System.out.println("Type the following commands to carry out the associated actions.");
System.out.println("\n");
System.out.println("----------------------{Commands Menu}---------------------
-------");
System.out.println("(1) NEW STORM x y");
System.out.println("(2) UPGRADE <name of storm>");
System.out.println("(3) DOWNGRADE <name of storm>");
System.out.println("(4) TICK");
System.out.println("(5) EXIT");
System.out.println("----------------------------------------------------------------");
String option=scan.nextLine();
if (option.substring(0,4).equals("TICK")){
obj.tick();
continue;
}
if (option.substring(0,9).equals("NEW STORM")){
if(option.length()==13){
xcoord = Integer.parseInt(option.substring(10,11));
ycoord = Integer.parseInt(option.substring(12,13));
}
else if (option.length()==14){
if (option.substring(11,12).equals(" ")){
xcoord = Integer.parseInt(option.substring(10,11));
ycoord = Integer.parseInt(option.substring(12,14));
}
else{
xcoord = Integer.parseInt(option.substring(10,12));
ycoord = Integer.parseInt(option.substring(13,14));
}
}
else if (option.length()==15){
xcoord = Integer.parseInt(option.substring(10,12));
ycoord = Integer.parseInt(option.substring(13,15));
}
obj.newstorm(xcoord,ycoord);
}
if (option.substring(0,7).equals("UPGRADE")){
String storm_name = option.substring(8);
obj.upgrade(storm_name);
}
if (option.substring(0,9).equals("DOWNGRADE")){
String storm_name = option.substring(10);
obj.downgrade(storm_name);
}
}
}
//GUI Class
public ShowGUI extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
System.out.println("GUI Successfully Loaded");
createAndShow();
setToolTipText("Select Storm");
setBackground(Color.WHITE);
}
private void createAndShow(){
JPanel mainFrame = new JPanel("Storm Tracker V1.0");
}
private void addComponents(Container pane){
pane.setLayout(null);
JLabel lblStormTrackerV = DefaultComponentFactory.getInstance().createTitle("
Storm Tracker V1.0");
lblStormTrackerV.setBackground(SystemColor.activeCaption);
lblStormTrackerV.setFont(new Font("Calibri", Font.PLAIN, 22));
lblStormTrackerV.setBounds(162, 11, 430, 41);
add(lblStormTrackerV);
JRadioButton rdbtnMap = new JRadioButton("MAP");
rdbtnMap.setBounds(283, 59, 72, 23);
add(rdbtnMap);
JRadioButton rdbtnBulletin = new JRadioButton("Bulletin");
rdbtnBulletin.setBounds(406, 60, 72, 23);
add(rdbtnBulletin);
textField = new JTextField();
textField.setBounds(61, 103, 72, 20);
add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(61, 134, 72, 20);
add(textField_1);
textField_1.setColumns(10);
JTextPane txtpnX = new JTextPane();
txtpnX.setText("X:");
txtpnX.setBounds(23, 103, 28, 20);
add(txtpnX);
JTextPane txtpnY = new JTextPane();
txtpnY.setText("Y:");
txtpnY.setBounds(23, 134, 12, 20);
add(txtpnY);
JTextPane txtpnSpeed = new JTextPane();
txtpnSpeed.setText("Speed:");
txtpnSpeed.setBounds(10, 165, 41, 20);
add(txtpnSpeed);
textField_2 = new JTextField();
textField_2.setBounds(61, 165, 72, 20);
add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(61, 196, 72, 20);
add(textField_3);
textField_3.setColumns(10);
JTextPane txtpnSpeed_1 = new JTextPane();
txtpnSpeed_1.setText("Speed:");
txtpnSpeed_1.setBounds(10, 196, 41, 20);
add(txtpnSpeed_1);
JTextPane txtpnBearing = new JTextPane();
txtpnBearing.setText("Bearing:");
txtpnBearing.setBounds(10, 227, 41, 20);
add(txtpnBearing);
textField_4 = new JTextField();
textField_4.setBounds(61, 227, 72, 20);
add(textField_4);
textField_4.setColumns(10);
File input = new File(new
FileReader("C:\\Users\\620086793\\Documents\\test\\src\\test"));
List<String> strings = new ArrayList<String>();
try {
String line = null;
while (( line = input.readLine()) != null){
strings.add(line);
}
}
catch (FileNotFoundException e) {
System.err.println("Error, file " + "C:\\Users\\620086793\\Documents\\test\\src\\test" + " didn't exist.");
}
finally {
input.close();
}
String[] lineArray = strings.toArray(new String[]{});
JComboBox comboBox = new JComboBox(lineArray);
JComboBox comboBox = new JComboBox();
comboBox.setToolTipText("Select Storm");
comboBox.setEditable(true);
comboBox.setBounds(20, 60, 113, 20);
add(comboBox);
JButton btnUpdate = new JButton("UPDATE");
btnUpdate.setFont(new Font("Calibri", Font.BOLD, 11));
btnUpdate.setBounds(10, 273, 110, 28);
add(btnUpdate);
JButton btnNewButton = new JButton("NEW STORM\r\n");
btnNewButton.setFont(new Font("Calibri", Font.BOLD, 11));
btnNewButton.setBounds(10, 312, 110, 28);
add(btnNewButton);
addActionListeners();
pane.add(rdbtnMap);
pane.add(rdbtnBulletin);
pane.add(textField);
pane.add(textField_1);
pane.add(textField_2);
pane.add(textField_3);
pane.add(textField_4);
pane.add(comboBox);
pane.add(btnUpdate);
pane.add(btnNewButton);
}
//Im not sure how to properly add the action listeners i first learned about them
today.
private void addActionListeners(){
rdbtnMap.addActionListener(new ActionListener){
public void actionPerformed(ActionEvent ){
}
}
rdbtnBulletin.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_2.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_3.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_4.addActionListener(){
public void actionPerformed(ActionEvent ){
stormTracker.tick = textField_4;
}
}
comboBox.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
btnUpdate.addActionListener(){
public void actionPerformed(ActionEvent ){
obj.upgrade() = btnUpdate;
return btnUpdate;
}
}
btnNewButton.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
}
When I try to add some function to get data from jtext field under public void actionPerformed(ActionEvent arg0) My GUI goes blank I have attached the images below I can't figure out what's wrong with it.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Register extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblName = new JLabel("Name");
lblName.setBounds(56, 56, 56, 16);
contentPane.add(lblName);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(56, 85, 77, 16);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(56, 114, 56, 16);
contentPane.add(lblPassword);
JLabel lblAge = new JLabel("Age");
lblAge.setBounds(56, 143, 56, 16);
contentPane.add(lblAge);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(56, 172, 56, 16);
contentPane.add(lblGender);
textField = new JTextField();
textField.setBounds(130, 53, 116, 19);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(130, 82, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(130, 111, 116, 22);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(130, 140, 116, 22);
contentPane.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(130, 169, 116, 22);
contentPane.add(textField_4);
textField_4.setColumns(10);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnSubmit.setBounds(169, 215, 97, 25);
contentPane.add(btnSubmit);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(330, 56, 56, 16);
contentPane.add(lblNewLabel);
}
}
EDIT 1: Added String name to action just to get name.
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String Name;
}
});
and This happened
GUI went blank:
You're using an absolute layout. Since you can't pack() the window, you need to at least validate() its contents.
Register frame = new Register();
frame.validate();
frame.setVisible(true);
Now you can see the real problem: you picked bounds for lblPassword and lblNewLabel that cut off the text on my computer. You really need to use a layout, maybe like this.
i write a convert temperature code using radiobutton, but when i use actionListener to print the value to a label, it show "Cannot refer to the non-final local variable valueto defined in an enclosing scope"
can some one show me the problem and how to fix it? (also sorry for my English)
the Radiobutton
JRadioButton Cbutton = new JRadioButton("Celsius");
Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton.setBounds(10, 40, 109, 23);
contentPane.add(Cbutton);
JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
Fbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(valueto));
}
});
i use this to convert
if(Fbutton.isSelected()&&Cbutton2.isSelected()){
value=F;
valueto=(F-32)/1.8;
}else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
value=F;
valueto=(F+459.67)*5/9;
}else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
value=C;
valueto=C*1.8+32;
}else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
value=C;
valueto=C+273.15;
}else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
value=K;
valueto=K-273.15;
}else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
value=K;
valueto=K*9.5-459.67;
}
Edit : i declare valueto at the bottom of the class
double value = 0;
double valueto = 0;
double F = 0, C = 0, K = 0;
it ask me to add final to "double valueto", but when i add it the valueto in "if" become error
The Whole code
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class CtoKtoF extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CtoKtoF frame = new CtoKtoF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* #return
*/
public CtoKtoF() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 303);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
double value = 0 ;
double valueto = 0;
double F = 0, C = 0, K = 0;
Border border = LineBorder.createGrayLineBorder();//make border for Jlabel
final JLabel label = new JLabel("New label");
label.setFont(new Font("Tahoma", Font.PLAIN, 12));
label.setBounds(10, 228, 220, 22);
label.setBorder(border);
contentPane.add(label);
JLabel lblNewLabel = new JLabel("Convert from");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblNewLabel.setBounds(10, 11, 92, 22);
contentPane.add(lblNewLabel);
JRadioButton Fbutton = new JRadioButton("Fahrenheit");
Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton.setBounds(10, 40, 109, 23);
//Fbutton.setActionCommand("F");
contentPane.add(Fbutton);
JRadioButton Cbutton = new JRadioButton("Celcius");
Cbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Cbutton.setBounds(121, 40, 109, 23);
//Cbutton.setActionCommand("C");
contentPane.add(Cbutton);
JRadioButton Kbutton = new JRadioButton("Kelvin");
Kbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Kbutton.setBounds(232, 40, 109, 23);
//Kbutton.setActionCommand("K");
contentPane.add(Kbutton);
JLabel lblEnterNumericTemperature = new JLabel("Enter Numeric Temperature");
lblEnterNumericTemperature.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblEnterNumericTemperature.setBounds(10, 70, 238, 22);
contentPane.add(lblEnterNumericTemperature);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 12));
textField.setBounds(10, 103, 220, 22);
textField.setText(String.valueOf(value));
contentPane.add(textField);
textField.setColumns(10);
JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
Fbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(valueto));
}
});
Fbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton2.setBounds(10, 165, 109, 23);
//Fbutton2.setActionCommand("F2");
contentPane.add(Fbutton2);
JRadioButton Cbutton2 = new JRadioButton("Celcius");
Cbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Cbutton2.setBounds(121, 165, 109, 23);
//Cbutton2.setActionCommand("C2");
contentPane.add(Cbutton2);
JRadioButton Kbutton2 = new JRadioButton("Kelvin");
Kbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Kbutton2.setBounds(232, 165, 109, 23);
//Kbutton2.setActionCommand("K2");
contentPane.add(Kbutton2);
ButtonGroup group1 = new ButtonGroup();
group1.add(Fbutton);
group1.add(Kbutton);
group1.add(Cbutton);
ButtonGroup group2 = new ButtonGroup();
group2.add(Fbutton2);
group2.add(Cbutton2);
group2.add(Kbutton2);
/*Fbutton.addActionListener(this);
Cbutton.addActionListener(this);
Kbutton.addActionListener(this);
Fbutton2.addActionListener(this);
Cbutton2.addActionListener(this);
Kbutton2.addActionListener(this);*/
//boolean checked = Fbutton.getState();
JLabel lblConvertTo = new JLabel("Convert to");
lblConvertTo.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblConvertTo.setBounds(10, 136, 92, 22);
contentPane.add(lblConvertTo);
JLabel lblComparableTemperatureIs = new JLabel("Comparable Temperature is");
lblComparableTemperatureIs.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblComparableTemperatureIs.setBounds(10, 195, 238, 22);
contentPane.add(lblComparableTemperatureIs);
if(Fbutton.isSelected()&&Cbutton2.isSelected()){
value=F;
valueto=(F-32)/1.8;
}else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
value=F;
valueto=(F+459.67)*5/9;
}else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
value=C;
valueto=C*1.8+32;
}else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
value=C;
valueto=C+273.15;
}else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
value=K;
valueto=K-273.15;
}else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
value=K;
valueto=K*9.5-459.67;
}
}
}
I'm a beginer in Java GUI, and i have an issue with trying to toggle between panel in other class.
My main Class "MyBoxGUI" has 2 Panels, "Login" and "UserMenu"
The default is "Login" panel, when pressing "OK" the window moved to "UserMenu" Panel.
So far so good while in the same class.
The issue is, when i set a new panel in new class and setvisibe that panel, i can't go back to "UserMenu" panel - (The actionlistener is working fine).
Can someone help me please?
MyBoxGUI Class
package main;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import java.awt.Component;
//import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Toolkit;
import GUIs.*;
//import ocsf.client.*;
public class MyBoxGUI extends JPanel
{
private Mtds mtd;
public JFrame frmMybox;
private static JTextField UserName;
private static JTextField port;
private static JTextField IP;
private static JPasswordField passwordField;
public final JPanel Login = new JPanel();
public final JPanel UserMenu = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
MyBoxGUI window = new MyBoxGUI();
window.frmMybox.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MyBoxGUI()
{
mtd = new Mtds();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frmMybox = new JFrame();
frmMybox.setFont(new Font("Tempus Sans ITC", Font.BOLD, 15));
// frmMybox.setIconImage(Toolkit.getDefaultToolkit().getImage(MyBoxGUI.class.getResource("/images/gift-ideas-gift-card-bridal-gift-baby-shower-gift-gift-box-groom-gift-christmas-gift-party-gift-gift-for-wedding-friend-gift-birthday-gift-baby-gift-good-gift-box-ideas-for-friend-necklace-gift-box.jpg")));
frmMybox.setBounds(100, 100, 800, 500);
frmMybox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMybox.getContentPane().setLayout(new CardLayout(0, 0));
frmMybox.setTitle("MyBox");
frmMybox.getContentPane().add(Login);
Login.setLayout(null);
JButton btnNewButton = new JButton("OK");
btnNewButton.addActionListener(new ActionListener()
{
#SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent arg0)
{
Boolean ok = false;
String UName, Pass, Prt, SIP;
UName = UserName.getText();
Pass = passwordField.getText();
Prt = port.getText();
SIP = IP.getText();
if( ( mtd.isUserValid(UName) && mtd.isPasswordValid(Pass) && mtd.isPortValid(Prt) && mtd.isIPValid(SIP))
&& ( !UName.equals("") && !Pass.equals("") && !Prt.equals("") && !SIP.equals("") ) )
ok = true;
else
{
if(!mtd.isUserValid(UName) || UName.equals(""))
JOptionPane.showMessageDialog(frmMybox,"User Name Can Contain Only The Following Characters: ( 0-9 , a-z , A-Z , _ )"
+ " ","Invalid characters",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isPasswordValid(Pass) || Pass.equals(""))
JOptionPane.showMessageDialog(frmMybox,"Invalid characters","Error Message",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isPortValid(Prt) || Prt.equals(""))
JOptionPane.showMessageDialog(frmMybox,"Port Can Contain Only Numbers","Invalid characters",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isIPValid(SIP) || SIP.equals(""))
JOptionPane.showMessageDialog(frmMybox,"IP Address Can Contain Only Numbers seperated By Dots '.' ","Invalid characters",JOptionPane.WARNING_MESSAGE);
}
if(ok)
{
//LoginController user = new LoginController();
//chat= new ClientGUI(IP.getText(),port.getText());
//client = ClientGUI.getClient();
//msg=new Msg("login",user);
//client.handleMessageFromClientUI(msg);
setScreen(Login,UserMenu);
// frmMybox.getContentPane().add(UserMenu, "UserMenu");
// UserMenu.setVisible(true);
// Login.setVisible(false);
}
}
});
btnNewButton.setBounds(344, 313, 82, 48);
Login.add(btnNewButton);
JLabel lblWellcomeToMybox = new JLabel("Wellcome to MyBox");
lblWellcomeToMybox.setForeground(new Color(51, 204, 255));
lblWellcomeToMybox.setFont(new Font("Arial", Font.PLAIN, 36));
lblWellcomeToMybox.setBounds(224, 23, 327, 42);
Login.add(lblWellcomeToMybox);
JLabel lblUserName = new JLabel("User Name:");
lblUserName.setBounds(268, 109, 89, 14);
lblUserName.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblUserName);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(268, 139, 69, 14);
lblPassword.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblPassword);
JLabel lblServerPort = new JLabel("Server port:");
lblServerPort.setBounds(268, 197, 82, 14);
lblServerPort.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblServerPort);
JLabel lblServerIp = new JLabel("Server IP:");
lblServerIp.setBounds(266, 222, 71, 14);
lblServerIp.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblServerIp);
UserName = new JTextField();
UserName.setBounds(406, 107, 96, 20);
Login.add(UserName);
UserName.setColumns(10);
port = new JTextField();
port.setBounds(406, 195, 96, 20);
Login.add(port);
port.setColumns(10);
IP = new JTextField();
IP.setBounds(406, 220, 96, 20);
Login.add(IP);
IP.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(406, 137, 96, 20);
Login.add(passwordField);
frmMybox.getContentPane().add(UserMenu, "User Menu");
UserMenu.setLayout(null);
JButton LeaveGOI = new JButton("Back");
LeaveGOI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
setScreen(UserMenu,Login);
}
});
LeaveGOI.setBounds(10, 66, 153, 23);
UserMenu.add(LeaveGOI);
JButton btnJoinAGoi = new JButton("Join a GOI");
btnJoinAGoi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
btnJoinAGoi.setBounds(10, 100, 153, 23);
UserMenu.add(btnJoinAGoi);
JButton btnSearchForA = new JButton("Search for a GOI");
btnSearchForA.setBounds(10, 134, 153, 23);
UserMenu.add(btnSearchForA);
JButton btnCreateAGoi = new JButton("Create a GOI");
btnCreateAGoi.setBounds(10, 32, 153, 23);
UserMenu.add(btnCreateAGoi);
JButton btnFilesSharedWith = new JButton("Files shared with you");
btnFilesSharedWith.setBounds(271, 66, 153, 23);
UserMenu.add(btnFilesSharedWith);
JButton btnUploadAFile = new JButton("Upload a file");
btnUploadAFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
JPanel UpLoad = new UploadFile();
setScreen(UserMenu,UpLoad);
// frmMybox.getContentPane().add(UpLoad, "UpLoad");
// UserMenu.setVisible(false);
// UpLoad.setVisible(true);
}
});
btnUploadAFile.setBounds(271, 32, 153, 23);
UserMenu.add(btnUploadAFile);
JButton btnSignout = new JButton("Sign-Out");
btnSignout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int dialog=JOptionPane.showConfirmDialog(Login, getUserName()+", are you sure you wants to leave?", "Do you want to leave?", JOptionPane.YES_NO_OPTION);
if(dialog==0){
//client.quit();
}
}
});
btnSignout.setBounds(293, 227, 131, 23);
UserMenu.add(btnSignout);
JButton btnHelpme = new JButton("Help-Me");
btnHelpme.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JOptionPane.showMessageDialog(Login,"Hello "+ getUserName()+", wellcom to Mybox!\n"
+ "Inside GOIs buttons you can ask to join, leave and even create a GOI.\n"
+ "Inside Files buttons you can check and edit files people shared with you.\n"
+ "You can even become an uploader and share files you own.\n"
+ "We wish you good luck and have fun using MyBox!");
}
});
btnHelpme.setBounds(10, 227, 131, 23);
UserMenu.add(btnHelpme);
}
public static String getUserName(){
return UserName.getText();
}
#SuppressWarnings("deprecation")
public static String getPassword(){
return passwordField.getText();
}
public static String getIP(){
return IP.getText();
}
public static String getPort(){
return port.getText();
}
public void setScreen(JPanel fls, JPanel tru)
{
frmMybox.getContentPane().add(tru);
tru.setVisible(true);
fls.setVisible(false);
}
}
UploadFile Class
package GUIs;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import main.MyBoxGUI;
public class UploadFile extends MyBoxGUI {
private JTextField textField;
/**
* Create the panel.
*/
public static final JPanel UpLoad = new JPanel();
public UploadFile()
{
setBounds(100, 100, 800, 500);
setLayout(null);
JButton btnNewButton = new JButton("Back");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
setScreen(UpLoad,UserMenu);
System.out.print("A+++");
}
});
btnNewButton.setBounds(126, 145, 205, 30);
add(btnNewButton);
textField = new JTextField();
textField.setBounds(409, 150, 300, 20);
add(textField);
textField.setColumns(10);
JButton btnDone = new JButton("Done");
btnDone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnDone.setBounds(365, 223, 89, 30);
add(btnDone);
JButton btnHelpMe = new JButton("Help Me");
btnHelpMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnHelpMe.setBounds(188, 390, 122, 52);
add(btnHelpMe);
JButton SignOut = new JButton("Sign-Out");
SignOut.setBounds(501, 392, 122, 48);
add(SignOut);
}
}
public void setScreen(JPanel fls, JPanel tru)
{
frmMybox.getContentPane().add(tru);
tru.setVisible(true);
fls.setVisible(false);
frmMybox.getContentPane().revalidate();
frmMybox.getContentPane().repaint();
}
update your setScreen method as above.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
public class ex12 extends JFrame implements ActionListener {
JPanel contentPane;
JTextField textField;
JTextField textField_1;
JTextField textField_2;
JTextField textField_3;
JTextField textField_4;
JTextField textField_5;
public ex12() {
setTitle("Student marks");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 472, 411);
contentPane = new JPanel();
contentPane.setBorder(null);
contentPane.setLayout(null);
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("Name");
lblNewLabel.setBounds(62, 61, 46, 14);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(153, 58, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("m1");
lblNewLabel_1.setBounds(62, 108, 46, 14);
contentPane.add(lblNewLabel_1);
textField_1 = new JTextField();
textField_1.setBounds(153, 105, 86, 20);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("m2");
lblNewLabel_2.setBounds(62, 157, 46, 14);
contentPane.add(lblNewLabel_2);
textField_2 = new JTextField();
textField_2.setBounds(153, 154, 86, 20);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("tot");
lblNewLabel_3.setBounds(62, 213, 46, 14);
contentPane.add(lblNewLabel_3);
textField_3 = new JTextField();
textField_3.setBounds(153, 210, 86, 20);
contentPane.add(textField_3);
textField_3.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("Avg");
lblNewLabel_4.setBounds(62, 270, 46, 14);
contentPane.add(lblNewLabel_4);
textField_4 = new JTextField();
textField_4.setBounds(153, 267, 86, 20);
contentPane.add(textField_4);
textField_4.setColumns(10);
JLabel lblNewLabel_5 = new JLabel("Grade");
lblNewLabel_5.setBounds(62, 317, 46, 14);
contentPane.add(lblNewLabel_5);
textField_5 = new JTextField();
textField_5.setBounds(153, 314, 86, 20);
contentPane.add(textField_5);
textField_5.setColumns(10);
JButton ok = new JButton("Ok");
ok.setBounds(313, 73, 89, 23);
contentPane.add(ok);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
double bsal,tax,allo,nsal;
if (arg0. getSource().equals(ok))
{
if(bsal > 5000 )
{
tax = bsal * 0.15;
allo = bsal * 0.2;
}
else if (bsal > 30000)
{
tax = bsal * 0.5;
allo = bsal * 0.1;
}
else if (bsal > 20000)
{
tax = 0;
allo = 0;
}
else
{
tax = 0;
allo = 0;
}
}
}
});
}
public static void main(String[] args) {
ex12 e = new ex12();
e.show();
}
}
above program i tried to write ActionListener it is giving error.
this is code
public class ex12 extends JFrame implements ActionListener
i wrote this program on eclipse
please any one fix this error
You need to implement the required methods that come from ActionListener interface because you're implementing the ActionListener interface.
First you could implement the actionPerformed method, and do like this
#override
public void actionPerformed(ActionEvent arg0) {
}
Then you can just add action listener.
ok.addActionListener(this);
Second you could just remove the implementing the ActionListener at all, and keep your class like:
public class ex12 extends JFrame {
//rest of your code.
if you want to write your own ActionListener try this:
public class Example extends JFrame {
private JButton ok = new JButton("ok");
public Example() {
ok.addActionListener(new OwnActionListener());
}
private class OwnActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
}
Or you can just implement the Interface like that:
public class Example extends JFrame implements ActionListener {
private JButton ok = new JButton("ok");
public Example() {
ok.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}