gpt4 book ai didi

java - 如何强制我的文本在 JTextFrame 中右对齐?

转载 作者:行者123 更新时间:2023-12-02 05:51:24 25 4
gpt4 key购买 nike

我试图使我的文本字段右对齐,但我不太确定如何使它们在 JTextField 中右对齐。我读到您可以使 Jtextfield 正确对齐,但我只是找不到正确的语法。

目前,它们是合理的。

 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class RecursivePowerCalculator extends JFrame {
//text fields for base, exponent, and power.
private JTextField jtfBase = new JTextField();
private JTextField jtfExponent = new JTextField();
private JTextField jtfPower = new JTextField();

//button to press called calcuate
private JButton jbtCalculate = new JButton("Calculate");

//constructor
public RecursivePowerCalculator(){
jtfPower.setEditable(false);

JPanel p1 = new JPanel(new GridLayout(3,2));

p1.add(new JLabel("Base"));
p1.add(jtfBase);

p1.add(new JLabel("Exponent"));
p1.add(jtfExponent);

p1.add(new JLabel("Power"));
p1.add(jtfPower);

JPanel p2 =new JPanel(new FlowLayout(FlowLayout.CENTER));
p2.add(jbtCalculate);

//add the panels to the frame
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);

jbtCalculate.addActionListener(new ButtonListener());
}

private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
//get values from text fields
double base = Double.parseDouble(jtfBase.getText());
double exponent = Double.parseDouble(jtfExponent.getText());

String pNum = "" + (power(base, exponent));
jtfPower.setText(pNum);
}
private double power(double x, double y){
if ( y == 0)
return 1.0;
else
return power(x,y - 1) * x;
}
}
}

最佳答案

使用jtfPower.setHorizo​​ntalAlignment(JTextField.RIGHT);请参阅javadoc .

关于java - 如何强制我的文本在 JTextFrame 中右对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com