繁体   English   中英

Java-更新JFrame / JLabels中的值

[英]Java - updating values in JFrame/JLabels

我是一名Java初学者,几天前,我对自己的技能充满信心,可以开始我的第一个“大”项目。 它基本上是一个计算器,一个GUI(仅JFrame,JPanel,JLabel和Buttons),它将显示数据,接受用户输入,从其他类中获取更多数据,然后计算内容并最终使用新的JLabel值更新GUI。 但是,我从来没有设法正确完成更新部分,每当我按“处理”按钮时,它都会使用新值创建一个新的JFrame,而旧的JFrame仍在运行。

我尝试了明显的东西(repaint(),revalidate()等),但那根本不起作用,然后我开始四处移动东西,将部分代码放入新类中,从网上复制代码,直到最终工作了。 但是代码完全是一团糟,我什至根本不了解到底出了什么问题,所以我把整个事情都弄糟了。

这是我的代码在简化之前的一个非常简化的版本:

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();
    }
}
}

我知道ActionListener会创建我的JFrame的新实例,但这是我将代码转换为Spaghetti之前最接近“更新JFrame”的实例。 我认为构建代码的方式是造成问题的原因,但是创建框架及其内容以及不同的类根本没有用。

所以我的问题是:

  1. 我真的有什么明显的缺失吗? 无需完全更改就可以按照我想要的方式运行它吗?

  2. 有没有更有效的方式来创建GUI? 我感觉到,我做这件事的方式是完全垃圾。

我读过其他类似问题的问题,但这也许是因为我对Java的了解仍然很差,但我无法真正确定它们是否与我的问题有关。 我也很想理解这一点,所以复制别人的代码根本没有帮助。

任何帮助或评论表示赞赏。

顺便说一句,我只是将类单击作为占位符放在那里。

好吧,我设法使其正常工作。 回答他自己的问题可能与礼节相反,但我认为这对于某些初学者(如昨天的我)可能很有用。 所以这是我的新代码:

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);
    }
}
}

同样,欢迎提出任何意见/建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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