[英]Update JLabel text
我正在使用一個簡單的GUI。 在按Button時,我要增加/減少變量並更新相應的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);
}
}
}
我鼓勵您花一些時間查看如何使用按鈕,復選框和單選按鈕以及如何編寫動作偵聽器以了解更多詳細信息。
更改值后
frame.repaint();
很高興看到您學習Java! 我應該指出幾件事。
首先,您的變量名很好,但是它們不遵循Java命名約定 。 盡管它看起來很小,但是遵循它只是一個好習慣。
當然是您的實際問題; 您已實現的動作偵聽器在JFrame上。 (請參見如何擴展JFrame並實現ActionListener?)此ActionListener應該位於按鈕上。 您可以通過幾種方法來做到這一點。
方法1:通過與您的代碼內聯添加
JButton heightButton = new JButton("Increase Height");
heightButton.addActionListener(new ActionListener(){
@Override
public void run(){
//run method here
}
});
方法2:創建一個實現ActionListener的類
class ButtonListener implements ActionListener{
@Override
public void run(){
//actionListener code here
}
}
然后實例化此類型的對象,並將其直接添加到您的代碼中。
ActionListner buttonListener = new ButtonListener(); //or ButtonListener buttonListener = new ButtonListener();
JButton heightButton = new JButton("Increase Height");
heightButton.addActionListener(buttonListener);
當然,正如MadProgrammers回答那樣,不要忘記將標簽等添加到您的JFrame或JPanel中。 祝您學習Java順利!
我敢打賭,您的程序什么也不顯示,不是嗎? 這是因為在addComponentsToPane方法中,除了空的JPanels之外,您沒有添加任何組件。 在注釋//將組件添加到GridPanel之后,您應該:
buttondraw.add(HeightIncrease);
buttondraw.add(HeightDecrease);
grid.add(height);
grid.add(width);
然后,要監聽按鈕事件,還應該添加:
HeightIncrease.addActionListener(this);
HeightDecrease.addActionListener(this);
之所以如此,是因為您的框架JFrameSetUp實現了ActionListener,因此,當單擊任一啟動子時,都會調用actionPerformed方法。 由於JLabel.setText方法將重新繪制自身,因此其組件層次結構也將重新繪制,因此您無需執行任何其他操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.