私は問題があります。毎回、TextAreaにSymbols(Numbers)を追加しても、スクロールできません。編集:今それは私が望むように動作します。私は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);
}
}
さて、主な問題はTextareaです。ご覧のとおり、英語は私の母国語ではなく、JFrameの初心者です。私はすべてを試しました...助けてください。
すでにありがとうございます。