gpt4 book ai didi

java - 使用 swing、JButtons 等在程序上显示信息时出现问题

转载 作者:行者123 更新时间:2023-12-01 23:10:37 24 4
gpt4 key购买 nike

我正在使用 swing 制作一个乘法表。它基本上由 JButton 组成。该表是根据用户的输入形成的。用户通过输入数字来选择表格的大小。我需要做的最后一件事是创建一个标题,显示创建的表的编号。这是我的示例代码,如果您运行它,您会看到它针对垂直数字完成了。我怎样才能得到上面的数字并正确格式化以代表每一列。谢谢。

package lab7;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUIMultiplicationTable{
JFrame theFrame;
int number = 0;
JPanel panel, answerPanel, topPanel, leftPanel;
JLabel answerLabel, topLabel, leftLabel;

private void createAndShowGui(){
String x;
do{
x = JOptionPane.showInputDialog(null, "Enter the number");
number = Integer.parseInt(x);
}while (number <= 0);
theFrame = new JFrame("Multiplication Table");
panel = new JPanel(new GridLayout(number, number));

answerPanel = new JPanel();
answerLabel = new JLabel();

topPanel = new JPanel();
topLabel = new JLabel();

leftPanel = new JPanel();
leftLabel = new JLabel();


for (int i = 0; i < number; i++){
JLabel blah = new JLabel(Integer.toString(i + 1));
panel.add(blah);//add center to label
for (int j = 0; j < number; j++){

JButton button = new JButton();
if (i == 0){

button.setText(String.valueOf(j + 1));
}
if (j == 0){
button.setText(String.valueOf(i + 1));
}
for (int k = 1; k < number; k++)
{
if (i == k)
{
button.setText(String.valueOf((j + 1) * (k + 1)));
}
}
button.addActionListener(new ButtonsTableActionListener(i, j));
panel.add(button);
}
}

answerPanel.add(answerLabel);
theFrame.add(answerPanel, BorderLayout.SOUTH);

topPanel.add(topLabel);
theFrame.add(topPanel, BorderLayout.NORTH);

theFrame.add(panel);
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theFrame.pack();
theFrame.setLocationRelativeTo(null);
theFrame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
GUIMultiplicationTable h = new GUIMultiplicationTable();
h.createAndShowGui();
}
});
}

private class ButtonsTableActionListener implements ActionListener{
private int theRow, theColumn;

public ButtonsTableActionListener(int row, int column){
theRow = row;
theColumn = column;
}

@Override
public void actionPerformed(ActionEvent e){
int value = (theRow + 1) * (theColumn + 1);
answerLabel.setText("The value is: " + value + ".\nI got that by multiplying \n" + (theRow + 1) + "x" + (theColumn + 1));
}
};
}

最佳答案

实现此目的的一个简单方法是将按钮的位置存储在 ActionListener 中,您可以通过创建自己的类扩展 ActionListener 来实现此目的,而不是这样做一个匿名类。这样,按钮执行的代码就已经拥有完成您想要的任何操作所需的信息。

而且你也不需要按钮数组,只需在面板中一次添加一个按钮,同时添加actionListener。

这是您已清理并正常工作的代码。现在,您可以做任何您想做的事情,而不是显示对话框。

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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUIMultiplicationTable
{
JFrame theFrame;
int number = 0;
JPanel panel;

private void createAndShowGui()
{
String x;
do
{
x = JOptionPane.showInputDialog(null, "Enter the number");
number = Integer.parseInt(x);
} while (number <= 0);
theFrame = new JFrame("Multiplication Table");
panel = new JPanel(new GridLayout(number, number));
for (int i = 0; i < number; i++)
{
for (int j = 0; j < number; j++)
{
JButton button = new JButton();
if (i == 0)
{
button.setText(String.valueOf(j + 1));
}
if (j == 0)
{
button.setText(String.valueOf(i + 1));
}
for (int k = 1; k < number; k++)
{
if (i == k)
{
button.setText(String.valueOf((j + 1) * (k + 1)));
}
}
button.addActionListener(new ButtonsTableActionListener(i, j));
panel.add(button);
}
}
theFrame.add(panel);
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theFrame.pack();
theFrame.setLocationRelativeTo(null);
theFrame.setVisible(true);

}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
GUIMultiplicationTable h = new GUIMultiplicationTable();
h.createAndShowGui();
}
});

}

private class ButtonsTableActionListener implements ActionListener
{
private int _row, _column;

public ButtonsTableActionListener(int row, int column)
{
_row = row;
_column = column;
}

@Override
public void actionPerformed(ActionEvent e)
{
// /do something
int value = (_row + 1) * (_column + 1);
String message = "I'm the button in the position (" + _row + ", " + _column + ")\nMy value is " + value + " = " + (_row + 1) + "*" + (_column + 1);
JOptionPane.showMessageDialog(theFrame, message);
}
};
}

关于java - 使用 swing、JButtons 等在程序上显示信息时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22083429/

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