有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何重用jframe而不使其成为新的?

我想知道是否有一种方法可以重用jframe而不使其成为新的。你可能想知道为什么。我有两个JFrame(实际上更多,但出于这个问题的目的,有两个)。其中一个包含一个单选按钮(同意),其中的条款和条件写在jtextarea中。另一个JFrame包含一个passwordtxtarea(password)、jtextarea(username)和一个单选按钮(readterms and conditions),以及一个“terms and conditions”按钮。我忘了提到第一个jtextarea包含一个“back”jbutton,如果我按下,我得到JFrame2,当我按下“Terms And Conditions”时,我得到JFrame1。问题是,我的代码要求单击“同意”和“阅读条款”单选按钮,但每当我按下“返回”或“条款和条件”时,我输入的任何输入(用户名、密码、单击单选按钮而不是默认值)都会丢失。因此,我无法继续我的计划

我认为这与我必须制作一个新的JFrame表单这一事实有关。也许它会将其设置回默认值?无论如何,我该如何解决这个问题?我从来没有见过这样的问题,所以除了“不可能”之外,有没有一个明显的答案我看不到


共 (1) 个答案

  1. # 1 楼答案

    你声明/我答复:

    I was wondering if there was a way to reuse a jframe without making it new.

    是的,可以重复使用组件(您问题的概括)

    You might be wondering why. I have two JFrames (actually more, but for this question's purpose, two).

    如前所述,这通常不是一个好主意。大多数Swing GUI应用程序只使用一个主窗口JFrame,然后交换视图(如使用CardLayout或JTabbedPane),或者显示模式或非模式对话框窗口

    One contains a radio button(agree) with the terms and conditions written in a jtextarea. THe other JFrame contains a passwordtxtarea(password), jtextarea(username) and a radio button(read terms and conditions), as well as a "TermsAnd Conditions" button. I forgot to mention that the first jtextarea contains a "back" jbutton,

    JTextArea有任何类型的按钮是最不寻常的。另外,这里没有“passwordtxtarea”之类的东西,也许你指的是JPasswordField?如果是这样的话,请在这里提问时准确地说出你的条件。根据一个描述,很难猜测某人的程序是什么样的,你不想让它对我们更难。另外,使用JTextArea作为用户名字段是非常不寻常的,因为通常您会使用JTextField。再说一次,精确性真的很重要。否则我们可能会给你错误的建议

    that if i press on, I get JFrame2, when I press "Terms And Conditions", I get JFrame1. The problem is, that my code requires both "Agree" and "read the Terms" radio buttons to be clicked on, but whenever I press "back" or "Terms And Conditions", any input I had put in (username, password, clicks on radio button other than default) is lost. Therefore I cannot proceed in my program.

    是的,您不应该在此处创建新组件,而应该重新使用以前创建的组件。如果您将组件设置为类字段,并且确保只创建一次,那么这一切都是可以做到的。这都是你如何编码的

    I think it has to do with the fact that I have to make a NEW JFrame Form. Maybe it sets it back to default? Anyway, how do I fix this problem? I haven't seen a question like this, so is there a blatantly obvious answer I'm unable to see, except for "it's impossible"?

    这也是可能的。解决方案将完全取决于程序的结构

    一句忠告:让您的GUI代码面向JPanel,而不是JFrames。通过这种方式,您可以将它们放置在JFrame、JDialog、另一个JPanel中所需的任何位置,或者与CardLayout交换,。。。在任何地方它大大增加了程序的灵活性