JTextField输入和输出(JTextField input and output)
我正在尝试用Java设计Mile to Nautical Mile计算器。
我能够通过控制台创建并运行它,但我在学习如何在JFrame中进行操作时遇到了麻烦。
我基本上想做的是:
有两个文本字段和一个按钮,一个文本字段英里和一个海里,当您将金额输入一个然后按下按钮时,结果将显示在另一个字段中。
我在下面粘贴了我的代码
不确定接下来该做什么
package Mile_Conversion; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class OwnWindow implements ActionListener{ public static void main(String[] args) { MyJFrame(); } public static void MyJFrame() { //JLabel JLabel start = new JLabel("Enter the amount below to calculate"); start.setBounds(/*x*/70, -65/*Y*/, 270, 150); // x, y, width, height x across, y down JLabel milecon = new JLabel("Miles"); milecon.setBounds(154, 55, 50, 150); JLabel nautcon = new JLabel("Nautical Miles"); nautcon.setBounds(135, -15, 150, 150); //JTextField JTextField miles = new JTextField(); miles.setBounds(100, 140, 150, 25); JTextField nautical = new JTextField(); nautical.setBounds(100, 70, 150, 25); double mile, nautmile; /* mile = nautmile * 0.868; nautmile = mile * 1.150; */ //JButton JButton convert = new JButton("Convert"); convert.setBounds(250, 200, 90, 25); //JFrame JFrame window = new JFrame("Milage Conversion"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(400, 300); window.setVisible(true); window.setLayout(null); window.setResizable(false); window.add(start); window.add(miles); window.add(nautical); window.add(convert); window.add(milecon); window.add(nautcon); } @Override public void actionPerformed(ActionEvent e) { } }
I'm trying to design a Mile to Nautical Mile calculator in Java.
I was able to create and run it okay through the console, but i'm having trouble learning how to do it in JFrames.
what im basically wanting to do is:
Have two text fields and a button, one text field miles and one for nautical miles, when you enter the amount into one then press the button the result will display in the other field.
I've pasted my code below
Not really sure what to do next
package Mile_Conversion; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class OwnWindow implements ActionListener{ public static void main(String[] args) { MyJFrame(); } public static void MyJFrame() { //JLabel JLabel start = new JLabel("Enter the amount below to calculate"); start.setBounds(/*x*/70, -65/*Y*/, 270, 150); // x, y, width, height x across, y down JLabel milecon = new JLabel("Miles"); milecon.setBounds(154, 55, 50, 150); JLabel nautcon = new JLabel("Nautical Miles"); nautcon.setBounds(135, -15, 150, 150); //JTextField JTextField miles = new JTextField(); miles.setBounds(100, 140, 150, 25); JTextField nautical = new JTextField(); nautical.setBounds(100, 70, 150, 25); double mile, nautmile; /* mile = nautmile * 0.868; nautmile = mile * 1.150; */ //JButton JButton convert = new JButton("Convert"); convert.setBounds(250, 200, 90, 25); //JFrame JFrame window = new JFrame("Milage Conversion"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(400, 300); window.setVisible(true); window.setLayout(null); window.setResizable(false); window.add(start); window.add(miles); window.add(nautical); window.add(convert); window.add(milecon); window.add(nautcon); } @Override public void actionPerformed(ActionEvent e) { } }
原文:https://stackoverflow.com/questions/12713052
最满意答案
Sub DeleteRows() Dim LastRow As Long Dim LastCol As Long Dim i As Long Dim wsTemp As Worksheet Dim v As Variant Set wsTemp = ActiveWorkbook.Worksheets.Add With Sheets("Test") LastRow = .Range("A:A").Find(what:="end", after:=.Cells(.Rows.Count, 1), LookIn:=xlValues, lookat:=xlWhole).Row For i = 1 To LastRow LastCol = .Cells(i, .Columns.Count).End(xlToLeft).Column If LastCol > 1 Then v = .Cells(i, 1).Resize(1, LastCol).Value .Cells(i, 1).Resize(1, LastCol).ClearContents wsTemp.Range("A:A").Clear wsTemp.Range("A1").Resize(UBound(v, 2), 1).Value = Application.Transpose(v) wsTemp.Range("A1").Resize(UBound(v, 2), 1).RemoveDuplicates Columns:=1, Header:=xlNo v = wsTemp.Range("A:A").SpecialCells(xlCellTypeConstants).Value .Cells(i, 1).Resize(1, UBound(v, 1)).Value = Application.Transpose(v) End If Next End With Application.DisplayAlerts = False wsTemp.Delete Application.DisplayAlerts = True End Sub
Sub DeleteRows() Dim LastRow As Long Dim LastCol As Long Dim i As Long Dim wsTemp As Worksheet Dim v As Variant Set wsTemp = ActiveWorkbook.Worksheets.Add With Sheets("Test") LastRow = .Range("A:A").Find(what:="end", after:=.Cells(.Rows.Count, 1), LookIn:=xlValues, lookat:=xlWhole).Row For i = 1 To LastRow LastCol = .Cells(i, .Columns.Count).End(xlToLeft).Column If LastCol > 1 Then v = .Cells(i, 1).Resize(1, LastCol).Value .Cells(i, 1).Resize(1, LastCol).ClearContents wsTemp.Range("A:A").Clear wsTemp.Range("A1").Resize(UBound(v, 2), 1).Value = Application.Transpose(v) wsTemp.Range("A1").Resize(UBound(v, 2), 1).RemoveDuplicates Columns:=1, Header:=xlNo v = wsTemp.Range("A:A").SpecialCells(xlCellTypeConstants).Value .Cells(i, 1).Resize(1, UBound(v, 1)).Value = Application.Transpose(v) End If Next End With Application.DisplayAlerts = False wsTemp.Delete Application.DisplayAlerts = True End Sub
相关问答
更多-
使用RemoveDuplicates() 并且,由于您从列“A”中删除所有重复项,您可以在列“A”或列“P”上排序:我假设您需要后者 Sub SortAndRemoveDUBS() With Worksheets("MyDataSheet") '<--| change "MyDataSheet" to your actual worksheet name With Range("A4:P" & .Cells(.Rows.Count, "B").End(xlUp).Row) ...
-
按要求修改,根据前两列比较重复项并列出A到E列: function removeDuplicatesRev() { var ss = SpreadsheetApp.getActiveSpreadsheet() var sheet =ss.getSheetByName("Sheet1") var lr=sheet.getLastRow() var lc=sheet.getLastColumn() var data = sheet.getRange(2,1,lr,lc).getValues( ...
-
使用VBA删除重复项(Removing Duplicates using VBA)[2023-04-13]
一种方法是动态创建数组。 一旦定义了块,我们就知道该块包含多少列: Sub luxation() Dim A1 As Range, rng As Range, cCount As Long Set A1 = Range("A1") Set rng = A1.CurrentRegion cCount = rng.Columns.Count - 1 ReDim ary(0 To cCount) For i = 0 To cCount ary(i ... -
您可以使用窗口功能执行此操作 from pyspark.sql import Window, functions as F df.withColumn( 'fg', F.count("id").over(Window.partitionBy("id", "value")) ).where("fg = 1").drop("fg").show() You can do that using the window functions from pyspark.sql import Window, fu ...
-
对于任何寻找代码的人来说,都是这样。 再次感谢您的帮助。 Sub Macro1() ' ' Macro1 Macro ' ' Range("A2:M23").Select ActiveWorkbook.Worksheets("Scratchsheet (2)").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Scratchsheet (2)").Sort.SortFields.Add Key:=Range( _ C ...
-
不要使用Activesheet - >它伤害了我的眼睛。 为您的工作表命名,然后使用 LR = nb.sheets('somesheet').Range("A1:H" & LR) 您会发现整个程序中的错误较少。 尝试在设置范围之前显式激活工作簿和工作表。 编辑根据您的第二个问题,您应该调试: 1)检查使用范围 Dim oRange as excel.range nb.sheets("NAMEYOURSHEET").activate set oRange = nb.sheets("NAMEYOURSH ...
-
如果函数没有返回任何值,则在调用RemoveDuplicates时删除括号,如下所示: selection.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes Remove the parenthesis when calling RemoveDuplicates if the function is not returning any value, like this: selection.RemoveDuplicates Columns:=Arra ...
-
Excel VBA如何根据列C删除所有重复行(Excel VBA How to Remove all the Duplicate rows based on column C)[2022-05-06]
这样的事情对你有用: Sub tgr() Dim ws As Worksheet Dim rCheck As Range Dim rDel As Range Set ws = ActiveWorkbook.ActiveSheet For Each rCheck In ws.Range("C1", ws.Cells(ws.Rows.Count, "C").End(xlUp)).Cells If WorksheetFunction.CountIf(w ... -
VBA从行中删除重复项(VBA Remove duplicates from rows)[2022-08-11]
Sub DeleteRows() Dim LastRow As Long Dim LastCol As Long Dim i As Long Dim wsTemp As Worksheet Dim v As Variant Set wsTemp = ActiveWorkbook.Worksheets.Add With Sheets("Test") LastRow = .Range("A:A").Find(what:="end", after:=.Cells(.Rows.Count, 1), L ... -
虽然我需要花一些时间来编写一些代码,但最好的办法是创建一个新数组并复制你想要保留的值。 您不能只删除数组中间的元素。 Redim仅允许您添加或删除数组末尾的元素。 While it would take me a while to cobble up some code the best bet would be to create a new array and copy the values you want to keep to it. You can't just delete elements i ...