Update JLabel text

时间:2017-07-12 08:01:25

标签: java jframe jlabel

I'm working on a simple GUI. On Button press i want to increase/decrease a variable and update the corresponding JLabel.

class JFrameSetUp

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


public class JFrameSetUp extends JFrame implements ActionListener {

    private int RecHeight = 0;
    private int RecWidth = 0;

    //Here Buttons

    JButton HeightIncrease = new JButton("+");
    JButton HeightDecrease = new JButton("-");

    JLabel height = new JLabel(Integer.toString(RecHeight));
    JLabel width = new JLabel(Integer.toString(RecWidth));

    GridLayout gridLayout = new GridLayout(2, 4);

    public JFrameSetUp(){

    }

    public void addComponentsToPane(final Container pane){

        //Create GridPanel and set Layout
        JPanel grid = new JPanel();
        grid.setLayout(gridLayout);

        //Create buttondrawPanel and set Layout
        JPanel buttondraw = new JPanel();
        buttondraw.setLayout(new GridLayout(2, 0));

        //Adding Components to GridPanel

        //Adding Layouts to pane

        pane.add(grid, BorderLayout.NORTH);
        pane.add(new JSeparator(), BorderLayout.CENTER);
        pane.add(buttondraw, BorderLayout.SOUTH);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        //Setting up ActionListener to Buttons

        if (e.getSource() == this.HeightDecrease) {

            RecHeight -= 1;
            height.setText(Integer.toString(RecHeight));

        } else if (e.getSource() == this.HeightIncrease) {

            RecHeight += 1;
            height.setText(Integer.toString(RecHeight));
        }

    }

}

Class with MainMethod

import javax.swing.JFrame;


public class Program {

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();

            }
        });
    }



    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrameSetUp frame = new JFrameSetUp();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set up the content pane.
        frame.addComponentsToPane(frame.getContentPane());
        //Display the window.
        frame.pack();
        frame.setVisible(true);

    }

}

I'm aware, that's kind a newbish question. I think I'm wrong with my Code Structure. Any help is appreciated.

Thanks in advance.

4 个答案:

答案 0 :(得分:1)

您永远不会在按钮上注册任何--- - hosts: localhost connection: local gather_facts: False vars: envs: - dev - prod - staging tasks: - name: set variable set_fact: denv: 'dev' - name: find dev debug: msg="Yay it's {{ item }}" with_items: "{{ envs }}" when: item == denv ...

ActionListener

您也永远不会将按钮添加到GUI

HeightIncrease.addActionListener(this);
HeightDecrease.addActionListener(this);

您也永远不会将标签添加到GUI ......

buttondraw.add(HeightIncrease);
buttondraw.add(HeightDecrease);

我重写了代码,因为你的例子弄乱了我的想法,希望你不要介意......

它在概念上是相同的想法,只是略微有效地完成了

grid.add(height);
grid.add(width);

我建议您花一些时间查看How to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listeners了解详情

答案 1 :(得分:0)

After changing the value call

frame.repaint();

答案 2 :(得分:0)

Good to see you learning Java! A few things I should point out.

Firstly, your variable names are good, but they don't follow the Java naming convention. Even though it seems small, it's just good practice to follow.

Of course, your actual problem; the action listener you've implemented is on the JFrame. (See how you extend JFrame and implement ActionListener?) This ActionListener should be on the button. You'll can do this a few ways.

Method 1: By adding it inline with your code

JButton heightButton = new JButton("Increase Height");
heightButton.addActionListener(new ActionListener(){
  @Override
  public void run(){
    //run method here
  }

});

Method 2: Create a class which implements ActionListener

class ButtonListener implements ActionListener{
  @Override
  public void run(){
    //actionListener code here
  }
}

And then instantiate an object of this type and add it directly to your code.

ActionListner buttonListener = new ButtonListener(); //or ButtonListener buttonListener = new ButtonListener();
JButton heightButton = new JButton("Increase Height");
heightButton.addActionListener(buttonListener);

Of course, as in MadProgrammers answer, don't forget to add the labels and such to your JFrame or JPanel. Good luck learning Java!

答案 3 :(得分:0)

我打赌你的程序什么也没有显示,不是吗?这是因为在addComponentsToPane方法中,您没有添加任何组件,只是空的JPanel。在评论//将组件添加到GridPanel后,您应该:

        buttondraw.add(HeightIncrease);
        buttondraw.add(HeightDecrease);
        grid.add(height);
        grid.add(width);

然后,要收听按钮事件,您还应添加:

        HeightIncrease.addActionListener(this);
        HeightDecrease.addActionListener(this);

“this”是因为你的框架JFrameSetUp实现了ActionListener,因此当单击bootton时,将调用方法actionPerformed。 由于JLabel.setText方法将重新绘制自身,因此其组件层次结构也被重新绘制,您不必做任何其他事情。