【发布时间】:2020-04-02 22:08:20
【问题描述】:
下面的代码是将十六进制值转换为十进制和二进制的 GUI。问题是按钮打印在顶部,我还附上了一些图片以供参考,以了解我想要的结果。
class MyFrames implements ActionListener
{
JFrame DancingFinchFrame;
static JLabel NewLabel, DecimalValue, BinaryValue, FinchSpeed;
static JTextField textField, DecimalText, BinaryText, SpeedText;
static JButton Convert, Dance;
static String Hexadecimal, Binary;
static int Decimal, Speed;
MyFrames()
{
// creates a frame
DancingFinchFrame=new JFrame("Calculator");
DancingFinchFrame.setTitle("Dancing Finch");
DancingFinchFrame.setSize(320, 280);
DancingFinchFrame.setLocationRelativeTo(null);
DancingFinchFrame.setResizable(false);
DancingFinchFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
NewLabel = new JLabel("Please enter a hexadecimal value");
NewLabel.setVerticalAlignment(SwingConstants.TOP);
NewLabel.setHorizontalAlignment(SwingConstants.CENTER);
NewLabel.setFont(new Font("Arial", Font.BOLD, 15));
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setColumns(20);
Convert = new JButton("CONVERT");
Convert.setBounds(10, 400, 100, 40);
DecimalValue = new JLabel("Decimal Value is ");
DecimalValue.setVisible(false);
DecimalValue.setFont(new Font("Arial", Font.BOLD, 15));
DecimalValue.setVisible(false);
DecimalText = new JTextField();
DecimalText.setEditable(false);
DecimalText.setVisible(false);
DecimalValue.setLabelFor(DecimalText);
DecimalText.setColumns(10);
BinaryValue = new JLabel("Binary Value is ");
BinaryValue.setVisible(false);
BinaryValue.setFont(new Font("Arial", Font.BOLD, 15));
BinaryValue.setToolTipText("");
BinaryText = new JTextField();
BinaryText.setEditable(false);
BinaryText.setVisible(false);
BinaryValue.setLabelFor(BinaryText);
BinaryText.setColumns(10);
FinchSpeed = new JLabel("Finch Speed is ");
FinchSpeed.setVisible(false);
FinchSpeed.setFont(new Font("Arial", Font.BOLD, 15));
SpeedText = new JTextField();
SpeedText.setVisible(false);
SpeedText.setEditable(false);
FinchSpeed.setLabelFor(SpeedText);
SpeedText.setColumns(10);
Dance = new JButton("DANCE");
Dance.setVisible(false);**
// creates the panel
JPanel mypanel = new JPanel();
//adds action listeners and initialises the event handling for the buttons
Convert.addActionListener(this);
Dance.addActionListener(this);
textField.addActionListener(this);
// adds elements to the panel
mypanel.add(Convert);
mypanel.add(Dance);
mypanel.add(NewLabel);
mypanel.add(textField);
mypanel.add(DecimalValue);
mypanel.add(DecimalText);
mypanel.add(BinaryValue);
mypanel.add(BinaryText);
mypanel.add(FinchSpeed);
mypanel.add(SpeedText);
mypanel.setBackground(Color.white);
// adds panel to frame
DancingFinchFrame.add(mypanel);
DancingFinchFrame.setSize(320, 280);
DancingFinchFrame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Hexadecimal = textField.getText();
Decimal = DecimalConverter.HexaToDecimal(Hexadecimal);
Binary = BinaryConverter.HexaToBinary(Hexadecimal);
Speed = SpeedCalculator.Speed(Decimal);
if(e.getSource()==Convert)
{
if (Decimal >= 144 && Decimal <= 255)
{
DecimalValue.setVisible(true);
DecimalText.setVisible(true);
BinaryValue.setVisible(true);
BinaryText.setVisible(true);
Dance.setVisible(true);
FinchSpeed.setVisible(true);
SpeedText.setVisible(true);
DecimalText.setText(""+ Decimal);
BinaryText.setText(""+ Binary);
SpeedText.setText("" + Speed);
}
else
{
try
{
JOptionPane.showMessageDialog(null, "Please enter a valid input!", "ERROR", JOptionPane.ERROR_MESSAGE);
}
catch (Exception ne)
{
ne.printStackTrace();
}
}
}
else if (e.getSource()==Dance)
{
int decimal = Integer.parseInt(SpeedText.getText());
String binary = BinaryText.getText();
FinchCommands Command = new FinchCommands();
Command.Dance(decimal, binary);
}
}
public JFrame getDancingFinchFrame()
{
return DancingFinchFrame;
}
}
问题是转换/跳舞按钮打印在顶部
这就是我想要的
提前谢谢你!!
【问题讨论】:
标签: java swing user-interface layout-manager