gpt4 book ai didi

java - 如果语句不适用于 JTextfield 到字符串的比较

转载 作者:行者123 更新时间:2023-11-29 05:43:51 26 4
gpt4 key购买 nike

这是我的代码

public Main_panel() {
initComponents();
setLocationRelativeTo(null);
tf_type.setVisible(false);
String normal = tf_type.getText();
String ntext = "normal";
if(normal.equals(ntext)) {
cmb_report.setVisible(false);
cmb_cu.setVisible(false);
}

至于其他信息,通过 netbeans 中的自定义代码将 tf_type 设置为 public static。但是 cmb_reports 和 cmb_cu 并没有变得不可见,即 if 语句没有被执行。这是为什么?

最佳答案

在用户有时间将数据输入任何 JTextField 之前,您将在程序的构造函数中调用 if block 。如果您希望在程序运行期间发生此更改,则需要使用某种监听器,例如将 ActionListener 添加到 JTextField。

关于您的声明:

the tf_type is set to public static via customize code in netbeans

你不想这样做。不要让你的字段只是静态的,这样你就可以在没有实例的情况下在 main 中访问它们。这将打破所有 OOP 原则,并使您的代码很难维护或更新。更好的方法是通过非静态公共(public)方法更改实例的状态。


编辑您声明

this was snippet from main_panel.java...in login jframe this code sets the value for tf_type by Main_panel.tf_type.setText(txt_type.getText()); fyi....after logged in, main panel appears...

我会使用模态 JDialog 而不是 JFrame 来登录,因为模态 JDialog 很容易让您知道它何时被完全处理,并且我会通过调用来获取登录对话框字段的状态公共(public)方法,而不是使用静态字段。


编辑2:例如,

import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class LogInDialogTest {

private static void createAndShowGui() {
JTextField textField1 = new JTextField(10);
textField1.setEditable(false);
textField1.setFocusable(false);

JPanel mainPanel = new JPanel();
mainPanel.add(textField1);
mainPanel.add(new JButton(new AbstractAction("Exit") {

@Override
public void actionPerformed(ActionEvent evt) {
JButton thisBtn = (JButton) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(thisBtn);
win.dispose();
}
}));

JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
// frame.setVisible(true);

JTextField textField2 = new JTextField(10);
JPanel mainPanel2 = new JPanel();
mainPanel2.add(textField2);
mainPanel2.add(new JButton(new AbstractAction("Submit") {

@Override
public void actionPerformed(ActionEvent evt) {
JButton thisBtn = (JButton) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(thisBtn);
win.dispose();
}
}));
JDialog dialog = new JDialog(frame, "Dialog", ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(mainPanel2);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);

textField1.setText(textField2.getText());
frame.setVisible(true);

}

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

编辑 3: 一个更好的例子,

import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class LogInDialogTest {

private static void createAndShowGui() {
final MainJPanel mainPanel = new MainJPanel();

JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
// frame.setVisible(true);

LoginJPanel loginPanel = new LoginJPanel();
JDialog dialog = new JDialog(frame, "Dialog",
ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(loginPanel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);

mainPanel.textFieldSetText(loginPanel.textFieldGetText());
frame.setVisible(true);

}

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

class LoginJPanel extends JPanel {
private JTextField textField = new JTextField(10);

public LoginJPanel() {
add(textField);
add(new JButton(new AbstractAction("Submit") {

@Override
public void actionPerformed(ActionEvent evt) {
JButton thisBtn = (JButton) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(thisBtn);
win.dispose();
}
}));
}

public String textFieldGetText() {
return textField.getText();
}
}

class MainJPanel extends JPanel {
private JTextField textField = new JTextField(10);

public MainJPanel() {
textField.setEditable(false);
textField.setFocusable(false);
add(textField);
add(new JButton(new AbstractAction("Exit") {

@Override
public void actionPerformed(ActionEvent evt) {
JButton thisBtn = (JButton) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(thisBtn);
win.dispose();
}
}));
}

public void textFieldSetText(String text) {
textField.setText(text);
}
}

关于java - 如果语句不适用于 JTextfield 到字符串的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16503657/

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