按钮点击变量修改/其他Swing对象 [英] Modification of variables/other swing objects on button click
问题描述
好了,所以基本上即时通讯努力与执行的Java Swing Jbutton将一些行动。我有一种感觉,我的问题是熟练的程序员用我的基本问题,使裸露的易用性:
Ok so basically im struggling to perform some actions with Java Swing JButtons. I have a feeling that my problem is an ease for skilled programmers so bare with my "elementary" problems:
我想要做的就是修改其他JComponent中的内容,并可能修改按钮点击一些变量。这似乎比较容易,但我想在已申报变量(或已经创建JComponent上)
What i'd like to do is modify content of other JComponent and possibly modify some variables on button click. It seems relatively easy but i would like to perform it on "already declared" variables (or already created JComponents)
无论我搜索我总是得到的一切是从零开始宣布实施内部功能的actionPerformed涉及到具体的JButton案件的例子,就像这里:
Wherever i search i always get examples of cases where everything is declared from scratch inside implementation of ActionPerformed function related to specific JButton, just like here:
HTTP://zet$c$c.com/tutorials/javaswingtutorial/swingevents /
//上面可以看到,在的actionPerformed功能,他们定义了新的变量,如:字符串/日期/区域/ StringBuffer的
http://zetcode.com/tutorials/javaswingtutorial/swingevents/ //above you can observe that in "actionPerformed" function they define new variables such as: string/date/locale/stringbuffer
这样的解决方案不通过任何方式满足我的需求。我想修改/显示变量,这是由于pviously与使用功能定义用户点击$ P $属于不同的类。
Such solution does not satisfy my needs by any means. I want to modify/show variable that was previously defined due to user click with use of function that belongs to different class.
好吧,也许会更清楚,如果我会告诉你什么,我想在一个示范性源$ C $ C做的:
Ok maybe it will be more clear if i'll show you what i'd like to do on a exemplary sourcecode:
class Number_String {
public String change_add_MOD (String sss){
String str_modified = sss + "_modified";
return str_modified;
}
public int change_plus1 (int z){
int z_modified = z + 1;
return z_modified;
}
public static void main(String[] args) {
// TODO code application logic here
}
}
public class probTESTswing extends javax.swing.JFrame {
public probTESTswing() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText("done");
jLabel2.setText(mystring2);
jLabel3.setText(ns.change_add_MOD(mystring3));
}
public static void main(String args[]) {
Number_String ns=null;
String first_str="myFIRSTstring";
String mystring2 = ns.change_add_MOD(first_str);
String mystring3 = "third_str";
probTESTswing testing= new probTESTswing();
testing.setVisible(true);
}
}
问候大家谁作出的努力来到这里;)
Greetings to everyone who made effort to get here;)
所以,还是让我先简单介绍一下:
So lemme briefly explain:
主类是Swing GUI的凡在main()函数我使用第二类初始化一些变量,现在我想显示处理的结果与我的NUMBER_STRING类的变量,但是,Java拒绝为我的编程合作技能缺乏一些基本的知识,你可能已经注意到了。
Main class is the "swing gui" where in main() function i initialize some variables using second class, now i want to show result of "processing" those variables with my Number_String class, however Java refuses to cooperate as my programming skills lack some fundamental knowledge as you probably noticed.
您可以看到,我想要做的它在两个方面:内部jButton1ActionPerformed在这里我想从我的NUMBER_STRING类访问函数(利用'mystring3),还哪里,我想其他的办法访问只需$ pviously定义的变量(mystring2)p $
You can observe that i want to do "it" in both ways: where inside jButton1ActionPerformed i want to access function from my Number_String class (with use of 'mystring3'), and also the other way where i'd like to access just previously defined variable(mystring2)
任何帮助非常AP preCIATED。
ANY HELP GREATLY APPRECIATED.
-
我该超载jButton1ActionPerformed功能(以便采取变量作为参数)? (是的,我知道它的私有 - >我使用Netbeans摇摆设计师创造了它)
--shall我设计的手我的GUI,然后重载它们? (不知道这样是否超载可能是你)
shall i "overload" jButton1ActionPerformed function (in order to take variables as a parameter)? (yes i know its private -> I created it with Netbeans Swing designer) --shall i design my GUI by hand and then overload them? (dunno whether such overload is possible thou)
也许我应该定义一些缓冲(现场整数和领域字符串),我想利用我的JButton的修改,然后把它们里面的probTESTswing类变量? (+编写相应的函数来处理,我使用它的功能,这样的方式对其他类?)
or maybe i should define some "buffer" (field for integer and field for string) for variables that i want modify with use of my Jbutton and then put them inside "probTESTswing" class? (+ write appropriate functions to handle the other class in such way that i use its functions?)
或者(因为我NUMBER_STRING不拥有任何变量 - 只是功能)移动到GUI类的那些功能(probTESTswing)
or maybe (since my Number_String does not possess any variables - just functions) move those functions to GUI class (probTESTswing)
也许我应该用一些指针/引用? (是啊,似乎这就是我需要在这里,unfortunetely指针/引用的东西我什么都不知道:(,但是我想学了很多,因为它似乎强制性的,因为我发现了 - 很难找到你的教程因为据我所知是没有指针,我不知道如何使用这样的方式引用,以便需要的功能就可以实现 - 因为你可以从我的问题看)
OR maybe i should use some pointers/references? (yea it seems that this is what i need here, unfortunetely pointers/references are something i know nothing about :(, however i'd like to learn it a lot, as it seems mandatory as i discovered now - hard to find tutorials thou, since as far as i know there is no pointers, and i have no clue how to use references in such way so that desired functionality would be achieved - as you can see from my problem)
或可能有完全不同的方式来处理正是这样的问题/个案?
OR maybe there is totally different way to deal with exactly such problems/cases?
请微启我,我想解决我的问题有效,在一个良好的编程方式,为了学习高效,优化,合理,快速的解决方案,但是即时通讯绝望的帮助,所以任何建议是极大的$ AP p $ pciated。
please enlight me, i'd like to solve my problem "efficiently" in a good programming manner, in order to learn on efficient, optimal, proper and fast solutions, however im desperate for help so any advice is greatly appreciated.
问候
推荐答案
一种方法是将数据模型从视图中使用的模型 - 视图 - 控制器模式,讨论<一个href=\"http://stackoverflow.com/questions/3066590/gui-problem-after-rewriting-to-mvc/3072979#3072979\">here.
One approach is to separate the data model from the view using the Model–View–Controller pattern, discussed here.
附录:通俗地讲,你可能只是需要给类 ProbTestSwing
合适的成员变量:
Addendum: More simply, you may just need to give class ProbTestSwing
a suitable member variable:
private Number_String ns = new Number_String();
这篇关于按钮点击变量修改/其他Swing对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!