嗨我正在爲我的java類的程序工作,並遇到一些問題。該任務是編輯Professor的現有代碼,以便在GUI中單擊按鈕時,它將採用當前JTextField的值並將其添加到下一個字段的值中,並將該值放入該字段中。我已經設法將他編輯爲具有正確的GUI佈局,並試圖讓計算方面的工作,但在思想的損失。我需要了解如何在啓動時立即在JTextFields中顯示值,以及如何添加內容。任何幫助是極大的讚賞!如何獲取JTextField爲int並返回?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics2D;
public class U27
{
public static void main(String[] args)
{
MiscFocusFrame myframe = new MiscFocusFrame();
myframe.setVisible(true);
}
}
class MiscFocusFrame extends JFrame
{
private JTextField myField;
private MiscFocusPanel myPanel;
private final int FRAMEW = 900;
private final int FRAMEH = 200;
public MiscFocusFrame()
{
setTitle("U27");
setSize(FRAMEW, FRAMEH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel = new MiscFocusPanel();
Container contentPane = getContentPane();
contentPane.add(myPanel, "Center");
}
}
class MiscFocusPanel extends JPanel
{
private MiscFocusRegister register1;
private MiscFocusRegister register2;
private MiscFocusRegister register3;
private MiscFocusRegister register4;
public MiscFocusPanel()
{
register1 = new MiscFocusRegister(4);
register2 = new MiscFocusRegister(4);
register3 = new MiscFocusRegister(4);
register4 = new MiscFocusRegister(4);
register1.setRegisterToPassFocusTo(register2);
register2.setRegisterToPassFocusTo(register3);
register3.setRegisterToPassFocusTo(register4);
register4.setRegisterToPassFocusTo(register1);
setLayout(new GridLayout(2, 6));
JLabel labelA = new JLabel("Cup 1", JLabel.LEFT);
JPanel labelPanelA = new JPanel();
labelPanelA.add(labelA);
add(labelPanelA);
JPanel regPanelA = new JPanel();
regPanelA.add(register1.getMyField());
add(regPanelA);
JButton myButton1 = new JButton("Add To Next Cup");
MiscButtonListener myButtonListener1 = new MiscButtonListener();
myButton1.addActionListener(myButtonListener1);
JPanel buttonPanel1 = new JPanel();
buttonPanel1.add(myButton1);
add(buttonPanel1);
JLabel labelB = new JLabel("Cup 2", JLabel.LEFT);
JPanel labelPanelB = new JPanel();
labelPanelB.add(labelB);
add(labelPanelB);
JPanel regPanelB = new JPanel();
regPanelB.add(register2.getMyField());
add(regPanelB);
JButton myButton2 = new JButton("Add To Next Cup");
MiscButtonListener myButtonListener2 = new MiscButtonListener();
myButton2.addActionListener(myButtonListener2);
JPanel buttonPanel2 = new JPanel();
buttonPanel2.add(myButton2);
add(buttonPanel2);
JLabel labelC = new JLabel("Cup 3", JLabel.LEFT);
JPanel labelPanelC = new JPanel();
labelPanelC.add(labelC);
add(labelPanelC);
JPanel regPanelC = new JPanel();
regPanelC.add(register3.getMyField());
add(regPanelC);
JButton myButton3 = new JButton("Add To Next Cup");
MiscButtonListener myButtonListener3 = new MiscButtonListener();
myButton3.addActionListener(myButtonListener3);
JPanel buttonPanel3 = new JPanel();
buttonPanel3.add(myButton3);
add(buttonPanel3);
JLabel labelD = new JLabel("Cup 4", JLabel.LEFT);
JPanel labelPanelD = new JPanel();
labelPanelD.add(labelD);
add(labelPanelD);
JPanel regPanelD = new JPanel();
regPanelD.add(register4.getMyField());
add(regPanelD);
JButton myButton4 = new JButton("Add To Next Cup");
MiscButtonListener myButtonListener4 = new MiscButtonListener();
myButton4.addActionListener(myButtonListener4);
JPanel buttonPanel4 = new JPanel();
buttonPanel4.add(myButton4);
add(buttonPanel4);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
}
private class MiscButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
register1.swapRegisterContents(register2);
}
}
}
/*
This class contains a new method, setRegisterToPassFocusTo(), and in
the listener focus is passed to that register after the new value for
the text field is taken in.
*/
class MiscFocusRegister
{
private MiscFocusByte registerValue;
private JTextField myField;
private MiscFocusRegister registerToPassFocusTo;
public MiscFocusRegister()
{
}
public MiscFocusRegister(int stringIn)
{
registerValue = new MiscFocusByte(stringIn);
myField = new JTextField(stringIn);
TextFieldListener myListener = new TextFieldListener();
myField.addActionListener(myListener);
}
public void setRegisterValue(MiscFocusByte byteIn)
{
registerValue = byteIn;
}
public MiscFocusByte getRegisterValue()
{
return registerValue;
}
public void setMyField(JTextField fieldIn)
{
myField = fieldIn;
}
public JTextField getMyField()
{
return myField;
}
public void setRegisterToPassFocusTo(MiscFocusRegister registerIn)
{
registerToPassFocusTo = registerIn;
}
public void swapRegisterContents(MiscFocusRegister source)
{
MiscFocusByte tempValue;
tempValue = source.getRegisterValue();
source.setRegisterValue(this.getRegisterValue());
source.myField.setText(source.getRegisterValue().getStringFromByte());
this.setRegisterValue(tempValue);
this.myField.setText(this.getRegisterValue().getStringFromByte());
}
private class TextFieldListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String inputString = myField.getText();
registerValue.setByteToThisString(inputString);
registerToPassFocusTo.getMyField().requestFocusInWindow();
}
}
}
class MiscFocusByte
{
public static int bitsinbyte = 8;
private static final String junk = "00000000";
private char bytearray[] = new char[bitsinbyte];
public String astring;
public MiscFocusByte()
{
junk.getChars(0, bitsinbyte, bytearray, 0);
}
public MiscFocusByte(int stringIn)
{
//junk.getChars(0, bitsinbyte, bytearray, 0);
//int stringlength = stringIn.length();
//if(stringlength > bitsinbyte)
// stringlength = bitsinbyte;
//stringIn.getChars(0, stringlength, bytearray, 0);
stringIn = stringIn + 4;
}
public void setByteToThisString(String astring)
{
//junk.getChars(0, bitsinbyte, bytearray, 0);
//int stringlength = astring.length();
//if(stringlength > bitsinbyte)
// stringlength = bitsinbyte;
//astring.getChars(0, stringlength, bytearray, 0);
astring = astring + 4;
}
public String getStringFromByte()
{
//return String.copyValueOf(bytearray);
return String.valueOf(astring);
}
}
我試着運行您的程序。點擊「添加到下一個杯子」後,上排顯示爲空。我沒有任何地方和你的問題是什麼。請詳細說明您的需求,以便它可以輕鬆幫助您。 – Smit
我也收到了你提到的空值。我只是在邏輯上不知道如何讓文本字段在程序開始時顯示「4」,以及如何在按下按鈕時將它們添加到下一個文本字段中。如果這太模糊了,我表示歉意,但我盡我所能去描述它。 – Derek