gpt4 book ai didi

java - 根据另一个类的组合框中的所选项目启用标签和文本字段

转载 作者:行者123 更新时间:2023-12-01 10:52:35 25 4
gpt4 key购买 nike

我有两个独立的类:SubDimensions.java 和 Subtraction.java。

我想根据 SubDimensions 类的组合框中的所选项目启用 Subtraction 类中的某些组件。我尝试在 SubDimensions 类中创建 switch 语句并在 Subtraction 类中创建 boolean 函数,但它不起作用。我想知道可能是什么问题。

SubDimensions.java

package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;

public class SubDimensions {

public JFrame frmSubDimensions;
public JComboBox comboBoxA;

/**
* Launch the application.
*/
public static void SubDimensions() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SubDimensions window = new SubDimensions();
window.frmSubDimensions.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public SubDimensions() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
public void initialize() {
frmSubDimensions = new JFrame();
frmSubDimensions.setTitle("VECTOR CALCULATOR");
frmSubDimensions.setBounds(100, 100, 330, 200);
frmSubDimensions.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmSubDimensions.getContentPane().setLayout(null);

JLabel lblVectorDimension = new JLabel("Vector dimensions:");
lblVectorDimension.setHorizontalAlignment(SwingConstants.CENTER);
lblVectorDimension.setBounds(10, 21, 294, 14);
frmSubDimensions.getContentPane().add(lblVectorDimension);

comboBoxA = new JComboBox();
comboBoxA.setModel(new DefaultComboBoxModel(new String[] {"2x1", "3x1", "4x1", "5x1", "6x1", "7x1", "8x1", "9x1", "10x1"}));
comboBoxA.setBounds(110, 46, 95, 20);
frmSubDimensions.getContentPane().add(comboBoxA);

JButton btnNewButton = new JButton("Okay");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Subtraction subtraction = new Subtraction();
subtraction.Subtraction();
frmSubDimensions.setVisible(false);

switch(comboBoxA.getSelectedItem().toString()){
case "3x1":{
subtraction.x3Visibility(true);
break;}
/*case "4x1":
lblX4.setEnabled(true);
break;*/
default:{
subtraction.x3Visibility(false);
break;}
}
}
});
btnNewButton.setBounds(62, 108, 89, 23);
frmSubDimensions.getContentPane().add(btnNewButton);

JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(167, 108, 89, 23);
frmSubDimensions.getContentPane().add(btnCancel);
}
}

减法.java

package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

public class Subtraction {

public JFrame frmSubtraction;
public JTextField textFieldx1;
public JTextField textFieldx2;
public JTextField textFieldx3;
public JTextField textFieldx4;
public JTextField textFieldx5;
public JTextField textFieldx6;
public JTextField textFieldx7;
public JTextField textFieldx8;
public JTextField textFieldx9;
public JTextField textFieldx10;
public JTextField textFieldy1;
public JTextField textFieldy2;
public JTextField textFieldy3;
public JTextField textFieldy4;
public JTextField textFieldy5;
public JTextField textFieldy6;
public JTextField textFieldy7;
public JTextField textFieldy8;
public JTextField textFieldy9;
public JTextField textFieldy10;
public JLabel lblX1;
public JLabel lblX2;
public JLabel lblX3;
public JLabel lblX4;
public JLabel lblX5;
public JLabel lblX6;
public JLabel lblX7;
public JLabel lblX8;
public JLabel lblX9;
public JLabel lblX10;
public JLabel lblY1;
public JLabel lblY2;
public JLabel lblY3;
public JLabel lblY4;
public JLabel lblY5;
public JLabel lblY6;
public JLabel lblY7;
public JLabel lblY8;
public JLabel lblY9;
public JLabel lblY10;

/**
* Launch the application.
*/
public static void Subtraction() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Subtraction window = new Subtraction();
window.frmSubtraction.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Subtraction() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
public void initialize(){
SubDimensions subDimensions = new SubDimensions();

frmSubtraction = new JFrame();
frmSubtraction.setTitle("VECTOR CALCULATOR");
frmSubtraction.setBounds(100, 100, 400, 400);
frmSubtraction.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmSubtraction.getContentPane().setLayout(null);

lblX1 = new JLabel("x1");
lblX1.setBounds(10, 32, 20, 14);
frmSubtraction.getContentPane().add(lblX1);

textFieldx1 = new JTextField();
textFieldx1.setBounds(40, 29, 50, 20);
frmSubtraction.getContentPane().add(textFieldx1);
textFieldx1.setColumns(10);

lblX2 = new JLabel("x2");
lblX2.setBounds(10, 60, 20, 14);
frmSubtraction.getContentPane().add(lblX2);

textFieldx2 = new JTextField();
textFieldx2.setColumns(10);
textFieldx2.setBounds(40, 57, 50, 20);
frmSubtraction.getContentPane().add(textFieldx2);

lblX3 = new JLabel("x3");
lblX3.setEnabled(false);
lblX3.setBounds(10, 88, 20, 14);
frmSubtraction.getContentPane().add(lblX3);

textFieldx3 = new JTextField();
textFieldx3.setEnabled(false);
textFieldx3.setColumns(10);
textFieldx3.setBounds(40, 85, 50, 20);
frmSubtraction.getContentPane().add(textFieldx3);

lblX4 = new JLabel("x4");
lblX4.setEnabled(false);
lblX4.setBounds(10, 116, 20, 14);
frmSubtraction.getContentPane().add(lblX4);

textFieldx4 = new JTextField();
textFieldx4.setEnabled(false);
textFieldx4.setColumns(10);
textFieldx4.setBounds(40, 113, 50, 20);
frmSubtraction.getContentPane().add(textFieldx4);

lblX5 = new JLabel("x5");
lblX5.setEnabled(false);
lblX5.setBounds(10, 144, 20, 14);
frmSubtraction.getContentPane().add(lblX5);

textFieldx5 = new JTextField();
textFieldx5.setEnabled(false);
textFieldx5.setColumns(10);
textFieldx5.setBounds(40, 141, 50, 20);
frmSubtraction.getContentPane().add(textFieldx5);

lblX6 = new JLabel("x6");
lblX6.setEnabled(false);
lblX6.setBounds(10, 172, 20, 14);
frmSubtraction.getContentPane().add(lblX6);

textFieldx6 = new JTextField();
textFieldx6.setEnabled(false);
textFieldx6.setColumns(10);
textFieldx6.setBounds(40, 169, 50, 20);
frmSubtraction.getContentPane().add(textFieldx6);

lblX7 = new JLabel("x7");
lblX7.setEnabled(false);
lblX7.setBounds(10, 200, 20, 14);
frmSubtraction.getContentPane().add(lblX7);

textFieldx7 = new JTextField();
textFieldx7.setEnabled(false);
textFieldx7.setColumns(10);
textFieldx7.setBounds(40, 197, 50, 20);
frmSubtraction.getContentPane().add(textFieldx7);

lblX8 = new JLabel("x8");
lblX8.setEnabled(false);
lblX8.setBounds(10, 228, 20, 14);
frmSubtraction.getContentPane().add(lblX8);

textFieldx8 = new JTextField();
textFieldx8.setEnabled(false);
textFieldx8.setColumns(10);
textFieldx8.setBounds(40, 225, 50, 20);
frmSubtraction.getContentPane().add(textFieldx8);

lblX9 = new JLabel("x9");
lblX9.setEnabled(false);
lblX9.setBounds(10, 256, 20, 14);
frmSubtraction.getContentPane().add(lblX9);

textFieldx9 = new JTextField();
textFieldx9.setEnabled(false);
textFieldx9.setColumns(10);
textFieldx9.setBounds(40, 253, 50, 20);
frmSubtraction.getContentPane().add(textFieldx9);

lblX10 = new JLabel("x10");
lblX10.setEnabled(false);
lblX10.setBounds(10, 281, 30, 14);
frmSubtraction.getContentPane().add(lblX10);

textFieldx10 = new JTextField();
textFieldx10.setEnabled(false);
textFieldx10.setColumns(10);
textFieldx10.setBounds(40, 281, 50, 20);
frmSubtraction.getContentPane().add(textFieldx10);

lblY1 = new JLabel("y1");
lblY1.setBounds(126, 32, 20, 14);
frmSubtraction.getContentPane().add(lblY1);

textFieldy1 = new JTextField();
textFieldy1.setColumns(10);
textFieldy1.setBounds(156, 29, 50, 20);
frmSubtraction.getContentPane().add(textFieldy1);

lblY2 = new JLabel("y2");
lblY2.setBounds(126, 60, 20, 14);
frmSubtraction.getContentPane().add(lblY2);

textFieldy2 = new JTextField();
textFieldy2.setColumns(10);
textFieldy2.setBounds(156, 57, 50, 20);
frmSubtraction.getContentPane().add(textFieldy2);

textFieldy3 = new JTextField();
textFieldy3.setEnabled(false);
textFieldy3.setColumns(10);
textFieldy3.setBounds(156, 85, 50, 20);
frmSubtraction.getContentPane().add(textFieldy3);

lblY3 = new JLabel("y3");
lblY3.setEnabled(false);
lblY3.setBounds(126, 88, 20, 14);
frmSubtraction.getContentPane().add(lblY3);

lblY4 = new JLabel("y4");
lblY4.setEnabled(false);
lblY4.setBounds(126, 116, 20, 14);
frmSubtraction.getContentPane().add(lblY4);

textFieldy4 = new JTextField();
textFieldy4.setEnabled(false);
textFieldy4.setColumns(10);
textFieldy4.setBounds(156, 113, 50, 20);
frmSubtraction.getContentPane().add(textFieldy4);

textFieldy5 = new JTextField();
textFieldy5.setEnabled(false);
textFieldy5.setColumns(10);
textFieldy5.setBounds(156, 141, 50, 20);
frmSubtraction.getContentPane().add(textFieldy5);

lblY5 = new JLabel("y5");
lblY5.setEnabled(false);
lblY5.setBounds(126, 144, 20, 14);
frmSubtraction.getContentPane().add(lblY5);

lblY6 = new JLabel("y6");
lblY6.setEnabled(false);
lblY6.setBounds(126, 172, 20, 14);
frmSubtraction.getContentPane().add(lblY6);

textFieldy6 = new JTextField();
textFieldy6.setEnabled(false);
textFieldy6.setColumns(10);
textFieldy6.setBounds(156, 169, 50, 20);
frmSubtraction.getContentPane().add(textFieldy6);

textFieldy7 = new JTextField();
textFieldy7.setEnabled(false);
textFieldy7.setColumns(10);
textFieldy7.setBounds(156, 197, 50, 20);
frmSubtraction.getContentPane().add(textFieldy7);

lblY7 = new JLabel("y7");
lblY7.setEnabled(false);
lblY7.setBounds(126, 200, 20, 14);
frmSubtraction.getContentPane().add(lblY7);

lblY8 = new JLabel("y8");
lblY8.setEnabled(false);
lblY8.setBounds(126, 228, 20, 14);
frmSubtraction.getContentPane().add(lblY8);

textFieldy8 = new JTextField();
textFieldy8.setEnabled(false);
textFieldy8.setColumns(10);
textFieldy8.setBounds(156, 225, 50, 20);
frmSubtraction.getContentPane().add(textFieldy8);

lblY9 = new JLabel("y9");
lblY9.setEnabled(false);
lblY9.setBounds(126, 256, 20, 14);
frmSubtraction.getContentPane().add(lblY9);

textFieldy9 = new JTextField();
textFieldy9.setEnabled(false);
textFieldy9.setColumns(10);
textFieldy9.setBounds(156, 253, 50, 20);
frmSubtraction.getContentPane().add(textFieldy9);

textFieldy10 = new JTextField();
textFieldy10.setEnabled(false);
textFieldy10.setColumns(10);
textFieldy10.setBounds(156, 281, 50, 20);
frmSubtraction.getContentPane().add(textFieldy10);

lblY10 = new JLabel("y10");
lblY10.setEnabled(false);
lblY10.setBounds(126, 281, 30, 14);
frmSubtraction.getContentPane().add(lblY10);

JLabel lblVectorU = new JLabel("Vector u:");
lblVectorU.setBounds(10, 11, 80, 14);
frmSubtraction.getContentPane().add(lblVectorU);

JLabel lblVectorV = new JLabel("Vector v:");
lblVectorV.setBounds(126, 11, 80, 14);
frmSubtraction.getContentPane().add(lblVectorV);

JTextArea textAreaZ = new JTextArea();
textAreaZ.setEditable(false);
textAreaZ.setBounds(251, 27, 123, 274);
frmSubtraction.getContentPane().add(textAreaZ);

JButton btnNewButton = new JButton("Calculate");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x1 = Integer.parseInt(textFieldx1.getText());
int x2 = Integer.parseInt(textFieldx2.getText());
int x3 = Integer.parseInt(textFieldx3.getText());
int x4 = Integer.parseInt(textFieldx4.getText());
int x5 = Integer.parseInt(textFieldx5.getText());
int x6 = Integer.parseInt(textFieldx6.getText());
int x7 = Integer.parseInt(textFieldx7.getText());
int x8 = Integer.parseInt(textFieldx8.getText());
int x9 = Integer.parseInt(textFieldx9.getText());
int x10 = Integer.parseInt(textFieldx10.getText());

int y1 = Integer.parseInt(textFieldy1.getText());
int y2 = Integer.parseInt(textFieldy2.getText());
int y3 = Integer.parseInt(textFieldy3.getText());
int y4 = Integer.parseInt(textFieldy4.getText());
int y5 = Integer.parseInt(textFieldy5.getText());
int y6 = Integer.parseInt(textFieldy6.getText());
int y7 = Integer.parseInt(textFieldy7.getText());
int y8 = Integer.parseInt(textFieldy8.getText());
int y9 = Integer.parseInt(textFieldy9.getText());
int y10 = Integer.parseInt(textFieldy10.getText());

int z1 = x1 - y1;
int z2 = x2 - y2;
int z3 = x3 - y3;
int z4 = x4 - y4;
int z5 = x5 - y5;
int z6 = x6 - y6;
int z7 = x7 - y7;
int z8 = x8 - y8;
int z9 = x9 - y9;
int z10 = x10 - y10;

textAreaZ.setText(Integer.toString(z1) + "\n" + Integer.toString(z2) + "\n" + Integer.toString(z3) + "\n" + Integer.toString(z4) + "\n" + Integer.toString(z5) + "\n" + Integer.toString(z6) + "\n" + Integer.toString(z7) + "\n" + Integer.toString(z8) + "\n" + Integer.toString(z9) + "\n" + Integer.toString(z10));

}
});

btnNewButton.setBounds(87, 327, 89, 23);
frmSubtraction.getContentPane().add(btnNewButton);

JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
subDimensions.SubDimensions();
frmSubtraction.setVisible(false);
}
});
btnCancel.setBounds(208, 327, 89, 23);
frmSubtraction.getContentPane().add(btnCancel);

JLabel lblVectorUv = new JLabel("Vector u+v:");
lblVectorUv.setBounds(251, 11, 123, 14);
frmSubtraction.getContentPane().add(lblVectorUv);


}

public void x3Visibility (boolean enabled){
lblX3.setEnabled(enabled);
textFieldx3.setEnabled(enabled);
lblY3.setEnabled(enabled);
textFieldy3.setEnabled(enabled);
}
/*public void x4Visibility (boolean visibility){
lblX4.setEnabled(true);
textFieldx4.setEnabled(true);
lblY4.setEnabled(true);
textFieldy4.setEnabled(true);
}*/
}

最佳答案

您没有在 Subtraction.java 中使用参数 enabled,请更改为:

public void x3Visibility (boolean enabled){
lblX3.setEnabled(enabled);
textFieldx3.setEnabled(enabled);
lblY3.setEnabled(enabled);
textFieldy3.setEnabled(enabled);
}

编辑:看来您缺乏对面向对象编程(OOP)的基本了解。

当你打电话时

Subtraction subtraction = new Subtraction();
subtraction.Subtraction();

您创建了另一个名为 window 的 Subtraction 实例:

public static void Subtraction() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Subtraction window = new Subtraction();
window.frmSubtraction.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

减法不可见,而窗口可见。但 window 不在 actionPerformed 的范围内,因此您无法访问它。目前,您可以将 actionPerformed 更改为以下内容来暂时解决您的问题。但我强烈建议您:1)重新审视 OOP 的概念2)尝试为标签和文本字段使用数组或集合,而不是一一硬编码它们

public void actionPerformed(ActionEvent e) {
Subtraction subtraction = new Subtraction();
subtraction.frmSubtraction.setVisible(true);
frmSubDimensions.setVisible(false);

switch(comboBoxA.getSelectedItem().toString()){
case "3x1":{
subtraction.x3Visibility(true);
break;}
/*case "4x1":
lblX4.setEnabled(true);
break;*/
default:{
subtraction.x3Visibility(false);
break;}
}
}

关于java - 根据另一个类的组合框中的所选项目启用标签和文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33757863/

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