简体   繁体   中英

Java - updating values in JFrame/JLabels

I am a beginner Java-coder and a few days ago I felt confident enough in my skills to start my first "big" project. It was basically a calculator, a GUI(only JFrame, JPanels, JLabels and Buttons) that would display data, accept user input, grab some more data from other classes, then calculate stuff and finally update the GUI with the new JLabel values. However I never managed to get the update part done properly, whenever I would press the 'process'-button it would create a new JFrame with the new values, while the old one was still up.

I tried the obvious stuff (repaint(), revalidate(), etc) but that didn't work at all, then I started to shift things around, put parts of the code into new classes, copied code from the net until it eventually worked. However the code was a total mess and I didn't even really understand what went exactly wrong in the first place, so I trashed the entire thing.

Here is a very simplified version of my code before things went downhill:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test_1 extends JFrame {
    public static class clicks{
    static int clicks = 0;
    public int getclicks(){
        return clicks;
    }
    public void setclicks(){
        clicks = clicks+1;
    }
}

public Test_1(){
    clicks getNumber = new clicks();
    int x = getNumber.getclicks();

    //FRAME AND LAYOUT
    JFrame window = new JFrame();
    window.getContentPane().setBackground(Color.darkGray);
    window.getContentPane().setLayout(new BorderLayout(20,10));
    window.setTitle("Test Frame 1");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setResizable(true);



    // Top JPanel
    JPanel northpanel = new JPanel();
    LayoutManager northlayout = new FlowLayout();
    northpanel.setLayout(northlayout);
    // Top JPanel content
    JLabel nlabel1 = new JLabel("Hello North");
    nlabel1.setPreferredSize(new Dimension(100,20));
    northpanel.add(nlabel1);

    JPanel westpanel = new JPanel();
    LayoutManager westlayout = new BoxLayout(westpanel, BoxLayout.Y_AXIS);
    westpanel.setLayout(westlayout);
    JLabel wlabel1 = new JLabel("Hello West");
    wlabel1.setPreferredSize(new Dimension(100,20));
    westpanel.add(wlabel1);

    JPanel eastpanel = new JPanel();
    LayoutManager eastlayout = new BoxLayout(eastpanel, BoxLayout.Y_AXIS);
    eastpanel.setLayout(eastlayout);
    JLabel elabel1 = new JLabel ("Hello East");
    elabel1.setPreferredSize(new Dimension(100,20));
    eastpanel.add(elabel1);

    JButton southbutton = new JButton("start");
    southbutton.setPreferredSize(new Dimension(400,50));
    southbutton.addActionListener(new Action());

    JPanel centralpanel = new JPanel();
    JLabel clabel1 = new JLabel("Clicks: " + x);
    centralpanel.add(clabel1);

    window.add(centralpanel, BorderLayout.CENTER);
    window.add(southbutton, BorderLayout.SOUTH);
    window.add(eastpanel, BorderLayout.EAST);
    window.add(westpanel, BorderLayout.WEST);
    window.add(northpanel, BorderLayout.NORTH);
    window.pack();
    window.setVisible(true);        
}


public static void main(String[] args) {
    Test_1 window_start = new Test_1();
}


static class Action implements ActionListener{  
    @Override
    public void actionPerformed (ActionEvent e){
        clicks Numbers = new clicks();
        Numbers.setclicks();
        int test = Numbers.getclicks();
        System.out.println("Button works, Number of clicks: "+test);
        Test_1 updateData = new Test_1();
    }
}
}

I know that the ActionListener creates a new instance of my JFrame, however that was the closest I ever came to "updating the JFrame" before I turned the code into Spaghetti. I assume that the way I build my code is the cause of my problem but creating the Frame and its content it different classes didn't work at all.

So my questions are:

  1. Is there something really obvious I missing? Would it be possible to make this run the way I want to without completely changing it?

  2. Is there a more efficient way to create a GUI? I get the feeling that the way I made this is total garbage.

I read other questions that dealt with similar problems but maybe it's because I am still pretty bad at Java but I couldn't really tell if they were related to my problem. Also I really want to understand this, so copying someone elses code wouldn't help at all.

Any help or comments are appreciated.

btw, the class click is something I just put there as a placeholder.

Alrighty I managed to get it to work. It's probably against the Etiquette to answer to his own question but I thought it might be useful for some beginners(like me yesterday). So here is my new code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test_1 extends JFrame {

public static class clicks{
    static int clicks = 0;
    public int getclicks(){
        return clicks;
    }
    public void setclicks(){
        clicks = clicks+1;
    }
}

clicks getNumber = new clicks();
int x = getNumber.getclicks();
JPanel northpanel, westpanel, eastpanel, southpanel, centralpanel;
static JLabel nlabel1, nlabel2, nlabel3, nlabel4, nlabel5;
static JLabel wlabel1, wlabel2, wlabel3, wlabel4, wlabel5;
static JLabel elabel1, elabel2, elabel3, elabel4, elabel5; 
static JLabel clabel1;
JButton southbutton;
String TextnL, TextwL, TexteL;


public Test_1(){
    setBackground(Color.darkGray);
    setLayout(new BorderLayout(20,10));
    setTitle("Test Frame 1");
    setSize(300,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(true);
    setLocationRelativeTo(null);
    setVisible(true);

    nlabel1 = new JLabel("North_1");
    nlabel2 = new JLabel("North_2");
    nlabel3 = new JLabel("North_3");
    nlabel4 = new JLabel("North_4");
    nlabel5 = new JLabel("North_5");

    wlabel1 = new JLabel("West_1 ");
    wlabel2 = new JLabel("West_2 ");
    wlabel3 = new JLabel("West_3 ");
    wlabel4 = new JLabel("West_4 ");
    wlabel5 = new JLabel("West_5 ");

    elabel1 = new JLabel("East_1");
    elabel2 = new JLabel("East_2");
    elabel3 = new JLabel("East_3");
    elabel4 = new JLabel("East_4");
    elabel5 = new JLabel("East_5");

    clabel1 = new JLabel("START");

    southbutton = new JButton("Process");
    southbutton.addActionListener(new Action());

    northpanel = new JPanel();
    northpanel.add(nlabel1);
    northpanel.add(nlabel2);
    northpanel.add(nlabel3);
    northpanel.add(nlabel4);
    northpanel.add(nlabel5);
    add(northpanel, BorderLayout.NORTH);

    westpanel = new JPanel();
    LayoutManager wBox = new BoxLayout(westpanel, BoxLayout.Y_AXIS);
    westpanel.setLayout(wBox);
    westpanel.add(wlabel1);
    westpanel.add(wlabel2);
    westpanel.add(wlabel3);
    westpanel.add(wlabel4);
    westpanel.add(wlabel5);
    add(westpanel, BorderLayout.WEST);

    eastpanel = new JPanel();
    LayoutManager eBox = new BoxLayout(eastpanel, BoxLayout.Y_AXIS);
    eastpanel.setLayout(eBox);
    eastpanel.add(elabel1);
    eastpanel.add(elabel2);
    eastpanel.add(elabel3);
    eastpanel.add(elabel4);
    eastpanel.add(elabel5);
    add(eastpanel, BorderLayout.EAST);

    centralpanel = new JPanel();
    centralpanel.add(clabel1);
    add(centralpanel, BorderLayout.CENTER);
    add(southbutton, BorderLayout.SOUTH);
}



public static void main(String[] args) {
    Test_1 window_start = new Test_1();
}


static class Action implements ActionListener{  
    @Override
    public void actionPerformed (ActionEvent e){
        clicks Numbers = new clicks();
        Numbers.setclicks();
        int test = Numbers.getclicks();
        clabel1.setText("clicks: "+test);
    }
}
}

And again, any comments/suggestions are welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM