在用户有时间将数据输入到任何JTextField之前,您正在调用程序构造函数中的if块。如果您希望在程序运行期间发生此更改,则需要使用某种类型的侦听器,例如将ActionListener添加到JTextField。
关于你的这个说法:
的tf_type是在NetBeans
你不想做这个通过自定义代码设置为公共静态。不要让你的领域保持静态,以便你可以在没有实例的情况下在主体中访问它们。这将打破所有OOP原则,并使您的代码非常难以维护或更新。更好的方法是通过非静态公共方法更改实例的状态。
编辑:幽州
,这是从main_panel.java片段......在登录的JFrame此代码设置由Main_panel.tf_type.setText(txt_type.getText(用于tf_type值));仅供参考....登录后,会出现主面板...
我会用一个模式的JDialog,而不是一个JFrame登录,因为模态的JDialog会轻易让你知道什么时候已经完全处理与,我会通过调用它的公共方法,而不是通过使用静态字段来获取登录对话框的字段的状态。
编辑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);
}
}
我采取了值的JTextField从以前的JFrame。 ... – IOSDealBreaker 2013-05-12 02:38:41
@MohammedZameer:没有更多上下文语句,''我从之前的jframe中获取jtextfield的值....“,除了您可能使用多个JFrame之外,对我们来说没有多大意义,这对于我们来说通常也不是一件好事做。 – 2013-05-12 02:39:49
这是来自main_panel.java的片段...在登录jframe中,此代码通过Main_panel.tf_type.setText(txt_type.getText())设置tf_type的值; fyi ....登录后,主面板出现... – IOSDealBreaker 2013-05-12 02:42:03