嗨,我正在为我的 java 类开发一个程序,但遇到了一些问题。任务是编辑教授的现有代码,以便当在 GUI 中单击按钮时,它会获取当前 JTextField 的值并将其添加到下一个字段的值中,然后将该值放入该字段中。我已经设法将他编辑到具有正确 GUI 布局的地步,并试图让计算方面发挥作用,但我失去了想法。我需要了解如何在启动时立即显示 JTextFields 中的值,以及如何添加内容。任何帮助是极大的赞赏!
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);
}
}