- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 JSwing 开发一个程序。
我的程序使用正确的逻辑,但由于某种原因,当我输入第一个输入时,它会创建一个包含正确更改的新窗口。
它只创建一个新窗口一次。
下面是我的转换器代码
public class Converter extends JFrame{
//Creating text fields to be put into the "Distance" Tab
JTextField metersTxt = new JTextField(), feetTxt = new JTextField(),milesTxt = new JTextField(),
inchesTxt = new JTextField(), cmTxt = new JTextField(), kmTxt = new JTextField();
public Converter(){
//Creating the GUI object for frame
JFrame frame = new JFrame("Distance/Energy Converter");
frame.setSize(600, 600);
frame.setVisible(true);
//Assigning a name to JTextFile
metersTxt.setName("meters");
feetTxt.setName("feet");
milesTxt.setName("miles");
inchesTxt.setName("inches");
cmTxt.setName("cm");
kmTxt.setName("km");
//Making an instance of a tabbedPane to be able to add 2 panes.
JTabbedPane tabbedPane = new JTabbedPane();
//Making the first tab to convert distance values
JPanel panel1 = new JPanel(true);
tabbedPane.addTab("Distance", panel1);
panel1.setLayout(new GridLayout(0,6));
//A second tab to switch to energy conversions
JPanel panel2 = new JPanel(true);
tabbedPane.addTab("Energy", panel2);
panel2.setLayout(new GridLayout(0,6));
//Add the tabbedPane to this panel.
frame.add(tabbedPane);
//Creates labels for the "Distance" tab
JLabel meters = new JLabel("Meters");
JLabel feet = new JLabel("Feet");
JLabel miles = new JLabel("Miles");
JLabel inches = new JLabel("Inches");
JLabel cm = new JLabel("Centimeters");
JLabel km = new JLabel ("Kilometers");
//Create labels for the "Energy" tab
JLabel joule = new JLabel("Joule");
JLabel cal = new JLabel("Calorie");
JLabel volt = new JLabel("Volt");
JLabel watt = new JLabel("Watt");
JLabel thermal = new JLabel("Thermal Units");
//Adding labels and text boxes to distance panel in order to be formatted right
panel1.add(meters);
panel1.add(metersTxt);
panel1.add(feet);
panel1.add(feetTxt);
panel1.add(miles);
panel1.add(milesTxt);
panel1.add(inches);
panel1.add(inchesTxt);
panel1.add(cm);
panel1.add(cmTxt);
panel1.add(km);
panel1.add(kmTxt);
//Allowing the user to edit the text boxes
metersTxt.setEditable(true);
feetTxt.setEditable(true);
milesTxt.setEditable(true);
inchesTxt.setEditable(true);
cmTxt.setEditable(true);
kmTxt.setEditable(true);
//Attaching the metric listener to each JTextField
metersTxt.addActionListener(new MetricListener());
feetTxt.addActionListener(new MetricListener());
milesTxt.addActionListener(new MetricListener());
inchesTxt.addActionListener(new MetricListener());
cmTxt.addActionListener(new MetricListener());
kmTxt.addActionListener(new MetricListener());
//To clean up interface and close it when close the window
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
Converter mf = new Converter();
}
}
然后是我的监听器类
public class MetricListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
Converter c = null;
c = new Converter();
//Getting where the action takes place
JTextField txt = new JTextField();
txt = (JTextField) e.getSource();
//Extracting input from GUI
String input = null;
input = e.getActionCommand();
//Turing the taken in value into type double
double value = 0;
value = Double.parseDouble(input);
//Variables to hold values for conversions
double meters = 0, feet = 0, miles = 0, inches = 0, cm = 0, km = 0;
String txtName = null;
txtName = txt.getName();
//The following if statements are seeing which JTextField is being changed
if(txtName.equals("meters")){
//Creating conversions from meters
meters = value;
feet = value * .3048;
miles = value * 0.000621371;
inches = value * 39.3701;
cm = value * 100;
km = value * .001;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("feet")){
//Creating conversions from feet
meters = value * .3048;
feet = value ;
miles = value * 0.000189394;
inches = value * 12;
cm = value * 30.48;
km = value * 0.0003048;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("miles")){
//Creating conversions from feet
meters = value * 1609.34;
feet = value * 5280;
miles = value;
inches = value * 63360;
cm = value * 160934;
km = value * 1.60934;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("inches")){
//Creating conversions from feet
meters = value * 0.0254;
feet = value * 0.0833333;
miles = value * .0000157828;
inches = value;
cm = value * 2.54;
km = value * .0000254;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("cm")){
//Creating conversions from feet
meters = value * 0.01;
feet = value * 0.0328084;
miles = value * .00000621371;
inches = value * 0.393701;
cm = value;
km = value * .00001;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("km")){
//Creating conversions from feet
meters = value * 1000f;
feet = value * 3280.84;
miles = value * 0.621371;
inches = value * 39370.1;
cm = value * 100000;
km = value;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
}
}
如有任何帮助,我们将不胜感激!谢谢!
最佳答案
您的第一个问题是在 Converter() 内创建一个新的 JFrame。 Converter 已经是一个 JFrame,因此您可以使用“this”而不是 auf“frame”。
我认为打开一个新窗口不是有意的,所以不要在actionPerformed中创建一个新的转换器。因此,请将转换器的引用提供给 MetricListener。如果将 MetricListener 设为 Converter 的内部类,那就更容易了。
我尝试在这里展示要点:
public class Converter extends JFrame{
//Creating text fields to be put into the "Distance" Tab
JTextField metersTxt = new JTextField(), feetTxt = new JTextField(),milesTxt = new JTextField(), inchesTxt = new JTextField(), cmTxt = new JTextField(), kmTxt = new JTextField();
//Creating the GUI object for frame
public Converter() {
this.setTitle("Distance/Energy Converter");
this.setSize(600, 600);
this.setVisible(true);
...
//Add the tabbedPane to this panel.
this.add(tabbedPane);
...
//To clean up interface and close it when close the window
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MetricListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
/* DO NOT CREATE A NEW Converter */
...
//The following if statements are seeing which JTextField is being changed
if(txtName.equals("meters")){
....
/* Since it is an inner class you can use these variables directly */
//Applying the converted values to respected places
metersTxt.setText(Double.toString(meters));
feetTxt.setText(Double.toString(feet));
milesTxt.setText(Double.toString(miles));
inchesTxt.setText(Double.toString(inches));
cmTxt.setText(Double.toString(cm));
kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("feet")){
/* and so on */
}
}
}
public static void main(String args[]){
Converter mf = new Converter();
}
}
关于java - Swing : Action Performed creating a new window.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42383427/
我知道它们是匿名类型,但我不明白 Razor 语法。在一些文档中,我找到了这样的示例: @Html.Label("Hello", new { htmlAtributes = new { id = "h
关于:new Object(new Array()) 有一个相当基本的问题,我自己确实无法给出答案,我正在寻求建议: 在js中实例化对象时使用如下方法: var obj = new Object();
在eclipse中右击项目时,“新建文件夹”、“新建源文件夹”和“新建包”有什么区别?他们似乎都在做同样的事情,引用文献并没有说太多。 谢谢 最佳答案 新建文件夹 在项目中创建一个新文件夹。 新建源文
几天来我一直在测试 bolt-cms,我试图了解它是如何工作的。 我想知道新页面、新条目和新展示柜之间有什么区别。 我已阅读 this它并没有填补空白。 最佳答案 Pages、Entries 和 Sh
更新:感谢所有的回答。我发现的最干净的解决方案是这个: if ( k(Arrays.asList(new LinkedList<>())); 我有一个递归方法,可以从列表中生成所有“n 选 k”组合。
我现在想知道这些指令是如何分配内存的。 例如,如果我得到代码怎么办: x = new int[5]; y = new int[5]; 如果分配了这些,它在 RAM 中的实际情况如何?是为每个变量保留整
我希望将其写入output.txt而不清除它 - 只是附加到末尾。但是,当我使用以下两种方法时: public void addEmails(ArrayList emails){ for (i
我正在分配内存,稍后将用于构造具有放置 new 的对象。我应该使用 operator new(n),还是应该使用 new unsigned char[n]?为什么? 最佳答案 因素: new[] 必须
基本上,我的问题是以下代码是否有效。 void* mem = operator new(sizeof(T)); T* instance = new(mem) T; delete instance; 如
很抱歉,如果之前有人问过这个问题,但我想就以下两种用法之间的区别提供一个简明的答案。 VS 似乎将它们都接受为有效代码。 private static void doSomeWork() { /
请告诉我这段代码在做什么,它是否创建多维数组(我认为不是)? 代码片段.. var hanoi_peg = new Array( new Array( 5, 4, 3, 2, 1,
这个问题在这里已经有了答案: String intern() behaviour (4 个答案) When should we use intern method of String on Stri
许多人说您应该避免使用 new Object、new Array(),而是使用 {}。 [] 和真/假。 使用字面量构造来获取对象或数组的新实例而不是使用 new 有什么好处?我知道 Crockfor
我正在开发一个存在内存泄漏的开源库。该库是围绕 boost::asio 构建的数据流服务。服务器端使用堆内存管理系统,该系统提供内存以容纳有限数量的 samples,同时它们等待通过 tcp 连接被推
我从以下函数中得到内存泄漏: int ReadWrite(int socket, char *readfile) { FILE *rf = NULL; rf = fopen(readfile,
在考虑类似的事情时 auto x = new T; 标准是否强制要求内存必须来自operator new——类特定的还是全局的?也就是说,如果缺少特定于类的 operator new,则没有办法从除全
只是出于好奇:为什么 C++ 选择 a = new A 而不是 a = A.new 作为实例化对象的方式?后者不是更像是面向对象的吗? 最佳答案 Just out of curiosity: Why
考虑以下代码: typedef SomeType type_t[2]; SomeType * arr1 = new type_t; //new or new[] ??? type_t * arr2
这个问题在这里已经有了答案: Difference between 'new operator' and 'operator new'? (8 个答案) 关闭 8 年前。 面试题:"new"运算符和
我正在为一个应用程序设计界面,以在 TableLayout 中显示从数据库中提取的一些数据。现在,默认 View 是纵向的,它由一个下拉菜单和一个三列的表格组成。当用户切换到横向时,微调器及其选项可以
我是一名优秀的程序员,十分优秀!