gpt4 book ai didi

java - 更新 JLabel 文本

转载 作者:行者123 更新时间:2023-12-02 12:38:15 25 4
gpt4 key购买 nike

我正在开发一个简单的 GUI。按下按钮时,我想增加/减少变量并更新相应的 JLabel。

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

}

}

带有 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);

}

}

我知道,这是一个新问题。我认为我的代码结构是错误的。如有任何帮助,我们将不胜感激。

提前致谢。

最佳答案

您永远不会向按钮注册任何 ActionListener...

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

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

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

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

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

我重新编写了代码,因为你的例子扰乱了我的思想,希望你不要介意......

概念上是相同的想法,只是效率更高一些

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

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 TestPane() {
setLayout(new BorderLayout());
//Create GridPanel and set Layout
JPanel grid = new JPanel();
grid.setLayout(gridLayout);

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

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

heightIncrease.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
recHeight += 1;
height.setText(Integer.toString(recHeight));
}
});
heightDecrease.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
recHeight -= 1;
height.setText(Integer.toString(recHeight));
}
});

buttondraw.add(heightIncrease);
buttondraw.add(heightDecrease);

//Adding Components to GridPanel
//Adding Layouts to pane
add(grid, BorderLayout.NORTH);
add(new JSeparator(), BorderLayout.CENTER);
add(buttondraw, BorderLayout.SOUTH);
}

}
}

我鼓励您花一些时间看看 How to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listeners了解更多详情

关于java - 更新 JLabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45051810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com