Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: The problem with Java and CPU usage, need HELP

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The problem with Java and CPU usage, need HELP

    Thank You for your reply!
    I write it in Java Language, yes i launch then all in the same time, every program will not require very much processor time, because if i run it in one language it is almost OK, working very normally, I am using DELL laptop i5 2.67GHz processor with 4GB RAM, and Java 1.7 JDK. each program just doing as an Automatic Transfer Money machine is do, namely (Deposit, Withdraw, Transfer money, Balance in Query, Exit) i think it is not much complex, there is three classes for each language, and three classes which every language shared like Database class which has the get Connection and so on method which is used by these three languages. and at the end one other class is used to collect these three languages to one, the class name is AllInOneFrame, the other classes is extended JPanel, and implements Key, Action, and Mouse Listeners, and the AllInOneFrame class is extended the JFrame, when i run this class the CPU usage dramatically goes to 100, and Computer become very slow.... I don't if there is any way to post the Project, because if i post the entire code it is very long, but i will post the last AllInOneFrame class for more information. thank you


    package com.ustb;

    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;

    import javax.swing.*;
    import javax.swing.border.Border;

    public class AllInOneFrame extends JFrame implements ActionListener, MouseListener {
    private static final long serialVersionUID = 1L;

    static JPanel cards;

    private JPanel panel1, panel2, panel3;

    private static JPanel panel4 = new JPanel();
    private JButton englishButton = new JButton("English");
    private JButton chinesehButton = new JButton("中文");
    private JButton persianhButton = new JButton("فارسی");

    private ImageIcon Up = new ImageIcon(getClass().getResource("image/Up.png"));
    private ImageIcon Up2 = new ImageIcon(getClass().getResource(
    "image/Up2.png"));
    private ImageIcon Do = new ImageIcon(getClass().getResource("image/Do.png"));
    private ImageIcon Do2 = new ImageIcon(getClass().getResource(
    "image/Do2.png"));
    private JLabel lableAdvertizeUp = new JLabel(Up);
    private JLabel lableAdvertizeDo = new JLabel(Do);
    private JLabel label = new JLabel();
    Border border = BorderFactory.createRaisedBevelBorder();

    ATMEnglish english = new ATMEnglish();
    ATMChinese chinese = new ATMChinese();
    ATMPersian persian = new ATMPersian();

    static boolean error;
    static boolean reSet = false;
    static ArrayList<String> errorIDList = new ArrayList<String>();

    Border border1 = BorderFactory.createLoweredBevelBorder();

    public AllInOneFrame() {
    panel1 = new JPanel(new FlowLayout());
    panel1.add(englishButton);
    panel1.add(chinesehButton);
    panel1.add(persianhButton);
    englishButton.setBackground(Color.decode("#800000" ));
    chinesehButton.setBackground(Color.decode("#800000 "));
    persianhButton.setBackground(Color.decode("#800000 "));
    englishButton.setBorder(border1);
    chinesehButton.setBorder(border1);
    persianhButton.setBorder(border1);
    MyFonts.setLabelFont(englishButton, Color.WHITE, 40);
    MyFonts.setLabelFont(chinesehButton, Color.WHITE, 40);
    MyFonts.setLabelFont(persianhButton, Color.WHITE, 40);
    MyFonts.setComponentFont(panel1, "#0000A0", Color.BLACK, null, 0);

    JLabel eng = new JLabel("Please Select Your Language", JLabel.CENTER);
    JLabel chn = new JLabel("请选择语言", JLabel.CENTER);
    JLabel per = new JLabel("لطفاً زبان خودرا انتخاب کنید", JLabel.CENTER);
    panel2 = new JPanel(new GridLayout(3, 0));
    panel2.add(eng);
    panel2.add(chn);
    panel2.add(per);
    MyFonts.setLabelFont(eng, Color.YELLOW, 50);
    MyFonts.setLabelFont(chn, Color.YELLOW, 50);
    MyFonts.setLabelFont(per, Color.YELLOW, 50);
    MyFonts.setComponentFont(panel2, "#0000A0", Color.BLACK, null, 0);

    panel3 = new JPanel(new BorderLayout());
    panel3.add(panel2, BorderLayout.NORTH);
    panel3.add(panel1, BorderLayout.CENTER);

    panel4 = new JPanel(new BorderLayout());
    panel4.add(panel3, BorderLayout.CENTER);

    cards = new JPanel();
    cards.setLayout(new CardLayout());
    cards.add("first", label);
    cards.add("center", panel4);
    cards.add("one", english.englishPanel());
    cards.add("two", chinese.chinesePanel());
    cards.add("three", persian.persianPanel());

    this.add(lableAdvertizeUp, BorderLayout.NORTH);
    this.add(cards, BorderLayout.CENTER);
    this.add(lableAdvertizeDo, BorderLayout.SOUTH);
    this.setSize(800, 600);
    this.setTitle("Auto Transfere Money");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setVisible(true);

    englishButton.addActionListener(this);
    chinesehButton.addActionListener(this);
    persianhButton.addActionListener(this);
    englishButton.addMouseListener(this);
    chinesehButton.addMouseListener(this);
    persianhButton.addMouseListener(this);
    label.addMouseListener(this);
    panel4.addMouseListener(this);

    new Thread(new MyAnimations.MyAnimation2(lableAdvertizeUp,
    lableAdvertizeDo, Up, Do, Up2, Do2)).start();
    MyAnimations anim2 = new MyAnimations();
    MyAnimations.MyAnimation3 animation2 = anim2.new MyAnimation3(label,"pic", 3);
    new Thread(animation2).start();
    //new Thread(new WrongPasswordLocker()).start();
    }

    public static void main(String[] args) {
    new AllInOneFrame();

    }

    public class WrongPasswordLocker implements Runnable {

    public void run() {
    try {
    while (true) {
    for (int i = 1; i <= 60; i++) {
    Thread.sleep(1000);
    if(error) {
    i = 1;
    error = false;
    }
    }
    if(errorIDList.size() == 0)
    continue;
    else {
    errorIDList.clear();
    }
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    }

    public void actionPerformed(ActionEvent e) {
    if(e.getSource() == englishButton) {
    english.startSeytem = true;
    reSet = true;
    ((CardLayout) cards.getLayout()).show(cards, "one");
    }
    if(e.getSource() == chinesehButton) {
    chinese.startSeytem = true;
    reSet = true;
    ((CardLayout) cards.getLayout()).show(cards, "two");
    }
    if(e.getSource() == persianhButton) {
    persian.startSeytem = true;
    reSet = true;
    ((CardLayout) cards.getLayout()).show(cards, "three");
    }
    }

    static void addListener() {
    //panel4.addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {
    if(e.getSource() == englishButton) {
    englishButton.setBackground(Color.decode("#000000" ));
    }
    else if(e.getSource() == chinesehButton) {
    chinesehButton.setBackground(Color.decode("#000000 "));
    }
    else if(e.getSource() == persianhButton) {
    persianhButton.setBackground(Color.decode("#000000 "));
    }
    else if(e.getSource() == label)
    ((CardLayout) cards.getLayout()).show(cards, "center");
    }

    public void mouseExited(MouseEvent e) {
    if(e.getSource() == englishButton)
    englishButton.setBackground(Color.decode("#800000" ));
    else if(e.getSource() == chinesehButton)
    chinesehButton.setBackground(Color.decode("#800000 "));
    else if(e.getSource() == persianhButton)
    persianhButton.setBackground(Color.decode("#800000 "));
    else if(e.getSource() == panel4)
    ((CardLayout) cards.getLayout()).show(cards, "first");

    }

    public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

    }

    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

    }

    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,261
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: The problem with Java and CPU usage, need HELP

    Please read the forum rules. You have posted the same thing three times. Double posting is not allowed. I am locking two of your posts. Stick to the first:
    When i run the Program, the CPU usage become very high
    And read the following:
    This Forum is for Saying Hi, not for Asking Technical Questions (READ BEFORE POSTING)

  3. The Following User Says Thank You to copeg For This Useful Post:

    jps (July 25th, 2012)

Similar Threads

  1. Problem with java program and CPU usage
    By Bahramudin in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2012, 09:26 AM
  2. When i run the Program, the CPU usage become very high
    By Bahramudin in forum Member Introductions
    Replies: 2
    Last Post: July 21st, 2012, 04:37 AM
  3. Usage of ClassLoader.setSigners() method
    By AlexDorogensky in forum Java Theory & Questions
    Replies: 1
    Last Post: January 13th, 2011, 09:37 AM
  4. How to get a System's total CPU usage percentage using a java program
    By userj2ee in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 12:28 AM
  5. HashMap usage in Java
    By neo_2010 in forum Collections and Generics
    Replies: 2
    Last Post: September 18th, 2009, 02:12 AM