gpt4 book ai didi

java - 如何在 Java 中为组布局设置 Scrollable TextArea?

转载 作者:行者123 更新时间:2023-11-30 06:27:25 26 4
gpt4 key购买 nike

我有一个问题。每次,当我向 TextArea 添加更多符号(数字)时,它都不会使其可滚动。编辑:现在它可以按我的意愿工作。我只需要改变2个字。谢谢。

class NumOnly extends KeyAdapter {  

private String Atlauts = "[^0-9]"; //Allowed Buttons.
public void keyReleased(KeyEvent e) { //Key event. What happens when the button is pressed
String curText = ((JTextComponent) e.getSource()).getText(); //Current text
curText = curText.replaceAll(Atlauts, "");

((JTextComponent) e.getSource()).setText(curText);
}
}

public class kursadarbs{

public static void main(String[] args) {

JFrame frame= new JFrame();
JPanel panel= new JPanel();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//components
JLabel label1= new JLabel("Insert first number: ");

final JTextField textbox1= new JTextField(10);
textbox1.addKeyListener(new NumOnly());

JLabel label2= new JLabel("Insert second number: ");

final JTextField textbox2= new JTextField(10);
textbox2.addKeyListener(new NumOnly());

JButton button= new JButton("Calculate");

final JTextArea textarea= new JTextArea(20,20); //Result is stored in there
textarea.setEditable(false);
textarea.setLineWrap(true);
JScrollPane scroll= new JScrollPane(textarea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textarea.setWrapStyleWord(true);
textarea.setBorder(new TitledBorder(new EtchedBorder(), "Result"));




GroupLayout groupLayout = new GroupLayout(panel);
panel.setLayout(groupLayout);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);

GroupLayout.SequentialGroup HorSGroup= groupLayout.createSequentialGroup();
GroupLayout.SequentialGroup VerSGroup= groupLayout.createSequentialGroup();

GroupLayout.ParallelGroup HParallelGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
GroupLayout.ParallelGroup HParallelGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
GroupLayout.ParallelGroup HParallelGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);

HParallelGroup1.addComponent(label1); //adding components to the group
HParallelGroup1.addComponent(label2);
HParallelGroup2.addComponent(textbox1);
HParallelGroup2.addComponent(textbox2);
HParallelGroup2.addComponent(scroll);
HParallelGroup3.addComponent(button);

HorSGroup.addGroup(HParallelGroup1);
HorSGroup.addGroup(HParallelGroup2);
HorSGroup.addGroup(HParallelGroup3);

GroupLayout.ParallelGroup VerPGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE); //Vertical group
GroupLayout.ParallelGroup VerPGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
GroupLayout.ParallelGroup VerPGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);

VerPGroup1.addComponent(label1); //adding components to groups
VerPGroup1.addComponent(textbox1);
VerPGroup1.addComponent(button);
VerPGroup2.addComponent(label2);
VerPGroup2.addComponent(textbox2);
VerPGroup3.addComponent(scroll);

VerSGroup.addGroup(VerPGroup1);
VerSGroup.addGroup(VerPGroup2);
VerSGroup.addGroup(VerPGroup3);

groupLayout.setHorizontalGroup(HorSGroup);
groupLayout.setVerticalGroup(VerSGroup);




Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) (dimension.getWidth()/4);
int y = (int) (dimension.getHeight()/4);
frame.setLocation(x, y); //Places the program almost in the middle

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
{
//what happens when "calculate" is pressed.
String first = null, second = null;
int first1=0, second1=0;

first= textbox1.getText(); //getting textbox1 value.
second= textbox2.getText();

if(!first.isEmpty() && !second.isEmpty()) //If fields are not empty..
{
first1= Integer.parseInt(first); //string to integer.
second1= Integer.parseInt(second);

if(first1<second1){ //Check, if the first number is bigger than second.
System.out.println(first1);
textarea.append(first+"\n");
}// Ja ir pareizi
else
{
JOptionPane.showMessageDialog(null,"Incorrect data. " );
}
} else
{
JOptionPane.showMessageDialog(null,"Incorrect data." );
}
}
});


frame.add(panel); //add the panel
frame.setSize(500, 500); //program size in pix
frame.setResizable(false); //putting that the frame can't change size
frame.setTitle("Kursa darbs");
frame.setVisible(true);


}

}

嗯,主要问题是文本区域。如您所见,英语不是我的母语,我是 JFrame 的初学者。我已经尝试了一切...请帮助。

谢谢你。

最佳答案

不要将 JTextArea 添加到组中,添加包含它的 JScrollPane!

    //HParallelGroup2.addComponent(textarea);
HParallelGroup2.addComponent(scroll);
HParallelGroup3.addComponent(button);

HorSGroup.addGroup(HParallelGroup1); //Horizontālās grupas tiek pievienotas sakārtotajai grupai
HorSGroup.addGroup(HParallelGroup2);
HorSGroup.addGroup(HParallelGroup3);

GroupLayout.ParallelGroup VerPGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE); //Izveido Vertikālās grupas.
GroupLayout.ParallelGroup VerPGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
GroupLayout.ParallelGroup VerPGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);

VerPGroup1.addComponent(label1); //Pievieno objektus
VerPGroup1.addComponent(textbox1);
VerPGroup1.addComponent(button);
VerPGroup2.addComponent(label2);
VerPGroup2.addComponent(textbox2);
//VerPGroup3.addComponent(textarea);
VerPGroup3.addComponent(scroll);

关于java - 如何在 Java 中为组布局设置 Scrollable TextArea?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13344289/

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